1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: mfrancois |
5
|
|
|
* Date: 17/09/2016 |
6
|
|
|
* Time: 16:46 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Distilleries\Expendable\Http\Router; |
10
|
|
|
|
11
|
|
|
use Illuminate\Support\Arr; |
12
|
|
|
|
13
|
|
|
class Router extends \Illuminate\Routing\Router |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Register an array of controllers with wildcard routing. |
19
|
|
|
* |
20
|
|
|
* @param array $controllers |
21
|
|
|
* @return void |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
public function controllers(array $controllers) |
25
|
|
|
{ |
26
|
|
|
foreach ($controllers as $uri => $controller) { |
27
|
|
|
$this->controller($uri, $controller); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
/** |
31
|
|
|
* Route a controller to a URI with wildcard routing. |
32
|
|
|
* |
33
|
|
|
* @param string $uri |
34
|
|
|
* @param string $controller |
35
|
|
|
* @param array $names |
36
|
|
|
* @return void |
37
|
|
|
* |
38
|
|
|
*/ |
39
|
|
|
public function controller($uri, $controller, $names = []) |
40
|
|
|
{ |
41
|
|
|
$prepended = $controller; |
42
|
|
|
// First, we will check to see if a controller prefix has been registered in |
43
|
|
|
// the route group. If it has, we will need to prefix it before trying to |
44
|
|
|
// reflect into the class instance and pull out the method for routing. |
45
|
|
|
if (! empty($this->groupStack)) { |
46
|
|
|
$prepended = $this->prependGroupUses($controller); |
47
|
|
|
} |
48
|
|
|
$routable = (new ControllerInspector) |
49
|
|
|
->getRoutable($prepended, $uri); |
50
|
|
|
// When a controller is routed using this method, we use Reflection to parse |
51
|
|
|
// out all of the routable methods for the controller, then register each |
52
|
|
|
// route explicitly for the developers, so reverse routing is possible. |
53
|
|
|
foreach ($routable as $method => $routes) { |
54
|
|
|
foreach ($routes as $route) { |
55
|
|
|
$this->registerInspected($route, $controller, $method, $names); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
$this->addFallthroughRoute($controller, $uri); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Prepend the last group uses onto the use clause. |
63
|
|
|
* |
64
|
|
|
* @param string $uses |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
protected function prependGroupUses($uses) |
68
|
|
|
{ |
69
|
|
|
$group = last($this->groupStack); |
70
|
|
|
|
71
|
|
|
return isset($group['namespace']) ? $group['namespace'].'\\'.$uses : $uses; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Register an inspected controller route. |
77
|
|
|
* |
78
|
|
|
* @param array $route |
79
|
|
|
* @param string $controller |
80
|
|
|
* @param string $method |
81
|
|
|
* @param array $names |
82
|
|
|
* @return void |
83
|
|
|
* |
84
|
|
|
*/ |
85
|
|
|
protected function registerInspected($route, $controller, $method, &$names) |
86
|
|
|
{ |
87
|
|
|
$action = ['uses' => $controller.'@'.$method]; |
88
|
|
|
// If a given controller method has been named, we will assign the name to the |
89
|
|
|
// controller action array, which provides for a short-cut to method naming |
90
|
|
|
// so you don't have to define an individual route for these controllers. |
91
|
|
|
$action['as'] = Arr::get($names, $method); |
92
|
|
|
$this->{$route['verb']}($route['uri'], $action); |
93
|
|
|
} |
94
|
|
|
/** |
95
|
|
|
* Add a fallthrough route for a controller. |
96
|
|
|
* |
97
|
|
|
* @param string $controller |
98
|
|
|
* @param string $uri |
99
|
|
|
* @return void |
100
|
|
|
* |
101
|
|
|
*/ |
102
|
|
|
protected function addFallthroughRoute($controller, $uri) |
103
|
|
|
{ |
104
|
|
|
$missing = $this->any($uri.'/{_missing}', $controller.'@missingMethod'); |
105
|
|
|
$missing->where('_missing', '(.*)'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |