diff --git a/Router.php b/Router.php index 063a2f9..1d6a90a 100644 --- a/Router.php +++ b/Router.php @@ -14,98 +14,104 @@ * * @package CodeZone\Router */ -class Router { - /** - * @var Router|null - */ - protected static ?Router $instance = null; - /** - * @var Container|mixed - */ - public Container $container; - /** - * @var array - */ - public array $config; - /** - * @var DispatcherFactory - */ - protected DispatcherFactory $dispatcherFactory; +class Router +{ + /** + * @var Router|null + */ + protected static ?Router $instance = null; + /** + * @var Container|mixed + */ + public Container $container; + /** + * @var array + */ + public array $config; + /** + * @var DispatcherFactory + */ + protected DispatcherFactory $dispatcherFactory; - /** - * @param array $config - * @param DispatcherFactory $dispatcherFactory - * - * @throws Exception - */ - public function __construct( array $config, DispatcherFactory $dispatcherFactory ) { - static::validateConfig( $config ); + /** + * @param array $config + * @param DispatcherFactory $dispatcherFactory + * + * @throws Exception + */ + public function __construct(array $config, DispatcherFactory $dispatcherFactory) + { + static::validateConfig($config); - $this->config = $config; - $this->container = $config['container']; - $this->dispatcherFactory = $dispatcherFactory; - } + $this->config = $config; + $this->container = $config['container']; + $this->dispatcherFactory = $dispatcherFactory; + } - /** - * Validate the router config - * - * @param array $config - * - * @throws Exception - */ - public static function validateConfig( array $config ): void { - if ( ! $config['container'] instanceof Container ) { - throw new Exception( 'Container must be an instance of Illuminate\Container\Container' ); - } - } + /** + * Validate the router config + * + * @param array $config + * + * @throws Exception + */ + public static function validateConfig(array $config): void + { + if (! $config['container'] instanceof Container) { + throw new Exception('Container must be an instance of Illuminate\Container\Container'); + } + } - /** - * Get the router instance - * - * @return Router\Router - * @throws Exception - */ - public static function instance(): Router { - if ( ! static::$instance ) { - throw new Exception( 'Router not registered.' ); - } + /** + * Get the router instance + * + * @return Router\Router + * @throws Exception + */ + public static function instance(): Router + { + if (! static::$instance) { + throw new Exception('Router not registered.'); + } - return static::$instance; - } + return static::$instance; + } - /** - * Register the router with a container - * - * @param Config $config - * - * @throws Exception - */ - public static function register( array $config ): Router { - static::validateConfig( $config ); + /** + * Register the router with a container + * + * @param Config $config + * + * @throws Exception + */ + public static function register(array $config): Router + { + static::validateConfig($config); - $container = $config['container']; + $container = $config['container']; - if ( ! $container->has( self::class ) ) { - $container->singleton( self::class, function ( $container ) use ( $config ) { - return new Router( $config, $container->make( DispatcherFactory::class ) ); - } ); - } + if (! $container->has(self::class)) { + $container->singleton(self::class, function ($container) use ($config) { + return new Router($config, $container->make(DispatcherFactory::class)); + }); + } - $instance = $container->make( self::class ); - self::$instance = $instance; + $instance = $container->make(self::class); + self::$instance = $instance; - return $instance; - } + return $instance; + } - /** - * Register routes via a callback - * - * @param callable $callback - * @param array $options - * - * @return Dispatcher - */ - public function routes( callable $callback, array $options = [] ): Dispatcher { - return $this->dispatcherFactory->make( $callback, $options ); - } + /** + * Register routes via a callback + * + * @param callable $callback + * @param array $options + * + * @return Dispatcher + */ + public function routes(callable $callback, array $options = []): Dispatcher + { + return $this->dispatcherFactory->make($callback, $options); + } } diff --git a/src/FastRoute/Routes.php b/src/FastRoute/Routes.php index 5a3674a..43fb378 100644 --- a/src/FastRoute/Routes.php +++ b/src/FastRoute/Routes.php @@ -5,6 +5,7 @@ use CodeZone\Router\Controllers\CallbackController; use CodeZone\Router\Factories\ConditionFactory; use FastRoute\RouteCollector; +use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Support\Collection; use function CodeZone\Router\container; @@ -61,7 +62,7 @@ protected function normalizeHandler( $httpMethod, $route, $handler ): array { } if ( is_callable( $handler ) ) { - $handler = [CallbackController::class, 'handle', [ 'handler' => $handler ] ]; + $handler = [ CallbackController::class, 'handle', [ 'handler' => $handler ] ]; } if ( ! isset( $handler[2] ) ) { @@ -125,11 +126,20 @@ public function middleware( $middleware, $callback ) { */ public function addMiddleware( $middleware, $callback ) { $middleware = is_string( $middleware ) ? [ $middleware ] : $middleware; - $this->currentMiddleware = $middleware; + $this->currentMiddleware = array_merge( $this->currentMiddleware, $middleware ); $callback( $this ); - $this->currentMiddleware = []; + $this->currentMiddleware = array_diff( $this->currentMiddleware, $middleware ); } + /** + * Alias for addCondition + * + * @param $condition + * @param callable $callback + * + * @return void + * @throws BindingResolutionException + */ public function condition( $condition, callable $callback ) { $this->addCondition( $condition, $callback ); } @@ -141,6 +151,7 @@ public function condition( $condition, callable $callback ) { * @param callable $callback * * @return void + * @throws BindingResolutionException */ public function addCondition( $condition, callable $callback ) { $condition = container()->make( ConditionFactory::class )->make( $condition );