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