ListCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 47
ccs 0
cts 28
cp 0
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 5 1
A execute() 0 28 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Command\Router;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Helper\TableSeparator;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Yiisoft\Router\RouteCollectionInterface;
13
use Yiisoft\Yii\Console\ExitCode;
14
15
class ListCommand extends Command
16
{
17
    private RouteCollectionInterface $routeCollection;
18
19
    protected static $defaultName = 'router/list';
20
21
    public function __construct(RouteCollectionInterface $routeCollection)
22
    {
23
        $this->routeCollection = $routeCollection;
24
        parent::__construct();
25
    }
26
27
    protected function configure(): void
28
    {
29
        $this
30
            ->setDescription('List all registered routes')
31
            ->setHelp('This command displays a list of registered routes.');
32
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output): int
35
    {
36
        $table = new Table($output);
37
        $routes = $this->routeCollection->getRoutes();
38
        uasort(
39
            $routes,
40
            static function ($a, $b) {
41
                return ($a->getHost() <=> $b->getHost()) ?: ($a->getName() <=> $b->getName());
42
            }
43
        );
44
        $table->setHeaders(['Host', 'Methods', 'Name', 'Pattern', 'Defaults']);
45
        foreach ($routes as $route) {
46
            $table->addRow(
47
                [
48
                    $route->getHost(),
49
                    implode(',', $route->getMethods()),
50
                    $route->getName(),
51
                    $route->getPattern(),
52
                    implode(',', $route->getDefaults()),
53
                ]
54
            );
55
            if (next($routes) !== false) {
56
                $table->addRow(new TableSeparator());
57
            }
58
        }
59
60
        $table->render();
61
        return ExitCode::OK;
62
    }
63
}
64