From 57bd20b3a669def8e09c24ed9fe84704070a395e Mon Sep 17 00:00:00 2001 From: Derek Date: Wed, 9 Mar 2022 18:43:59 -0500 Subject: [PATCH] Fix route:list --except-vendor hiding Route::view() & redirect() Don't consider Illuminate\Routing\RedirectController & ViewController as vendor-registered routes. Assume Route::view() or Route::redirect() were called in userland routes/*.php. --- .../Foundation/Console/RouteListCommand.php | 20 ++++++++++++++++++- src/Illuminate/Routing/Route.php | 12 ++++++++++- .../Testing/Console/RouteListCommandTest.php | 15 ++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Console/RouteListCommand.php b/src/Illuminate/Foundation/Console/RouteListCommand.php index c623f0e9969a..3b7f62d08aeb 100644 --- a/src/Illuminate/Foundation/Console/RouteListCommand.php +++ b/src/Illuminate/Foundation/Console/RouteListCommand.php @@ -223,7 +223,11 @@ protected function isVendorRoute(Route $route) str_contains($route->action['uses'], 'SerializableClosure')) { return false; } elseif (is_string($route->action['uses'])) { - $path = (new ReflectionClass(explode('@', $route->action['uses'])[0])) + if ($this->isFrameworkController($route)) { + return false; + } + + $path = (new ReflectionClass($route->getControllerClass())) ->getFileName(); } else { return false; @@ -232,6 +236,20 @@ protected function isVendorRoute(Route $route) return str_starts_with($path, base_path('vendor')); } + /** + * Determine if the route uses a framework controller. + * + * @param \Illuminate\Routing\Route $route + * @return bool + */ + protected function isFrameworkController(Route $route) + { + return in_array($route->getControllerClass(), [ + '\Illuminate\Routing\RedirectController', + '\Illuminate\Routing\ViewController', + ], true); + } + /** * Filter the route by URI and / or name. * diff --git a/src/Illuminate/Routing/Route.php b/src/Illuminate/Routing/Route.php index 92acb5c098cd..25be726a1ba7 100755 --- a/src/Illuminate/Routing/Route.php +++ b/src/Illuminate/Routing/Route.php @@ -270,7 +270,7 @@ protected function runController() public function getController() { if (! $this->controller) { - $class = $this->parseControllerCallback()[0]; + $class = $this->getControllerClass(); $this->controller = $this->container->make(ltrim($class, '\\')); } @@ -278,6 +278,16 @@ public function getController() return $this->controller; } + /** + * Get the controller class used for the route. + * + * @return string + */ + public function getControllerClass() + { + return $this->parseControllerCallback()[0]; + } + /** * Get the controller method used for the route. * diff --git a/tests/Testing/Console/RouteListCommandTest.php b/tests/Testing/Console/RouteListCommandTest.php index ea6808e55fcd..b4f3eeb924bd 100644 --- a/tests/Testing/Console/RouteListCommandTest.php +++ b/tests/Testing/Console/RouteListCommandTest.php @@ -113,6 +113,21 @@ public function testRouteCanBeFilteredByName() ->expectsOutput(''); } + public function testDisplayRoutesExceptVendor() + { + $this->router->get('foo/{user}', [FooController::class, 'show']); + $this->router->view('view', 'blade.path'); + $this->router->redirect('redirect', 'destination'); + + $this->artisan(RouteListCommand::class, ['--except-vendor' => true]) + ->assertSuccessful() + ->expectsOutput('') + ->expectsOutput(' GET|HEAD foo/{user} Illuminate\Tests\Testing\Console\FooController@show') + ->expectsOutput(' ANY redirect .... Illuminate\Routing\RedirectController') + ->expectsOutput(' GET|HEAD view .............................................. ') + ->expectsOutput(''); + } + protected function tearDown(): void { parent::tearDown();