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