Test Failed
Push — develop ( 0204ba...bd77d0 )
by nguereza
03:38
created

RouteCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 50
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 4 2
A __construct() 0 8 1
A showRouteList() 0 25 2
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Platine\Framework\Demo\Command;
10
11
use Platine\Config\Config;
12
use Platine\Console\Command\Command;
13
use Platine\Framework\App\Application;
14
use Platine\Route\Route;
15
use Platine\Route\Router;
16
use Platine\Stdlib\Helper\Str;
17
18
/**
19
 * Description of RouteCommand
20
 *
21
 * @author tony
22
 */
23
class RouteCommand extends Command
24
{
25
    protected Application $application;
26
    protected Router $router;
27
28
    /**
29
     *
30
     */
31
    public function __construct(Application $application, Router $router)
32
    {
33
        parent::__construct('route', 'Command to manage route');
34
35
        $this->addOption('-l|--list', 'Show route list', null, false);
36
37
        $this->router = $router;
38
        $this->application = $application;
39
    }
40
41
    public function execute()
42
    {
43
        if ($this->getOptionValue('list')) {
44
            $this->showRouteList();
45
        }
46
    }
47
48
    protected function showRouteList(): void
49
    {
50
        $writer = $this->io()->writer();
51
52
        $writer->boldGreen('ROUTE LIST', true)->eol();
53
        /** @template T @var Config<T> $config */
54
        $config = $this->application->get(Config::class);
55
        $routeList = $config->get('routes', []);
56
        $routeList[0]($this->router);
57
58
        $routes = $this->router->routes()->all();
59
        $rows = [];
60
        foreach ($routes as /** @var Route $route */ $route) {
61
            $handler = Str::stringify($route->getHandler());
62
            $rows[] = [
63
                'name' => $route->getName(),
64
                'method' => implode('|', $route->getMethods()),
65
                'path' => $route->getPattern(),
66
                'handler' => $handler,
67
            ];
68
        }
69
70
        $writer->table($rows);
71
72
        $writer->green('Command finished successfully')->eol();
73
    }
74
}
75