|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class ListRouteCommand extends SilverstripeCommand |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* @var string |
|
7
|
|
|
*/ |
|
8
|
|
|
protected $signature = 'list:route'; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $description = 'List all routes to Controllers'; |
|
14
|
|
|
|
|
15
|
|
|
public function fire() |
|
16
|
|
|
{ |
|
17
|
|
|
$headers = ['Route', 'Controller', 'Allowed Actions']; |
|
18
|
|
|
|
|
19
|
|
|
$this->table($headers, $this->routes()); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function routes() |
|
23
|
|
|
{ |
|
24
|
|
|
$routes = Config::inst()->get('Director', 'rules'); |
|
25
|
|
|
|
|
26
|
|
|
$list = []; |
|
27
|
|
|
|
|
28
|
|
|
foreach ($routes as $route => $controller) { |
|
|
|
|
|
|
29
|
|
|
$handlers = $this->getUrlHandlersForController($controller); |
|
30
|
|
|
|
|
31
|
|
|
$actions = $handlers ? $handlers : $this->getActionsForController($controller); |
|
32
|
|
|
|
|
33
|
|
|
$list[$route] = [$route, $controller, implode("\n", $actions)]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
ksort($list); |
|
37
|
|
|
|
|
38
|
|
|
return $list; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $controller |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function getActionsForController($controller) |
|
47
|
|
|
{ |
|
48
|
|
|
$actions = (array) $this->getValuesOrKeysFromConfig($controller, 'allowed_actions'); |
|
49
|
|
|
|
|
50
|
|
|
foreach ($actions as $key => $action) { |
|
51
|
|
|
if ($action == 'index') { |
|
52
|
|
|
unset($actions[$key]); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $actions; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $controller |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function getUrlHandlersForController($controller) |
|
65
|
|
|
{ |
|
66
|
|
|
$handlers = (array) $this->getValuesOrKeysFromConfig($controller, 'url_handlers'); |
|
67
|
|
|
|
|
68
|
|
|
foreach ($handlers as $key => $handler) { |
|
69
|
|
|
if ($handler == '') { |
|
70
|
|
|
unset($handlers[$key]); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $handlers; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $controller |
|
79
|
|
|
* @param string $config |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getValuesOrKeysFromConfig($controller, $config = 'allowed_actions') |
|
84
|
|
|
{ |
|
85
|
|
|
$values = (array) Config::inst()->get($controller, $config, Config::UNINHERITED); |
|
86
|
|
|
|
|
87
|
|
|
if (!isset($values[0])) { // assoc with permissions set as values |
|
88
|
|
|
$values = array_keys($values); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $values; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.