Skip to content

Commit

Permalink
[9.x] Add option to filter out routes defined in vendor packages in `…
Browse files Browse the repository at this point in the history
…route:list` command (#41254)

* Add option to filter out routes defined in vendor

* CS Fix

* Update RouteListCommand.php

* Update RouteListCommand.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
amiranagram and taylorotwell committed Feb 28, 2022
1 parent 219e0ea commit aa2853e
Showing 1 changed file with 26 additions and 1 deletion.
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'],
];
}
}

0 comments on commit aa2853e

Please sign in to comment.