1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Support; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionMethod; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Stolen from Laravel 5.2. |
11
|
|
|
*/ |
12
|
|
|
class ControllerInspector |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* An array of HTTP verbs. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $verbs = [ |
20
|
|
|
'any', 'get', 'post', 'put', 'patch', |
21
|
|
|
'delete', 'head', 'options', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get the routable methods for a controller. |
26
|
|
|
* |
27
|
|
|
* @param string $controller |
28
|
|
|
* @param string $prefix |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function getRoutable($controller, $prefix) |
32
|
|
|
{ |
33
|
|
|
$routable = []; |
34
|
|
|
|
35
|
|
|
$reflection = new ReflectionClass($controller); |
36
|
|
|
|
37
|
|
|
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC); |
38
|
|
|
|
39
|
|
|
// To get the routable methods, we will simply spin through all methods on the |
40
|
|
|
// controller instance checking to see if it belongs to the given class and |
41
|
|
|
// is a publicly routable method. If so, we will add it to this listings. |
42
|
|
|
foreach ($methods as $method) { |
43
|
|
|
if ($this->isRoutable($method)) { |
44
|
|
|
$data = $this->getMethodData($method, $prefix); |
45
|
|
|
|
46
|
|
|
$routable[$method->name][] = $data; |
47
|
|
|
|
48
|
|
|
// If the routable method is an index method, we will create a special index |
49
|
|
|
// route which is simply the prefix and the verb and does not contain any |
50
|
|
|
// the wildcard place-holders that each "typical" routes would contain. |
51
|
|
|
if ($data['plain'] == $prefix.'/index') { |
52
|
|
|
$routable[$method->name][] = $this->getIndexData($data, $prefix); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $routable; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Determine if the given controller method is routable. |
62
|
|
|
* |
63
|
|
|
* @param \ReflectionMethod $method |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function isRoutable(ReflectionMethod $method) |
67
|
|
|
{ |
68
|
|
|
if ($method->class == 'Illuminate\Routing\Controller') { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return Str::startsWith($method->name, $this->verbs); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the method data for a given method. |
77
|
|
|
* |
78
|
|
|
* @param \ReflectionMethod $method |
79
|
|
|
* @param string $prefix |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function getMethodData(ReflectionMethod $method, $prefix) |
83
|
|
|
{ |
84
|
|
|
$verb = $this->getVerb($name = $method->name); |
85
|
|
|
|
86
|
|
|
$uri = $this->addUriWildcards($plain = $this->getPlainUri($name, $prefix)); |
87
|
|
|
|
88
|
|
|
return compact('verb', 'plain', 'uri'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the routable data for an index method. |
93
|
|
|
* |
94
|
|
|
* @param array $data |
95
|
|
|
* @param string $prefix |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
protected function getIndexData($data, $prefix) |
99
|
|
|
{ |
100
|
|
|
return ['verb' => $data['verb'], 'plain' => $prefix, 'uri' => $prefix]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Extract the verb from a controller action. |
105
|
|
|
* |
106
|
|
|
* @param string $name |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getVerb($name) |
110
|
|
|
{ |
111
|
|
|
return head(explode('_', Str::snake($name))); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Determine the URI from the given method name. |
116
|
|
|
* |
117
|
|
|
* @param string $name |
118
|
|
|
* @param string $prefix |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getPlainUri($name, $prefix) |
122
|
|
|
{ |
123
|
|
|
return $prefix.'/'.implode('-', array_slice(explode('_', Str::snake($name)), 1)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Add wildcards to the given URI. |
128
|
|
|
* |
129
|
|
|
* @param string $uri |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function addUriWildcards($uri) |
133
|
|
|
{ |
134
|
|
|
return $uri.'/{one?}/{two?}/{three?}/{four?}/{five?}'; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|