mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
[!!!][FEATURE] Removed php-di as suggested by #477
NOTE: Custom class-loader should be used to create custom integrations with frameworks like php-di. See documentation for more information. - Removed all references to php-cli from composer + code. - Added ClassLoader php-unit tests.
This commit is contained in:
@@ -2,22 +2,10 @@
|
||||
|
||||
namespace Pecee\SimpleRouter\ClassLoader;
|
||||
|
||||
use DI\Container;
|
||||
use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
||||
|
||||
class ClassLoader implements IClassLoader
|
||||
{
|
||||
/**
|
||||
* Dependency injection enabled
|
||||
* @var bool
|
||||
*/
|
||||
protected $useDependencyInjection = false;
|
||||
|
||||
/**
|
||||
* @var Container|null
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* Load class
|
||||
*
|
||||
@@ -27,21 +15,10 @@ class ClassLoader implements IClassLoader
|
||||
*/
|
||||
public function loadClass(string $class)
|
||||
{
|
||||
if (class_exists($class) === false) {
|
||||
if (\class_exists($class) === false) {
|
||||
throw new NotFoundHttpException(sprintf('Class "%s" does not exist', $class), 404);
|
||||
}
|
||||
|
||||
if ($this->useDependencyInjection === true) {
|
||||
$container = $this->getContainer();
|
||||
if ($container !== null) {
|
||||
try {
|
||||
return $container->get($class);
|
||||
} catch (\Exception $e) {
|
||||
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new $class();
|
||||
}
|
||||
|
||||
@@ -55,64 +32,7 @@ class ClassLoader implements IClassLoader
|
||||
*/
|
||||
public function loadClosure(Callable $closure, array $parameters)
|
||||
{
|
||||
if ($this->useDependencyInjection === true) {
|
||||
$container = $this->getContainer();
|
||||
if ($container !== null) {
|
||||
try {
|
||||
return $container->call($closure, $parameters);
|
||||
} catch (\Exception $e) {
|
||||
throw new NotFoundHttpException($e->getMessage(), (int)$e->getCode(), $e->getPrevious());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return \call_user_func_array($closure, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get dependency injector container.
|
||||
*
|
||||
* @return Container|null
|
||||
*/
|
||||
public function getContainer(): ?Container
|
||||
{
|
||||
return $this->container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the dependency-injector container.
|
||||
*
|
||||
* @param Container $container
|
||||
* @return ClassLoader
|
||||
*/
|
||||
public function setContainer(Container $container): self
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable dependency injection.
|
||||
*
|
||||
* @param bool $enabled
|
||||
* @return static
|
||||
*/
|
||||
public function useDependencyInjection(bool $enabled): self
|
||||
{
|
||||
$this->useDependencyInjection = $enabled;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if dependency injection is enabled.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isDependencyInjectionEnabled(): bool
|
||||
{
|
||||
return $this->useDependencyInjection;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -143,7 +143,7 @@ class EventHandler implements IEventHandler
|
||||
* Get events.
|
||||
*
|
||||
* @param string|null $name Filter events by name.
|
||||
* @param array ...$names Add multiple names...
|
||||
* @param array|string ...$names Add multiple names...
|
||||
* @return array
|
||||
*/
|
||||
public function getEvents(?string $name, ...$names): array
|
||||
|
||||
@@ -77,7 +77,6 @@ abstract class Route implements IRoute
|
||||
$router->debug('Executing callback');
|
||||
|
||||
/* When the callback is a function */
|
||||
|
||||
return $router->getClassLoader()->loadClosure($callback, $parameters);
|
||||
}
|
||||
|
||||
@@ -250,7 +249,7 @@ abstract class Route implements IRoute
|
||||
/**
|
||||
* Set callback
|
||||
*
|
||||
* @param string|array\Closure $callback
|
||||
* @param string|array|\Closure $callback
|
||||
* @return static
|
||||
*/
|
||||
public function setCallback($callback): IRoute
|
||||
|
||||
@@ -306,7 +306,7 @@ class Router
|
||||
*
|
||||
* @return string|null
|
||||
* @throws NotFoundHttpException
|
||||
* @throws TokenMismatchException
|
||||
* @throws \Pecee\Http\Middleware\Exceptions\TokenMismatchException
|
||||
* @throws HttpException
|
||||
* @throws \Exception
|
||||
*/
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
|
||||
namespace Pecee\SimpleRouter;
|
||||
|
||||
use DI\Container;
|
||||
use Pecee\Exceptions\InvalidArgumentException;
|
||||
use Pecee\Http\Middleware\BaseCsrfVerifier;
|
||||
use Pecee\Http\Request;
|
||||
@@ -533,17 +532,12 @@ class SimpleRouter
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable dependency injection
|
||||
*
|
||||
* @param Container $container
|
||||
* @return IClassLoader
|
||||
* Set custom class-loader class used.
|
||||
* @param IClassLoader $classLoader
|
||||
*/
|
||||
public static function enableDependencyInjection(Container $container): IClassLoader
|
||||
public static function setCustomClassLoader(IClassLoader $classLoader): void
|
||||
{
|
||||
return static::router()
|
||||
->getClassLoader()
|
||||
->useDependencyInjection(true)
|
||||
->setContainer($container);
|
||||
static::router()->setClassLoader($classLoader);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user