Skip to content

Commit

Permalink
[9.x] Add counts to route:list command (#42551)
Browse files Browse the repository at this point in the history
* Added route counts to the route:list command.

* Only display count at the bottom of the CLI output.
  • Loading branch information
ash-jc-allen committed May 31, 2022
1 parent 3277687 commit 9a9c991
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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

0 comments on commit 9a9c991

Please sign in to comment.