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