Skip to content

Commit

Permalink
Fixing bug where nested middleware was overwritten instead of applied.
Browse files Browse the repository at this point in the history
  • Loading branch information
incraigulous committed Apr 5, 2024
1 parent 579060f commit b18d2b9
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 86 deletions.
172 changes: 89 additions & 83 deletions Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
17 changes: 14 additions & 3 deletions src/FastRoute/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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] ) ) {
Expand Down Expand Up @@ -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 );
}
Expand All @@ -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 );
Expand Down

0 comments on commit b18d2b9

Please sign in to comment.