Skip to content

Commit

Permalink
fix: patch up middleware not working in groups
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Jun 30, 2023
1 parent ee0630f commit 9417720
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Router.php
Expand Up @@ -62,15 +62,21 @@ public static function mount(string $path, $handler)

$namespace = static::$namespace;
$initialGroupRoute = static::$groupRoute;
$initialGroupMiddleware = static::$routeMiddleware;

if ($groupOptions['namespace']) {
static::$namespace = $groupOptions['namespace'];
}

static::$groupRoute = static::$groupRoute . (strpos($path, '/') !== 0 ? "/$path" : $path);

if (isset($groupOptions['middleware'])) {
static::$routeMiddleware = $groupOptions['middleware'];
}

call_user_func($handler);

static::$routeMiddleware = $initialGroupMiddleware;
static::$namespace = $namespace;
static::$groupRoute = $initialGroupRoute;
}
Expand Down Expand Up @@ -143,9 +149,8 @@ public static function match(string $methods, string $pattern, $handler)
static::$namedRoutes[$routeOptions['name']] = $pattern;
}


if ($routeOptions['middleware']) {
static::before($methods, $rawPattern, $routeOptions['middleware']);
if ($routeOptions['middleware'] || !empty(static::$routeMiddleware)) {
static::before($methods, $rawPattern, $routeOptions['middleware'] ?? static::$routeMiddleware);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Router/Core.php
Expand Up @@ -76,6 +76,11 @@ class Core
*/
protected static $namedRoutes = [];

/**
* Route based middleware
*/
protected static $routeMiddleware = [];

/**
* Current group base path
*/
Expand Down
140 changes: 140 additions & 0 deletions tests/middleware.test.php
Expand Up @@ -110,4 +110,144 @@ public function call()

expect(ob_get_contents())->toBe('12');
ob_end_clean();

// resets
$router->hook('router.after', function () {});
});

test('middleware is only called for routes that run', function () {
$_SERVER['REQUEST_METHOD'] = 'PUT';
$_SERVER['REQUEST_URI'] = '/users/disable/5';

$router = new Router;

$router->group('/users', function () use ($router) {
/**
* Disables a user
*/
$router->put('/disable/{id}', [
'middleware' => function () {
echo 'mid 1';
},
function ($id) {
echo 'test 1';
}
]);

$router->put('/enable/{id}', [
'middleware' => function () {
echo 'mid 2';
},
function ($id) {
echo 'test 2';
}
]);

$router->put('/delete/{id}', [
'middleware' => function () {
echo 'mid 3';
},
function ($id) {
echo 'test 3';
}
]);
});

ob_start();
$router->run();

expect(ob_get_contents())->toBe('mid 1test 1');
ob_end_clean();
});

test('in-route named middleware', function () {
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/thisRoute';

$router = new Router;
$router->registerMiddleware('mid1', function () use ($router) {
echo 'named middleware --- ';
});

$router->get('/thisRoute', ['middleware' => 'mid1', function () {
echo 'route';
}]);

ob_start();
$router->run();

expect(ob_get_contents())->toBe('named middleware --- route');

ob_end_clean();
});

test('in-route middleware + groups', function () {
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/thatGroup/thatRoute';

$router = new Router;
$router->registerMiddleware('mid1', function () use ($router) {
echo 'named middleware 3 --- ';
});

$router->group('/thatGroup', ['middleware' => 'mid1', function () use ($router) {
$router->get('/thatRoute', function () {
echo 'route';
});
}]);

ob_start();
$router->run();

expect(ob_get_contents())->toBe('named middleware 3 --- route');

ob_end_clean();
});

test('in-route named middleware + groups', function () {
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/thisGroup/thisRoute';

$router = new Router;
$router->registerMiddleware('mid1', function () use ($router) {
echo 'named middleware 2 --- ';
});

$router->group('/thisGroup', ['middleware' => 'mid1', function () use ($router) {
$router->get('/thisRoute', function () {
echo 'route';
});
}]);

ob_start();
$router->run();

expect(ob_get_contents())->toBe('named middleware 2 --- route');

ob_end_clean();
});

test('in-route named middleware + groups + sub groups', function () {
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/thisGroup/thisSubGroup/thisRoute';

$router = new Router;
$router->registerMiddleware('mid1', function () use ($router) {
echo 'named middleware 3 --- ';
});

$router->group('/thisGroup', ['middleware' => 'mid1', function () use ($router) {
$router->group('/thisSubGroup', function () use ($router) {
$router->get('/thisRoute', function () {
echo 'route';
});
});
}]);

ob_start();
$router->run();

expect(ob_get_contents())->toBe('named middleware 3 --- route');

ob_end_clean();
});

0 comments on commit 9417720

Please sign in to comment.