|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Dispatcher;
|
|
4
|
|
|
|
|
5
|
|
|
use Exception;
|
|
6
|
|
|
use Nip\AutoLoader\AutoLoader;
|
|
7
|
|
|
use Nip\Controller;
|
|
8
|
|
|
use Nip\Http\Response\Response;
|
|
9
|
|
|
use Nip\Request;
|
|
10
|
|
|
|
|
11
|
|
|
/**
|
|
12
|
|
|
* Class Dispatcher
|
|
13
|
|
|
* @package Nip\Dispatcher
|
|
14
|
|
|
*/
|
|
15
|
|
|
class Dispatcher
|
|
16
|
|
|
{
|
|
17
|
|
|
|
|
18
|
|
|
/**
|
|
19
|
|
|
* @var null|Request
|
|
20
|
|
|
*/
|
|
21
|
|
|
protected $request = null;
|
|
22
|
|
|
|
|
23
|
|
|
protected $currentController = false;
|
|
24
|
|
|
|
|
25
|
|
|
protected $hops = 0;
|
|
26
|
|
|
|
|
27
|
|
|
protected $maxHops = 30;
|
|
28
|
|
|
|
|
29
|
|
|
/**
|
|
30
|
|
|
* @param $controller
|
|
31
|
|
|
* @return mixed
|
|
32
|
|
|
*/
|
|
33
|
1 |
|
public static function reverseControllerName($controller)
|
|
34
|
|
|
{
|
|
35
|
1 |
|
return inflector()->unclassify($controller);
|
|
36
|
|
|
}
|
|
37
|
|
|
|
|
38
|
|
|
/**
|
|
39
|
|
|
* @param $name
|
|
40
|
|
|
* @return mixed
|
|
41
|
|
|
*/
|
|
42
|
|
|
public static function formatActionName($name)
|
|
43
|
|
|
{
|
|
44
|
|
|
$name = inflector()->camelize($name);
|
|
45
|
|
|
$name[0] = strtolower($name[0]);
|
|
46
|
|
|
|
|
47
|
|
|
return $name;
|
|
48
|
|
|
}
|
|
49
|
|
|
|
|
50
|
|
|
/**
|
|
51
|
|
|
* @param Request|null $request
|
|
52
|
|
|
* @return null|Response
|
|
53
|
|
|
* @throws Exception
|
|
54
|
|
|
*/
|
|
55
|
|
|
public function dispatch(Request $request = null)
|
|
56
|
|
|
{
|
|
57
|
|
|
if ($request) {
|
|
58
|
|
|
$this->setRequest($request);
|
|
59
|
|
|
} else {
|
|
60
|
|
|
$request = $this->getRequest();
|
|
61
|
|
|
}
|
|
62
|
|
|
$this->hops++;
|
|
63
|
|
|
|
|
64
|
|
|
if ($this->hops <= $this->maxHops) {
|
|
65
|
|
|
if ($request->getControllerName() == null) {
|
|
66
|
|
|
throw new Exception('No valid controller name in request ['.$request->getMCA().']');
|
|
|
|
|
|
|
67
|
|
|
}
|
|
68
|
|
|
|
|
69
|
|
|
$controller = $this->generateController($request);
|
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
if ($controller instanceof Controller) {
|
|
72
|
|
|
try {
|
|
73
|
|
|
$this->currentController = $controller;
|
|
|
|
|
|
|
74
|
|
|
$controller->setRequest($request);
|
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
return $controller->dispatch();
|
|
77
|
|
|
} catch (ForwardException $e) {
|
|
78
|
|
|
$return = $this->dispatch();
|
|
79
|
|
|
|
|
80
|
|
|
return $return;
|
|
81
|
|
|
}
|
|
82
|
|
|
} else {
|
|
83
|
|
|
throw new Exception('Error finding a valid controller for ['.$request->getMCA().']');
|
|
84
|
|
|
}
|
|
85
|
|
|
} else {
|
|
86
|
|
|
throw new Exception("Maximum number of hops ($this->maxHops) has been reached for {$request->getMCA()}");
|
|
87
|
|
|
}
|
|
88
|
|
|
}
|
|
89
|
|
|
|
|
90
|
|
|
/**
|
|
91
|
|
|
* @return Request
|
|
92
|
|
|
*/
|
|
93
|
|
|
public function getRequest()
|
|
94
|
|
|
{
|
|
95
|
|
|
return $this->request;
|
|
96
|
|
|
}
|
|
97
|
|
|
|
|
98
|
|
|
/**
|
|
99
|
|
|
* @param Request|null $request
|
|
100
|
|
|
*/
|
|
101
|
|
|
public function setRequest($request)
|
|
102
|
|
|
{
|
|
103
|
|
|
$this->request = $request;
|
|
104
|
|
|
}
|
|
105
|
|
|
|
|
106
|
|
|
/**
|
|
107
|
|
|
* @param Request $request
|
|
108
|
|
|
* @return Controller|null
|
|
109
|
|
|
* @throws Exception
|
|
110
|
|
|
*/
|
|
111
|
|
|
public function generateController($request)
|
|
112
|
|
|
{
|
|
113
|
|
|
$module = $this->formatModuleName($request->getModuleName());
|
|
114
|
|
|
$controller = $this->formatControllerName($request->getControllerName());
|
|
115
|
|
|
|
|
116
|
|
|
$namespaceClass = $this->generateFullControllerNameNamespace($module, $controller);
|
|
117
|
|
|
if ($this->isValidControllerNamespace($namespaceClass)) {
|
|
118
|
|
|
return $this->newController($namespaceClass);
|
|
119
|
|
|
} else {
|
|
120
|
|
|
$classicClass = $this->generateFullControllerNameString($module, $controller);
|
|
121
|
|
|
if ($this->getAutoloader()->isClass($classicClass)) {
|
|
122
|
|
|
return $this->newController($classicClass);
|
|
123
|
|
|
}
|
|
124
|
|
|
}
|
|
125
|
|
|
throw new Exception(
|
|
126
|
|
|
'Error finding a valid controller ['.$namespaceClass.']['.$classicClass.'] for ['.$request->getMCA().']'
|
|
127
|
|
|
);
|
|
128
|
|
|
}
|
|
129
|
|
|
|
|
130
|
|
|
/**
|
|
131
|
|
|
* @param $name
|
|
132
|
|
|
* @return mixed
|
|
133
|
|
|
*/
|
|
134
|
|
|
public function formatModuleName($name)
|
|
135
|
|
|
{
|
|
136
|
|
|
$name = $name ? $name : 'default';
|
|
137
|
|
|
|
|
138
|
|
|
return inflector()->camelize($name);
|
|
139
|
|
|
}
|
|
140
|
|
|
|
|
141
|
|
|
/**
|
|
142
|
|
|
* @param $name
|
|
143
|
|
|
* @return mixed
|
|
144
|
|
|
*/
|
|
145
|
|
|
public function formatControllerName($name)
|
|
146
|
|
|
{
|
|
147
|
|
|
$name = $name ? $name : 'index';
|
|
148
|
|
|
|
|
149
|
|
|
return $this->getControllerName($name);
|
|
150
|
|
|
}
|
|
151
|
|
|
|
|
152
|
|
|
/**
|
|
153
|
|
|
* @param $controller
|
|
154
|
|
|
* @return mixed
|
|
155
|
|
|
*/
|
|
156
|
|
|
public function getControllerName($controller)
|
|
157
|
|
|
{
|
|
158
|
|
|
return inflector()->classify($controller);
|
|
159
|
|
|
}
|
|
160
|
|
|
|
|
161
|
|
|
/**
|
|
162
|
|
|
* @param $module
|
|
163
|
|
|
* @param $controller
|
|
164
|
|
|
* @return string
|
|
165
|
|
|
*/
|
|
166
|
|
|
protected function generateFullControllerNameNamespace($module, $controller)
|
|
167
|
|
|
{
|
|
168
|
|
|
$name = app('app')->getRootNamespace() . 'Modules\\';
|
|
169
|
|
|
$module = $module == 'Default' ? 'Frontend' : $module;
|
|
170
|
|
|
$name .= $module . '\Controllers\\';
|
|
171
|
|
|
$name .= str_replace('_', '\\', $controller) . "Controller";
|
|
172
|
|
|
|
|
173
|
|
|
return $name;
|
|
174
|
|
|
}
|
|
175
|
|
|
|
|
176
|
|
|
/**
|
|
177
|
|
|
* @param $namespaceClass
|
|
178
|
|
|
* @return bool
|
|
179
|
|
|
*/
|
|
180
|
|
|
protected function isValidControllerNamespace($namespaceClass)
|
|
181
|
|
|
{
|
|
182
|
|
|
return class_exists($namespaceClass);
|
|
183
|
|
|
// $loader = $this->getAutoloader()->getPsr4ClassLoader();
|
|
|
|
|
|
|
184
|
|
|
// $loader->load($namespaceClass);
|
|
185
|
|
|
// if ($loader->isLoaded($namespaceClass)) {
|
|
186
|
|
|
// return true;
|
|
187
|
|
|
// }
|
|
188
|
|
|
//
|
|
189
|
|
|
// return false;
|
|
190
|
|
|
}
|
|
191
|
|
|
|
|
192
|
|
|
/**
|
|
193
|
|
|
* @param $class
|
|
194
|
|
|
* @return Controller
|
|
195
|
|
|
*/
|
|
196
|
|
|
public function newController($class)
|
|
197
|
|
|
{
|
|
198
|
|
|
$controller = new $class();
|
|
199
|
|
|
/** @var Controller $controller */
|
|
200
|
|
|
$controller->setDispatcher($this);
|
|
201
|
|
|
|
|
202
|
|
|
return $controller;
|
|
203
|
|
|
}
|
|
204
|
|
|
|
|
205
|
|
|
/**
|
|
206
|
|
|
* @param $module
|
|
207
|
|
|
* @param $controller
|
|
208
|
|
|
* @return string
|
|
209
|
|
|
*/
|
|
210
|
|
|
protected function generateFullControllerNameString($module, $controller)
|
|
211
|
|
|
{
|
|
212
|
|
|
return $module . "_" . $controller . "Controller";
|
|
213
|
|
|
}
|
|
214
|
|
|
|
|
215
|
|
|
/**
|
|
216
|
|
|
* @return AutoLoader
|
|
217
|
|
|
*/
|
|
218
|
|
|
protected function getAutoloader()
|
|
219
|
|
|
{
|
|
220
|
|
|
return app('autoloader');
|
|
221
|
|
|
}
|
|
222
|
|
|
|
|
223
|
|
|
/**
|
|
224
|
|
|
* @param bool $params
|
|
225
|
|
|
*/
|
|
226
|
|
|
public function throwError($params = false)
|
|
|
|
|
|
|
227
|
|
|
{
|
|
228
|
|
|
// $this->getFrontController()->getTrace()->add($params);
|
|
|
|
|
|
|
229
|
|
|
$this->setErrorController();
|
|
230
|
|
|
$this->forward('index');
|
|
|
|
|
|
|
231
|
|
|
|
|
232
|
|
|
return;
|
|
233
|
|
|
}
|
|
234
|
|
|
|
|
235
|
|
|
/**
|
|
236
|
|
|
* @return $this
|
|
237
|
|
|
*/
|
|
238
|
|
|
public function setErrorController()
|
|
239
|
|
|
{
|
|
240
|
|
|
$this->getRequest()->setActionName('index');
|
|
241
|
|
|
$this->getRequest()->setControllerName('error');
|
|
242
|
|
|
$this->getRequest()->setModuleName('default');
|
|
243
|
|
|
|
|
244
|
|
|
return $this;
|
|
245
|
|
|
}
|
|
246
|
|
|
|
|
247
|
|
|
/**
|
|
248
|
|
|
* @param bool $action
|
|
249
|
|
|
* @param bool $controller
|
|
250
|
|
|
* @param bool $module
|
|
251
|
|
|
* @param array $params
|
|
252
|
|
|
* @throws ForwardException
|
|
253
|
|
|
*/
|
|
254
|
|
|
public function forward($action = false, $controller = false, $module = false, $params = [])
|
|
255
|
|
|
{
|
|
256
|
|
|
$this->getRequest()->setActionName($action);
|
|
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
if ($controller) {
|
|
259
|
|
|
$this->getRequest()->setControllerName($controller);
|
|
|
|
|
|
|
260
|
|
|
}
|
|
261
|
|
|
if ($module) {
|
|
262
|
|
|
$this->getRequest()->setModuleName($module);
|
|
|
|
|
|
|
263
|
|
|
}
|
|
264
|
|
|
|
|
265
|
|
|
if (is_array($params)) {
|
|
266
|
|
|
$this->getRequest()->attributes->add($params);
|
|
267
|
|
|
}
|
|
268
|
|
|
|
|
269
|
|
|
throw new ForwardException;
|
|
270
|
|
|
}
|
|
271
|
|
|
|
|
272
|
|
|
/**
|
|
273
|
|
|
* @return bool
|
|
274
|
|
|
*/
|
|
275
|
|
|
public function getCurrentController()
|
|
276
|
|
|
{
|
|
277
|
|
|
return $this->currentController;
|
|
278
|
|
|
}
|
|
279
|
|
|
}
|
|
280
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: