|
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
|
|
|
|