Skip to content

Commit

Permalink
Fix route:list --except-vendor hiding Route::view() & redirect() (#41465
Browse files Browse the repository at this point in the history
)

Don't consider Illuminate\Routing\RedirectController & ViewController
as vendor-registered routes. Assume Route::view() or Route::redirect()
were called in userland routes/*.php.
  • Loading branch information
derekmd committed Mar 14, 2022
1 parent 5897180 commit bd422ae
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/Illuminate/Foundation/Console/RouteListCommand.php
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down
12 changes: 11 additions & 1 deletion src/Illuminate/Routing/Route.php
Expand Up @@ -270,14 +270,24 @@ protected function runController()
public function getController()
{
if (! $this->controller) {
$class = $this->parseControllerCallback()[0];
$class = $this->getControllerClass();

$this->controller = $this->container->make(ltrim($class, '\\'));
}

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.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/Testing/Console/RouteListCommandTest.php
Expand Up @@ -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, ['-v' => true, '--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();
Expand Down

0 comments on commit bd422ae

Please sign in to comment.