|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Adelowo\Cfar; |
|
4
|
|
|
|
|
5
|
|
|
use Aura\Router\Route; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* CFAR -- Controller for Aura.Router |
|
9
|
|
|
* @author Lanre Adelowo <[email protected]> |
|
10
|
|
|
* Allows you to use controllers and methods (laravel/symfony style) for `Aura.Router` 3.x. |
|
11
|
|
|
* If you still make use of `Aura.Router` 2.x, please see the <= 0.2.* releases |
|
12
|
|
|
*/ |
|
13
|
|
|
class Cfar |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* This is here to bail the current request if a "listener" was not defined on the class. |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
const CFAR_DEFAULT_METHOD = 'indexAction'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Aura.Router Instance |
|
24
|
|
|
* @var Route |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $matchedRoute; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The name of the called controller class (plus it's fully quantified namespace). |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $controller; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The name of the method invoked on the called class . |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $method; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The parameters to be passed unto the method. |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $parameters; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param Route $router |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public function __construct(Route $router) |
|
50
|
|
|
{ |
|
51
|
3 |
|
$this->matchedRoute = $router; |
|
52
|
3 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns an array of parameters for the matched route. |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function getParameters() |
|
59
|
|
|
{ |
|
60
|
1 |
|
return $this->parameters; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns a string that denotes the called class plus it's namespace |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function getController() |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->controller; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns a string that denotes the method that was invoked. |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function getMethod() |
|
77
|
|
|
{ |
|
78
|
2 |
|
return $this->method; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Calls the controller associated with the route and invoke the specified method. |
|
83
|
|
|
* @throws \Adelowo\Cfar\CfarException if the class cannot be be found |
|
84
|
|
|
*/ |
|
85
|
3 |
|
public function dispatch() |
|
86
|
|
|
{ |
|
87
|
|
|
try { |
|
88
|
|
|
|
|
89
|
3 |
|
$this->doesRouteHaveAValidDeclaration(); |
|
90
|
|
|
|
|
91
|
2 |
|
list($this->controller, $this->method) = $this->getRegisteredControllerAndMethod(); |
|
92
|
|
|
|
|
93
|
2 |
|
$this->parameters = $this->matchedRoute->attributes; |
|
94
|
|
|
|
|
95
|
2 |
|
$this->getReflectionClass($this->controller) |
|
96
|
2 |
|
->getMethod($this->method) |
|
97
|
2 |
|
->invokeArgs(new $this->controller, $this->parameters); |
|
98
|
|
|
|
|
99
|
3 |
|
} catch (\ReflectionException $e) { |
|
100
|
1 |
|
throw new CfarException($e->getMessage()); |
|
101
|
|
|
} |
|
102
|
2 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
2 |
|
protected function getRegisteredControllerAndMethod() |
|
108
|
|
|
{ |
|
109
|
|
|
return [ |
|
110
|
2 |
|
$this->matchedRoute->handler, |
|
111
|
2 |
|
array_key_exists('listener', |
|
112
|
2 |
|
$this->matchedRoute->extras) ? $this->matchedRoute->extras['listener'] : self::CFAR_DEFAULT_METHOD |
|
113
|
2 |
|
]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return \ReflectionClass |
|
118
|
|
|
*/ |
|
119
|
3 |
|
protected function doesRouteHaveAValidDeclaration() |
|
120
|
|
|
{ |
|
121
|
3 |
|
return $this->getReflectionClass($this->matchedRoute->handler); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param $class string|object The class to resolve |
|
127
|
|
|
* @return \ReflectionClass |
|
128
|
|
|
*/ |
|
129
|
3 |
|
protected function getReflectionClass($class) |
|
130
|
|
|
{ |
|
131
|
3 |
|
return new \ReflectionClass($class); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|