1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Kernel\Http; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Router |
20
|
|
|
* @package O2System\Kernel\Http |
21
|
|
|
*/ |
22
|
|
|
class Router |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Router::$uri |
26
|
|
|
* |
27
|
|
|
* @var Router\Uri |
|
|
|
|
28
|
|
|
*/ |
29
|
|
|
protected $uri; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Router::$addresses |
33
|
|
|
* |
34
|
|
|
* @var Router\Addresses |
35
|
|
|
*/ |
36
|
|
|
protected $addresses; |
37
|
|
|
|
38
|
|
|
// ------------------------------------------------------------------------ |
39
|
|
|
|
40
|
|
|
public function getUri() |
41
|
|
|
{ |
42
|
|
|
return $this->uri; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function setAddresses(Router\Addresses $addresses) |
46
|
|
|
{ |
47
|
|
|
$this->addresses = $addresses; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getAddresses() |
53
|
|
|
{ |
54
|
|
|
return $this->addresses; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// ------------------------------------------------------------------------ |
58
|
|
|
|
59
|
|
|
public function parseRequest(Message\Uri $uri = null) |
60
|
|
|
{ |
61
|
|
|
$this->uri = is_null($uri) ? server_request()->getUri() : $uri; |
|
|
|
|
62
|
|
|
$uriSegments = $this->uri->getSegments()->getParts(); |
63
|
|
|
$uriString = $this->uri->getSegments()->getString(); |
64
|
|
|
|
65
|
|
|
if($this->uri->getSegments()->getTotalParts()) { |
66
|
|
|
if(strpos(end($uriSegments), '.json') !== false) { |
67
|
|
|
output()->setContentType('application/json'); |
|
|
|
|
68
|
|
|
$endSegment = str_replace('.json', '', end($uriSegments)); |
69
|
|
|
array_pop($uriSegments); |
70
|
|
|
array_push($uriSegments, $endSegment); |
71
|
|
|
$this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
72
|
|
|
$uriString = $this->uri->getSegments()->getString(); |
73
|
|
|
} elseif(strpos(end($uriSegments), '.xml') !== false) { |
74
|
|
|
output()->setContentType('application/xml'); |
75
|
|
|
$endSegment = str_replace('.xml', '', end($uriSegments)); |
76
|
|
|
array_pop($uriSegments); |
77
|
|
|
array_push($uriSegments, $endSegment); |
78
|
|
|
$this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
79
|
|
|
$uriString = $this->uri->getSegments()->getString(); |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
|
|
$uriPath = urldecode( |
83
|
|
|
parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH) |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$uriPathParts = explode('public/', $uriPath); |
87
|
|
|
$uriPath = end($uriPathParts); |
88
|
|
|
|
89
|
|
|
if ($uriPath !== '/') { |
90
|
|
|
$uriString = $uriPath; |
91
|
|
|
$uriSegments = array_filter(explode('/', $uriString)); |
92
|
|
|
|
93
|
|
|
$this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
94
|
|
|
$uriString = $this->uri->getSegments()->getString(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($this->addresses instanceof Router\Addresses) { |
|
|
|
|
99
|
|
|
// Domain routing |
100
|
|
|
if (null !== ($domain = $this->addresses->getDomain())) { |
101
|
|
|
if (is_array($domain)) { |
102
|
|
|
$uriSegments = array_merge($domain, $uriSegments); |
103
|
|
|
$this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
104
|
|
|
$uriString = $this->uri->getSegments()->getString(); |
105
|
|
|
} |
106
|
|
|
} elseif (false !== ($subdomain = $this->uri->getSubdomain())) { |
107
|
|
|
if (is_array($subdomain)) { |
108
|
|
|
$uriSegments = array_merge($subdomain, $uriSegments); |
109
|
|
|
$this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
110
|
|
|
$uriString = $this->uri->getSegments()->getString(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Try to translate from uri string |
116
|
|
|
if (false !== ($action = $this->addresses->getTranslation($uriString))) { |
117
|
|
|
if ( ! $action->isValidHttpMethod(server_request()->getMethod()) && ! $action->isAnyHttpMethod()) { |
118
|
|
|
output()->sendError(405); |
119
|
|
|
} else { |
120
|
|
|
if (false !== ($parseSegments = $action->getParseUriString($uriString))) { |
121
|
|
|
$uriSegments = $parseSegments; |
122
|
|
|
} else { |
123
|
|
|
$uriSegments = []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->uri = $this->uri->withSegments(new Message\Uri\Segments($uriSegments)); |
127
|
|
|
$uriString = $this->uri->getSegments()->getString(); |
|
|
|
|
128
|
|
|
|
129
|
|
|
$this->parseAction($action, $uriSegments); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Let's the app do the rest when there is no controller found |
134
|
|
|
// the app should redirect to PAGE 404 |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// ------------------------------------------------------------------------ |
138
|
|
|
|
139
|
|
|
protected function parseAction(Router\Datastructures\Action $action, array $uriSegments = []) |
140
|
|
|
{ |
141
|
|
|
$closure = $action->getClosure(); |
142
|
|
|
if (empty($closure)) { |
143
|
|
|
output()->sendError(204); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($closure instanceof Controller) { |
147
|
|
|
$uriSegments = empty($uriSegments) |
148
|
|
|
? $action->getClosureParameters() |
149
|
|
|
: $uriSegments; |
150
|
|
|
$this->setController( |
151
|
|
|
(new Router\Datastructures\Controller($closure)) |
152
|
|
|
->setRequestMethod('index'), |
153
|
|
|
$uriSegments |
154
|
|
|
); |
155
|
|
|
} elseif ($closure instanceof Router\Datastructures\Controller) { |
156
|
|
|
$this->setController($closure, $action->getClosureParameters()); |
157
|
|
|
} elseif (is_array($closure)) { |
158
|
|
|
$uri = (new Message\Uri()) |
|
|
|
|
159
|
|
|
->withSegments(new Message\Uri\Segments('')) |
160
|
|
|
->withQuery(''); |
161
|
|
|
$this->parseRequest($this->uri->addSegments($closure)); |
162
|
|
|
} else { |
163
|
|
|
if (class_exists($closure)) { |
164
|
|
|
$this->setController( |
165
|
|
|
(new Router\Datastructures\Controller($closure)) |
166
|
|
|
->setRequestMethod('index'), |
167
|
|
|
$uriSegments |
168
|
|
|
); |
169
|
|
|
} elseif (preg_match("/([a-zA-Z0-9\\\]+)(@)([a-zA-Z0-9\\\]+)/", $closure, $matches)) { |
170
|
|
|
$this->setController( |
171
|
|
|
(new Router\Datastructures\Controller($matches[ 1 ])) |
172
|
|
|
->setRequestMethod($matches[ 3 ]), |
173
|
|
|
$uriSegments |
174
|
|
|
); |
175
|
|
|
} elseif (is_string($closure) && $closure !== '') { |
176
|
|
|
if (is_json($closure)) { |
177
|
|
|
output()->setContentType('application/json'); |
178
|
|
|
output()->send($closure); |
|
|
|
|
179
|
|
|
} else { |
180
|
|
|
output()->send($closure); |
181
|
|
|
} |
182
|
|
|
} elseif (is_array($closure) || is_object($closure)) { |
183
|
|
|
output()->send($closure); |
184
|
|
|
} elseif (is_numeric($closure)) { |
185
|
|
|
output()->sendError($closure); |
186
|
|
|
} else { |
187
|
|
|
output()->sendError(204); |
188
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// ------------------------------------------------------------------------ |
194
|
|
|
|
195
|
|
|
protected function setController( |
196
|
|
|
Router\Datastructures\Controller $controller, |
197
|
|
|
array $uriSegments = [] |
198
|
|
|
) { |
199
|
|
|
if ( ! $controller->isValid()) { |
200
|
|
|
output()->sendError(400); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// Add Controller PSR4 Namespace |
204
|
|
|
loader()->addNamespace($controller->getNamespaceName(), $controller->getFileInfo()->getPath()); |
|
|
|
|
205
|
|
|
|
206
|
|
|
$controllerMethod = $controller->getRequestMethod(); |
207
|
|
|
$controllerMethod = empty($controllerMethod) ? reset($uriSegments) : $controllerMethod; |
208
|
|
|
$controllerMethod = camelcase($controllerMethod); |
209
|
|
|
|
210
|
|
|
// Set default controller method to index |
211
|
|
|
if ( ! $controller->hasMethod($controllerMethod) && |
212
|
|
|
! $controller->hasMethod('route') |
213
|
|
|
) { |
214
|
|
|
$controllerMethod = 'index'; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// has route method, controller method set to index as default |
218
|
|
|
if (empty($controllerMethod)) { |
219
|
|
|
$controllerMethod = 'index'; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
if (camelcase(reset($uriSegments)) === $controllerMethod) { |
223
|
|
|
array_shift($uriSegments); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$controllerMethodParams = $uriSegments; |
227
|
|
|
|
228
|
|
|
if ($controller->hasMethod('route')) { |
229
|
|
|
$controller->setRequestMethod('route'); |
230
|
|
|
$controller->setRequestMethodArgs([ |
231
|
|
|
$controllerMethod, |
232
|
|
|
$controllerMethodParams, |
233
|
|
|
]); |
234
|
|
|
} elseif ($controller->hasMethod($controllerMethod)) { |
235
|
|
|
$method = $controller->getMethod($controllerMethod); |
236
|
|
|
|
237
|
|
|
// Method doesn't need any parameters |
238
|
|
|
if ($method->getNumberOfParameters() == 0) { |
239
|
|
|
// But there is parameters requested |
240
|
|
|
if (count($controllerMethodParams)) { |
241
|
|
|
output()->sendError(404); |
242
|
|
|
} else { |
243
|
|
|
$controller->setRequestMethod($controllerMethod); |
244
|
|
|
} |
245
|
|
|
} else { |
246
|
|
|
$parameters = []; |
247
|
|
|
|
248
|
|
|
if (count($controllerMethodParams)) { |
249
|
|
|
if (is_numeric(key($controllerMethodParams))) { |
250
|
|
|
$parameters = $controllerMethodParams; |
251
|
|
|
} else { |
252
|
|
|
foreach ($method->getParameters() as $index => $parameter) { |
253
|
|
|
if (isset($uriSegments[ $parameter->name ])) { |
254
|
|
|
$parameters[ $index ] = $controllerMethodParams[ $parameter->name ]; |
255
|
|
|
} else { |
256
|
|
|
$parameters[ $index ] = null; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$controller->setRequestMethod($controllerMethod); |
263
|
|
|
$controller->setRequestMethodArgs($parameters); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// Set Controller |
268
|
|
|
services()->add($controller, 'controller'); |
269
|
|
|
} |
270
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths