|
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
|
|
|
* Register an inspected controller route. |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $route |
|
64
|
|
|
* @param string $controller |
|
65
|
|
|
* @param string $method |
|
66
|
|
|
* @param array $names |
|
67
|
|
|
* @return void |
|
68
|
|
|
* |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function registerInspected($route, $controller, $method, &$names) |
|
71
|
|
|
{ |
|
72
|
|
|
$action = ['uses' => $controller.'@'.$method]; |
|
73
|
|
|
// If a given controller method has been named, we will assign the name to the |
|
74
|
|
|
// controller action array, which provides for a short-cut to method naming |
|
75
|
|
|
// so you don't have to define an individual route for these controllers. |
|
76
|
|
|
$action['as'] = Arr::get($names, $method); |
|
77
|
|
|
$this->{$route['verb']}($route['uri'], $action); |
|
78
|
|
|
} |
|
79
|
|
|
/** |
|
80
|
|
|
* Add a fallthrough route for a controller. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $controller |
|
83
|
|
|
* @param string $uri |
|
84
|
|
|
* @return void |
|
85
|
|
|
* |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function addFallthroughRoute($controller, $uri) |
|
88
|
|
|
{ |
|
89
|
|
|
$missing = $this->any($uri.'/{_missing}', $controller.'@missingMethod'); |
|
90
|
|
|
$missing->where('_missing', '(.*)'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
} |