|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.9 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Console\Commands; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Module\Exceptions\ModuleException; |
|
18
|
|
|
use Quantum\Router\Exceptions\RouteException; |
|
19
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
20
|
|
|
use Quantum\Module\ModuleLoader; |
|
21
|
|
|
use Quantum\Console\QtCommand; |
|
22
|
|
|
use Quantum\Router\Router; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class ServeCommand |
|
26
|
|
|
* @package Quantum\Console |
|
27
|
|
|
*/ |
|
28
|
|
|
class RouteListCommand extends QtCommand |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* The console command name. |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $name = 'route:list'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The console command description. |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $description = 'Display all registered routes'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Command options |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $options = [ |
|
47
|
|
|
['module', 'm', 'optional', 'Filter by module name'], |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Executes the command |
|
52
|
|
|
*/ |
|
53
|
|
|
public function exec() |
|
54
|
|
|
{ |
|
55
|
|
|
try { |
|
56
|
|
|
$modulesRoutes = ModuleLoader::getInstance()->loadModulesRoutes(); |
|
57
|
|
|
|
|
58
|
|
|
Router::setRoutes($modulesRoutes); |
|
59
|
|
|
|
|
60
|
|
|
$routes = Router::getRoutes(); |
|
61
|
|
|
|
|
62
|
|
|
$module = $this->getOption('module'); |
|
63
|
|
|
|
|
64
|
|
|
if ($module) { |
|
65
|
|
|
$routes = array_filter($routes, function ($route) use ($module) { |
|
66
|
|
|
return strtolower($route['module']) === strtolower($module); |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
if (empty($routes)) { |
|
70
|
|
|
$this->error('The module is not found'); |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$rows = []; |
|
76
|
|
|
|
|
77
|
|
|
foreach ($routes as $route) { |
|
78
|
|
|
$rows[] = $this->composeTableRow($route, 50); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$table = new Table($this->output); |
|
82
|
|
|
|
|
83
|
|
|
$table->setHeaderTitle('Routes') |
|
84
|
|
|
->setHeaders(['MODULE', 'METHOD', 'URI', 'ACTION', 'MIDDLEWARE']) |
|
85
|
|
|
->setRows($rows) |
|
86
|
|
|
->render(); |
|
87
|
|
|
} catch (ModuleException|RouteException $e) { |
|
88
|
|
|
$this->error($e->getMessage()); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Composes a table row |
|
94
|
|
|
* @param array $route |
|
95
|
|
|
* @param int $maxContentLength |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
private function composeTableRow(array $route, int $maxContentLength = 25): array |
|
99
|
|
|
{ |
|
100
|
|
|
$action = $route['action'] |
|
101
|
|
|
. '\\' |
|
102
|
|
|
. $route['controller'] |
|
103
|
|
|
. '@' |
|
104
|
|
|
. $route['action']; |
|
105
|
|
|
|
|
106
|
|
|
if (mb_strlen($action) > $maxContentLength) { |
|
107
|
|
|
$action = mb_substr($action, 0, $maxContentLength) . '...'; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$middlewares = isset($route['middlewares']) |
|
111
|
|
|
? implode(',', $route['middlewares']) |
|
112
|
|
|
: '-'; |
|
113
|
|
|
|
|
114
|
|
|
return [ |
|
115
|
|
|
$route['module'] ?? '', |
|
116
|
|
|
$route['method'] ?? '', |
|
117
|
|
|
$route['route'] ?? '', |
|
118
|
|
|
$action, |
|
119
|
|
|
$middlewares, |
|
120
|
|
|
]; |
|
121
|
|
|
} |
|
122
|
|
|
} |