1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router\Command; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use RuntimeException; |
18
|
|
|
use Sunrise\Http\Router\Router; |
19
|
|
|
use Symfony\Component\Console\Command\Command; |
20
|
|
|
use Symfony\Component\Console\Helper\Table; |
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Import functions |
26
|
|
|
*/ |
27
|
|
|
use function join; |
28
|
|
|
use function sprintf; |
29
|
|
|
use function Sunrise\Http\Router\path_plain; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* This command will list all routes in your application |
33
|
|
|
* |
34
|
|
|
* If you cannot pass the router to the constructor |
35
|
|
|
* or your architecture has problems with autowiring, |
36
|
|
|
* then just inherit this class and override the getRouter method. |
37
|
|
|
* |
38
|
|
|
* @since 2.9.0 |
39
|
|
|
*/ |
40
|
|
|
class RouteListCommand extends Command |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
protected static $defaultName = 'router:route-list'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
protected static $defaultDescription = 'Lists all routes in your application'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The router instance populated with routes |
55
|
|
|
* |
56
|
|
|
* @var Router|null |
57
|
|
|
*/ |
58
|
|
|
private $router; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Constructor of the class |
62
|
|
|
* |
63
|
|
|
* @param Router|null $router |
64
|
|
|
*/ |
65
|
3 |
|
public function __construct(?Router $router = null) |
66
|
|
|
{ |
67
|
3 |
|
parent::__construct(); |
68
|
|
|
|
69
|
3 |
|
$this->router = $router; |
70
|
3 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Gets the router instance populated with routes |
74
|
|
|
* |
75
|
|
|
* @return Router |
76
|
|
|
* |
77
|
|
|
* @throws RuntimeException |
78
|
|
|
* If the command doesn't contain the router instance. |
79
|
|
|
* |
80
|
|
|
* @since 2.11.0 |
81
|
|
|
*/ |
82
|
2 |
|
protected function getRouter() : Router |
83
|
|
|
{ |
84
|
2 |
|
if (null === $this->router) { |
85
|
1 |
|
throw new RuntimeException(sprintf( |
86
|
|
|
'The %2$s() method MUST return the %1$s class instance. ' . |
87
|
1 |
|
'Pass the %1$s class instance to the constructor, or override the %2$s() method.', |
88
|
1 |
|
Router::class, |
89
|
|
|
__METHOD__ |
90
|
1 |
|
)); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return $this->router; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
3 |
|
final protected function execute(InputInterface $input, OutputInterface $output) : int |
100
|
|
|
{ |
101
|
3 |
|
$table = new Table($output); |
102
|
|
|
|
103
|
3 |
|
$table->setHeaders([ |
104
|
3 |
|
'Name', |
105
|
|
|
'Host', |
106
|
|
|
'Path', |
107
|
|
|
'Verb', |
108
|
|
|
]); |
109
|
|
|
|
110
|
3 |
|
foreach ($this->getRouter()->getRoutes() as $route) { |
111
|
1 |
|
$table->addRow([ |
112
|
1 |
|
$route->getName(), |
113
|
1 |
|
$route->getHost() ?? 'ANY', |
114
|
1 |
|
path_plain($route->getPath()), |
|
|
|
|
115
|
1 |
|
join(', ', $route->getMethods()), |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
$table->render(); |
120
|
|
|
|
121
|
2 |
|
return 0; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|