Skip to content

Commit a4b85dd

Browse files
committed
Add a getter for the middleware that merges controller middleware.
Signed-off-by: Jason Lewis <[email protected]>
1 parent b08b1d9 commit a4b85dd

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/Console/Command/Routes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ protected function getRoutes()
9696
*/
9797
protected function routeHasAuthMiddleware($route)
9898
{
99-
$middleware = $route->getAction()['middleware'];
99+
$middleware = $route->getMiddleware();
100100

101-
return array_search('api.auth', $middleware) !== false;
101+
return isset($middleware['api.auth']);
102102
}
103103

104104
/**

src/Routing/Route.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ class Route
114114
*/
115115
protected $conditionalRequest = true;
116116

117+
/**
118+
* Middleware applied to route.
119+
*
120+
* @var array
121+
*/
122+
protected $middleware;
123+
117124
/**
118125
* Create a new route instance.
119126
*
@@ -238,15 +245,41 @@ public function getController()
238245
{
239246
if (! isset($this->action['uses']) || ! is_string($this->action['uses'])) {
240247
return;
248+
} elseif (isset($this->controller)) {
249+
return $this->controller;
241250
}
242251

243252
if (str_contains($this->action['uses'], '@')) {
244253
list($controller, $this->method) = explode('@', $this->action['uses']);
245254

246-
$this->controller = $this->container->make($controller);
255+
return $this->controller = $this->container->make($controller);
256+
}
257+
}
258+
259+
/**
260+
* Get the middleware applied to the route.
261+
*
262+
* @return array
263+
*/
264+
public function getMiddleware()
265+
{
266+
if (! is_null($this->middleware)) {
267+
return $this->middleware;
268+
}
269+
270+
$this->middleware = [];
271+
272+
foreach ($this->action['middleware'] as $middleware) {
273+
list ($middleware, $options) = array_merge(explode(':', $middleware), [[]]);
274+
275+
$this->middleware[$middleware] = $options;
276+
}
277+
278+
if ($controller = $this->getController()) {
279+
$this->middleware = array_merge($this->middleware, $controller->getMiddleware());
247280
}
248281

249-
return $this->controller;
282+
return $this->middleware;
250283
}
251284

252285
/**

0 commit comments

Comments
 (0)