Passed
Push — master ( c82314...7a7802 )
by Filipe
15:08 queued 14s
created

RouterDebugCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 15
rs 9.9666
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Infrastructure\Console;
13
14
use Symfony\Component\Console\Attribute\AsCommand;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use Symfony\Component\Routing\Route;
21
use Symfony\Component\Routing\RouteCollection;
22
use Symfony\Component\Routing\RouterInterface;
23
24
/**
25
 * RouterDebugCommand
26
 *
27
 * @package Slick\WebStack\Infrastructure\Console
28
 */
29
#[AsCommand(name: 'debug:router', description: 'Display current routes for an application')]
30
final class RouterDebugCommand extends Command
31
{
32
33
    public function __construct(private RouterInterface $router)
34
    {
35
        parent::__construct();
36
    }
37
38
    protected function configure(): void
39
    {
40
        $this
41
            ->setDefinition([
42
                new InputArgument('name', InputArgument::OPTIONAL, 'A route name'),
43
            ])
44
            ->setHelp("Displays configured routes.")
45
        ;
46
    }
47
48
    protected function execute(InputInterface $input, OutputInterface $output): int
49
    {
50
        $style = new SymfonyStyle($input, $output);
51
        $name = $input->getArgument('name');
52
        $routes = $this->router->getRouteCollection();
53
54
        if ($name) {
55
            $route = $routes->get($name);
56
            $this->renderRouteTable($route, $style, $name);
57
            return Command::SUCCESS;
58
        }
59
60
        $this->renderListTable($routes, $style);
61
62
        return Command::SUCCESS;
63
    }
64
65
    private function renderListTable(RouteCollection $routes, SymfonyStyle $style): void
66
    {
67
        $table = $style->createTable();
68
69
        $table->setHeaderTitle("Configured routes");
70
        $headers = ['Name', 'Methods', 'Host', 'Path', 'Controller'];
71
        $table->setHeaders($headers);
72
        foreach ($routes as $name => $route) {
73
            $row = [
74
                $name,
75
                empty($route->getMethods()) ? "ANY" : implode(", ", $route->getMethods()),
76
                strlen($route->getHost()) > 0 ? $route->getHost() : "ANY",
77
                $route->getPath(),
78
                $route->getDefault('_controller')."::".$route->getDefault('_action')."()"
79
            ];
80
81
            $table->addRow($row);
82
        }
83
84
        $style->writeln('');
85
        $table->render();
86
    }
87
88
    private function renderRouteTable(?Route $route, SymfonyStyle $style, string $name): void
89
    {
90
        if (!$route) {
91
            $style->warning("There's no route named: '$name'.");
92
            return;
93
        }
94
95
        $table = $style->createTable();
96
97
        $table->setHeaderTitle("Found route");
98
        $headers = ['Property', 'Value'];
99
        $table->setHeaders($headers);
100
        $options = [];
101
        foreach ($route->getOptions() as $key => $value) {
102
            $options[] = [$key, $value];
103
        }
104
        $table->addRows([
105
            ["Route name", $name],
106
            ["Methods", empty($route->getMethods()) ? "ANY" : implode(", ", $route->getMethods())],
107
            ["Host", strlen($route->getHost()) > 0 ? $route->getHost() : "ANY"],
108
            ["Path", $route->getPath()],
109
            ["Controller", $route->getDefault('_controller')."::".$route->getDefault('_action')."()"],
110
            ...$options
111
        ]);
112
113
        $style->writeln('');
114
        $table->render();
115
    }
116
}
117