Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 6 additions & 64 deletions src/Illuminate/Foundation/Console/RouteListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Foundation\Console;

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Routing\Route;
Expand Down Expand Up @@ -137,74 +138,15 @@ protected function displayRoutes(array $routes)
*/
protected function getMiddleware($route)
{
$middlewares = array_values($route->middleware());
$middlewares = $route->gatherMiddleware();

$actionName = $route->getActionName();

if (! empty($actionName) && $actionName !== 'Closure') {
$middlewares = array_merge($middlewares, $this->getControllerMiddleware($actionName));
}

return implode(',', $middlewares);
}

/**
* Get the middleware for the given Controller@action name.
*
* @param string $actionName
* @return array
*/
protected function getControllerMiddleware($actionName)
{
$segments = explode('@', $actionName);

return $this->getControllerMiddlewareFromInstance(
$this->laravel->make($segments[0]),
isset($segments[1]) ? $segments[1] : null
);
}

/**
* Get the middlewares for the given controller instance and method.
*
* @param \Illuminate\Routing\Controller $controller
* @param string|null $method
* @return array
*/
protected function getControllerMiddlewareFromInstance($controller, $method)
{
if (! method_exists($controller, 'getMiddleware')) {
return [];
}

$middleware = $this->router->getMiddleware();

$results = [];

foreach ($controller->getMiddleware() as $data) {
if (! is_string($data['middleware'])) {
continue;
}

if (! $method || ! $this->methodExcludedByOptions($method, $data['options'])) {
$results[] = Arr::get($middleware, $data['middleware'], $data['middleware']);
foreach ($middlewares as $i => $middleware) {
if ($middleware instanceof Closure) {
$middlewares[$i] = 'Closure';
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know what the standards are for Laravel, but I (personally) would rewrite this with references:

foreach ($middlewares as &$middleware) {
    if ($middleware instanceof Closure) {
        $middleware = 'Closure';
    }
}


return $results;
}

/**
* Determine if the given options exclude a particular method.
*
* @param string $method
* @param array $options
* @return bool
*/
protected function methodExcludedByOptions($method, array $options)
{
return (! empty($options['only']) && ! in_array($method, (array) $options['only'])) ||
(! empty($options['except']) && in_array($method, (array) $options['except']));
return implode(',', $middlewares);
}

/**
Expand Down