Test Failed
Push — master ( 992a11...749776 )
by Derek Stephen
14:00
created

RouterCommand::getRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bone\Router\Command;
6
7
use Bone\Http\RouterInterface;
8
use Bone\Router\Router;
9
use League\Route\Route;
10
use League\Route\RouteGroup;
11
use League\Route\Router as LeagueRouter;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Server\RequestHandlerInterface;
15
use ReflectionClass;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use function usort;
21
22
class RouterCommand extends Command
23
{
24
25
    public function __construct(private readonly Router $router)
26
    {
27
        parent::__construct('router:list');
28
    }
29
30
    public function configure(): void
31
    {
32
        $this->setDescription('Lists all routes registered with the router');
33
        $this->setHelp('List all routes');
34
    }
35
36
    public function execute(InputInterface $input, OutputInterface $output): int
37
    {
38
        $io = new SymfonyStyle($input, $output);
39
        $io->title('Configured routes :');
40
        $groups = $this->router->getGroups();
41
42
        foreach ($groups as $group) {
43
            $this->getRoutes($group);
44
        }
45
46
        $routes = $this->router->getRoutes();
47
        $sort = function (Route $a, Route $b) {
48
            return $a->getPath() <=> $b->getPath();
49
        };
50
        usort($routes, $sort);
51
        $paths = [];
52
53
        foreach ($routes as $route) {
54
            $paths[] = [$route->getMethod(), $route->getPath()];
55
        }
56
57
        $io->table(['Method', 'Path'], $paths);
58
59
        return Command::SUCCESS;
60
    }
61
62
    private function getRoutes(RouteGroup $group)
63
    {
64
        $mirror = new ReflectionClass(RouteGroup::class);
65
        $callback = $mirror->getProperty('callback')->getValue($group);
66
        $callback($group);
67
    }
68
}
69