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 counts to route:list command #42551

Merged
merged 3 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
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
26 changes: 25 additions & 1 deletion src/Illuminate/Foundation/Console/RouteListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ protected function forCli($routes)

$terminalWidth = $this->getTerminalWidth();

$routeCount = $this->determineRouteCountOutput($routes, $terminalWidth);

return $routes->map(function ($route) use ($maxMethod, $terminalWidth) {
[
'action' => $action,
Expand Down Expand Up @@ -400,7 +402,29 @@ protected function forCli($routes)
$dots,
str_replace(' ', ' › ', $action),
), $this->output->isVerbose() && ! empty($middleware) ? "<fg=#6C7280>$middleware</>" : null];
})->flatten()->filter()->prepend('')->push('')->toArray();
})
->flatten()
->filter()
->prepend('')
->push('')->push($routeCount)->push('')
->toArray();
}

/**
* Determine and return the output for displaying the number of routes in the CLI output.
*
* @param \Illuminate\Support\Collection $routes
* @param int $terminalWidth
* @return string
*/
protected function determineRouteCountOutput($routes, $terminalWidth)
{
$routeCountText = 'Showing ['.$routes->count().'] routes';

$offset = $terminalWidth - mb_strlen($routeCountText) - 2;
$spaces = str_repeat(' ', $offset);

return $spaces.'<fg=blue;options=bold>Showing ['.$routes->count().'] routes</>';
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Testing/Console/RouteListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public function testDisplayRoutesForCli()
->expectsOutput(' POST controller-invokable Illuminate\Tests\Testing\Console\…')
->expectsOutput(' GET|HEAD controller-method/{user} Illuminate\Tests\Testing\Cons…')
->expectsOutput(' GET|HEAD {account}.example.com/user/{id} ............. user.show')
->expectsOutput('')
->expectsOutput(' Showing [6] routes')
->expectsOutput('');
}

Expand All @@ -92,6 +94,8 @@ public function testDisplayRoutesForCliInVerboseMode()
->expectsOutput(' GET|HEAD controller-method/{user} Illuminate\\Tests\\Testing\\Console\\FooController@show')
->expectsOutput(' GET|HEAD {account}.example.com/user/{id} ............. user.show')
->expectsOutput(' ⇂ web')
->expectsOutput('')
->expectsOutput(' Showing [4] routes')
->expectsOutput('');
}

Expand All @@ -110,6 +114,8 @@ public function testRouteCanBeFilteredByName()
->assertSuccessful()
->expectsOutput('')
->expectsOutput(' GET|HEAD foo ...................................... foo.show')
->expectsOutput('')
->expectsOutput(' Showing [1] routes')
->expectsOutput('');
}

Expand All @@ -125,6 +131,8 @@ public function testDisplayRoutesExceptVendor()
->expectsOutput(' GET|HEAD foo/{user} Illuminate\Tests\Testing\Console\FooController@show')
->expectsOutput(' ANY redirect .... Illuminate\Routing\RedirectController')
->expectsOutput(' GET|HEAD view .............................................. ')
->expectsOutput('')
->expectsOutput(' Showing [3] routes')
->expectsOutput('');
}

Expand Down