Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add option to filter out routes defined in vendor packages in route:list command #41254

Merged
merged 4 commits into from
Feb 28, 2022
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
27 changes: 26 additions & 1 deletion src/Illuminate/Foundation/Console/RouteListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionFunction;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Terminal;

Expand Down Expand Up @@ -148,6 +149,7 @@ protected function getRouteInformation(Route $route)
'name' => $route->getName(),
'action' => ltrim($route->getActionName(), '\\'),
'middleware' => $this->getMiddleware($route),
'vendor' => $this->isVendorRoute($route),
]);
}

Expand Down Expand Up @@ -206,6 +208,27 @@ protected function getMiddleware($route)
})->implode("\n");
}

/**
* Determine if the route has been defined outside of the application.
*
* @param \Illuminate\Routing\Route $route
* @return bool
*/
protected function isVendorRoute(Route $route)
{
if ($route->action['uses'] instanceof Closure) {
$path = (new ReflectionFunction($route->action['uses']))
->getFileName();
} elseif (is_string($route->action['uses'])) {
$path = (new ReflectionClass(explode('@', $route->action['uses'])[0]))
->getFileName();
} else {
return false;
}

return str_starts_with($path, base_path('vendor'));
}

/**
* Filter the route by URI and / or name.
*
Expand All @@ -217,7 +240,8 @@ protected function filterRoute(array $route)
if (($this->option('name') && ! Str::contains($route['name'], $this->option('name'))) ||
($this->option('path') && ! Str::contains($route['uri'], $this->option('path'))) ||
($this->option('method') && ! Str::contains($route['method'], strtoupper($this->option('method')))) ||
($this->option('domain') && ! Str::contains($route['domain'], $this->option('domain')))) {
($this->option('domain') && ! Str::contains($route['domain'], $this->option('domain'))) ||
($this->option('except-vendor') && $route['vendor'])) {
return;
}

Expand Down Expand Up @@ -426,6 +450,7 @@ protected function getOptions()
['except-path', null, InputOption::VALUE_OPTIONAL, 'Do not display the routes matching the given path pattern'],
['reverse', 'r', InputOption::VALUE_NONE, 'Reverse the ordering of the routes'],
['sort', null, InputOption::VALUE_OPTIONAL, 'The column (precedence, domain, method, uri, name, action, middleware) to sort by', 'uri'],
['except-vendor', null, InputOption::VALUE_NONE, 'Do not display routes defined by vendor packages'],
];
}
}