Skip to content

Commit ad41790

Browse files
committed
Include global constraints and middleware when listing routes in the cli
1 parent 2cf6cad commit ad41790

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

phpstan-baseline.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ parameters:
4444
message: "#^Call to an undefined static method PDO::connect\\(\\)\\.$#"
4545
count: 1
4646
path: src/mako/database/connections/Connection.php
47+
-
48+
message: "#^Call to protected method orderMiddlewareByPriority\\(\\) of class mako\\\\http\\\\routing\\\\Dispatcher\\.$#"
49+
count: 1
50+
path: src/mako/application/cli/commands/app/ListRoutes.php

src/mako/application/cli/commands/app/ListRoutes.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use Closure;
1111
use mako\cli\input\arguments\Argument;
1212
use mako\cli\output\helpers\Alert;
13+
use mako\http\routing\Dispatcher;
1314
use mako\http\routing\Route;
15+
use mako\http\routing\Router;
1416
use mako\http\routing\Routes;
1517
use mako\reactor\Command;
1618
use mako\utility\Arr;
@@ -86,7 +88,7 @@ protected function routeMatches(string $filter, Route $route): bool
8688
/**
8789
* Executes the command.
8890
*/
89-
public function execute(Routes $routes, bool $detailed = false, ?string $filter = null): void
91+
public function execute(Routes $routes, Dispatcher $dispatcher, Router $router, bool $detailed = false, ?string $filter = null): void
9092
{
9193
$this->clear();
9294

@@ -102,9 +104,23 @@ public function execute(Routes $routes, bool $detailed = false, ?string $filter
102104

103105
$matched++;
104106

107+
$middleware = (function ($middleware) {
108+
/** @var \mako\http\routing\Dispatcher $this */
109+
return $this->orderMiddlewareByPriority($middleware);
110+
})
111+
->bindTo($dispatcher, Dispatcher::class)([
112+
...$dispatcher->getGlobalMiddleware(),
113+
...$route->getMiddleware(),
114+
]);
115+
116+
$constraints = [
117+
...$router->getGlobalConstraints(),
118+
...$route->getConstraints(),
119+
];
120+
105121
$methods = implode(', ', $route->getMethods());
106-
$middleware = implode(', ', Arr::pluck($route->getMiddleware(), 'middleware'));
107-
$constraints = implode(', ', $route->getConstraints());
122+
$middleware = implode(', ', Arr::pluck($middleware, 'middleware'));
123+
$constraints = implode(', ', $constraints);
108124
$name = $route->getName();
109125

110126
$this->alert("Path: {$route->getRoute()}", Alert::INFO);

src/mako/http/routing/Dispatcher.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ public function registerGlobalMiddleware(string $middleware, mixed ...$parameter
8282
return $this;
8383
}
8484

85+
/**
86+
* Returns the global middleware.
87+
*/
88+
public function getGlobalMiddleware(): array
89+
{
90+
return $this->globalMiddleware;
91+
}
92+
8593
/**
8694
* Orders resolved middleware by priority.
8795
*/
@@ -91,11 +99,21 @@ protected function orderMiddlewareByPriority(array $middleware): array
9199
return $middleware;
92100
}
93101

94-
$priority = array_intersect_key($this->middlewarePriority, $middleware) + array_fill_keys(array_keys(array_diff_key($middleware, $this->middlewarePriority)), static::MIDDLEWARE_DEFAULT_PRIORITY);
102+
// Ensure that each middleware only exists once in the final list
103+
104+
$middlewareByName = [];
105+
106+
foreach ($middleware as $value) {
107+
$middlewareByName[$value['middleware']] = $value;
108+
}
109+
110+
// Sort middleware by priority
111+
112+
$priority = array_intersect_key($this->middlewarePriority, $middlewareByName) + array_fill_keys(array_keys(array_diff_key($middlewareByName, $this->middlewarePriority)), static::MIDDLEWARE_DEFAULT_PRIORITY);
95113

96114
asort($priority);
97115

98-
return [...$priority, ...$middleware];
116+
return [...$priority, ...$middlewareByName];
99117
}
100118

101119
/**
@@ -104,17 +122,7 @@ protected function orderMiddlewareByPriority(array $middleware): array
104122
protected function addMiddlewareToStack(Onion $onion, array $middleware): void
105123
{
106124
if (empty($middleware) === false) {
107-
// Group middleware
108-
109-
$layers = [];
110-
111-
foreach ($middleware as $layer) {
112-
$layers[$layer['middleware']] = $layer;
113-
}
114-
115-
// Add ordered middleware to stack
116-
117-
foreach ($this->orderMiddlewareByPriority($layers) as $layer) {
125+
foreach ($this->orderMiddlewareByPriority($middleware) as $layer) {
118126
$onion->addLayer($layer['middleware'], $layer['parameters']);
119127
}
120128
}

src/mako/http/routing/Router.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public function registerGlobalConstraint(string $constraint, mixed ...$parameter
5353
return $this;
5454
}
5555

56+
/**
57+
* Returns the global constraints.
58+
*/
59+
public function getGlobalConstraints(): array
60+
{
61+
return $this->globalConstraints;
62+
}
63+
5664
/**
5765
* Returns TRUE if the route matches the request path and FALSE if not.
5866
*/

0 commit comments

Comments
 (0)