Total Complexity | 86 |
Total Lines | 499 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 4 | Features | 0 |
Complex classes like Router often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class Router extends KernelRouter |
||
33 | { |
||
34 | /** |
||
35 | * Router::handle |
||
36 | * |
||
37 | * @param KernelMessageUri|null $uri |
||
38 | * |
||
39 | * @return bool |
||
40 | * @throws \O2System\Spl\Exceptions\RuntimeException |
||
41 | * @throws \ReflectionException |
||
42 | */ |
||
43 | public function handle(KernelMessageUri $uri = null) |
||
44 | { |
||
45 | $this->uri = is_null($uri) ? new KernelMessageUri() : $uri; |
||
46 | |||
47 | // Handle Extension Request |
||
48 | if ($this->uri->segments->count()) { |
||
|
|||
49 | $this->handleExtensionRequest(); |
||
50 | } else { |
||
51 | $uriPath = urldecode( |
||
52 | parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH) |
||
53 | ); |
||
54 | |||
55 | $uriPathParts = explode('public/', $uriPath); |
||
56 | $uriPath = end($uriPathParts); |
||
57 | |||
58 | if ($uriPath !== '/') { |
||
59 | $uriString = $uriPath; // tobe removed |
||
60 | $uriSegments = array_filter(explode('/', $uriString)); // tobe removed |
||
61 | |||
62 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments( |
||
63 | array_filter(explode('/', $uriString))) |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | unset($uriPathParts, $uriPath); |
||
68 | } |
||
69 | |||
70 | // Load app addresses config |
||
71 | $this->addresses = config()->loadFile('addresses', true); |
||
72 | |||
73 | if ($this->addresses instanceof KernelAddresses) { |
||
74 | // Domain routing |
||
75 | if (null !== ($domain = $this->addresses->getDomain())) { |
||
76 | if (is_array($domain)) { |
||
77 | $this->uri->segments->exchangeArray( |
||
78 | array_merge($domain, $this->uri->segments->getArrayCopy()) |
||
79 | ); |
||
80 | $domain = $this->uri->segments->first(); |
||
81 | } |
||
82 | |||
83 | if (false !== ($app = modules()->getApp($domain))) { |
||
84 | $this->registerModule($app); |
||
85 | } elseif (false !== ($module = modules()->getModule($domain))) { |
||
86 | $this->registerModule($module); |
||
87 | } |
||
88 | } elseif (false !== ($subdomain = $this->uri->getSubdomain())) { |
||
89 | if (false !== ($app = modules()->getApp($subdomain))) { |
||
90 | $this->registerModule($app); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | // App and Module routing |
||
96 | if ($numOfUriSegments = $this->uri->segments->count()) { |
||
97 | if (empty($app)) { |
||
98 | if (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
||
99 | $this->registerModule($module); |
||
100 | |||
101 | if ($module->getType() === 'APP') { |
||
102 | $this->uri->segments->shift(); |
||
103 | $this->handleAppRequest($module); |
||
104 | } else { |
||
105 | $this->handleSegmentsRequest(); |
||
106 | } |
||
107 | } |
||
108 | } else { |
||
109 | $this->handleAppRequest($app); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | // Try to translate from uri string |
||
114 | if (false !== ($action = $this->addresses->getTranslation($this->uri->segments->__toString()))) { |
||
115 | if ( ! $action->isValidHttpMethod(input()->server('REQUEST_METHOD')) && ! $action->isAnyHttpMethod()) { |
||
116 | output()->sendError(405); |
||
117 | } else { |
||
118 | // Checks if action closure is an array |
||
119 | if (is_array($closureSegments = $action->getClosure())) { |
||
120 | $this->uri->segments->exchangeArray($closureSegments); |
||
121 | |||
122 | if (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
||
123 | $this->registerModule($module); |
||
124 | |||
125 | if ($module->getType() === 'APP') { |
||
126 | $this->uri->segments->shift(); |
||
127 | $this->handleAppRequest($module); |
||
128 | } else { |
||
129 | $this->handleSegmentsRequest(); |
||
130 | } |
||
131 | } else { |
||
132 | $this->handleSegmentsRequest(); |
||
133 | } |
||
134 | } else { |
||
135 | if (false !== ($parseSegments = $action->getParseUriString($this->uri->segments->__toString()))) { |
||
136 | $uriSegments = $parseSegments; |
||
137 | } else { |
||
138 | $uriSegments = []; |
||
139 | } |
||
140 | |||
141 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
142 | $uriString = $this->uri->segments->__toString(); |
||
143 | |||
144 | $this->parseAction($action, $uriSegments); |
||
145 | if ( ! empty(services()->has('controller'))) { |
||
146 | return true; |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | |||
152 | // Try to get route from controller & page |
||
153 | if ($numOfUriSegments = $this->uri->segments->count()) { |
||
154 | $uriSegments = $this->uri->segments->getArrayCopy(); |
||
155 | $uriString = $this->uri->segments->__toString(); |
||
156 | |||
157 | for ($i = 0; $i <= $numOfUriSegments; $i++) { |
||
158 | $uriRoutedSegments = array_slice($uriSegments, 0, ($numOfUriSegments - $i)); |
||
159 | $modules = modules()->getArrayCopy(); |
||
160 | |||
161 | foreach ($modules as $module) { |
||
162 | $controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
||
163 | |||
164 | if ($module->getNamespace() === 'O2System\Framework\\') { |
||
165 | $controllerNamespace = 'O2System\Framework\Http\Controllers\\'; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Try to find requested controller |
||
170 | */ |
||
171 | if (class_exists($controllerClassName = $controllerNamespace . implode('\\', |
||
172 | array_map('studlycase', $uriRoutedSegments)))) { |
||
173 | |||
174 | if ($controllerClassName::$inherited) { |
||
175 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
176 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
177 | $uriSegments); |
||
178 | |||
179 | break; |
||
180 | } else { |
||
181 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
182 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
183 | $uriSegments); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Try to find requested page |
||
189 | */ |
||
190 | if (false !== ($pagesDir = $module->getResourcesDir('pages'))) { |
||
191 | if ($controllerClassName = $this->getPagesControllerClassName()) { |
||
192 | |||
193 | /** |
||
194 | * Try to find from database |
||
195 | */ |
||
196 | $modelClassName = str_replace('Controllers', 'Models', $controllerClassName); |
||
197 | |||
198 | if (class_exists($modelClassName)) { |
||
199 | models()->load($modelClassName, 'controller'); |
||
200 | |||
201 | if (false !== ($page = models('controller')->find($uriString, 'segments'))) { |
||
202 | if (isset($page->content)) { |
||
203 | presenter()->partials->offsetSet('content', $page->content); |
||
204 | |||
205 | $this->setController( |
||
206 | (new KernelControllerDataStructure($controllerClassName)) |
||
207 | ->setRequestMethod('index') |
||
208 | ); |
||
209 | |||
210 | return true; |
||
211 | break; |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Try to find from page file |
||
218 | */ |
||
219 | |||
220 | foreach (['.phtml', '.vue'] as $pageExtension) { |
||
221 | $pageFilePath = $pagesDir . implode(DIRECTORY_SEPARATOR, |
||
222 | array_map('dash', $uriRoutedSegments)) . $pageExtension; |
||
223 | |||
224 | if (is_file($pageFilePath)) { |
||
225 | presenter()->page->setFile($pageFilePath); |
||
226 | break; |
||
227 | } else { |
||
228 | $pageFilePath = str_replace($pageExtension, |
||
229 | DIRECTORY_SEPARATOR . 'index' . $pageExtension, $pageFilePath); |
||
230 | if (is_file($pageFilePath)) { |
||
231 | presenter()->page->setFile($pageFilePath); |
||
232 | break; |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
237 | if (presenter()->page->file instanceof SplFileInfo) { |
||
238 | $this->setController( |
||
239 | (new KernelControllerDataStructure($controllerClassName)) |
||
240 | ->setRequestMethod('index') |
||
241 | ); |
||
242 | |||
243 | return true; |
||
244 | break; |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | |||
250 | // break the loop if the controller has been set |
||
251 | if (services()->has('controller')) { |
||
252 | return true; |
||
253 | break; |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | if (class_exists($controllerClassName = modules()->top()->getDefaultControllerClassName())) { |
||
259 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
260 | $this->uri->segments->getArrayCopy()); |
||
261 | |||
262 | return true; |
||
263 | } |
||
264 | |||
265 | // Let's the framework do the rest when there is no controller found |
||
266 | // the framework will redirect to PAGE 404 |
||
267 | } |
||
268 | |||
269 | // ------------------------------------------------------------------------ |
||
270 | |||
271 | protected function handleExtensionRequest() |
||
272 | { |
||
273 | $lastSegment = $this->uri->segments->last(); |
||
274 | |||
275 | if (strpos($lastSegment, '.json') !== false) { |
||
276 | output()->setContentType('application/json'); |
||
277 | $lastSegment = str_replace('.json', '', $lastSegment); |
||
278 | $this->uri->segments->pop(); |
||
279 | $this->uri->segments->push($lastSegment); |
||
280 | } elseif (strpos($lastSegment, '.xml') !== false) { |
||
281 | output()->setContentType('application/xml'); |
||
282 | $lastSegment = str_replace('.xml', '', $lastSegment); |
||
283 | $this->uri->segments->pop(); |
||
284 | $this->uri->segments->push($lastSegment); |
||
285 | } elseif (strpos($lastSegment, '.js') !== false) { |
||
286 | output()->setContentType('application/x-javascript'); |
||
287 | $lastSegment = str_replace('.js', '', $lastSegment); |
||
288 | $this->uri->segments->pop(); |
||
289 | $this->uri->segments->push($lastSegment); |
||
290 | } elseif (strpos($lastSegment, '.css') !== false) { |
||
291 | output()->setContentType('text/css'); |
||
292 | $lastSegment = str_replace('.css', '', $lastSegment); |
||
293 | $this->uri->segments->pop(); |
||
294 | $this->uri->segments->push($lastSegment); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Router::handleAppRequest |
||
300 | * |
||
301 | * @param \O2System\Framework\Containers\Modules\DataStructures\Module $app |
||
302 | */ |
||
303 | public function handleAppRequest(FrameworkModuleDataStructure $app) |
||
304 | { |
||
305 | // Find App module |
||
306 | foreach(['modules', 'plugins'] as $additionalSegment) { |
||
307 | if (false !== ($module = modules()->getModule([ |
||
308 | $app->getParameter(), |
||
309 | $additionalSegment, |
||
310 | $this->uri->segments->first(), |
||
311 | ]))) { |
||
312 | $this->uri->segments->shift(); |
||
313 | |||
314 | $this->registerModule($module); |
||
315 | $this->handleSegmentsRequest(); |
||
316 | break; |
||
317 | } |
||
318 | } |
||
319 | } |
||
320 | |||
321 | // ------------------------------------------------------------------------ |
||
322 | |||
323 | /** |
||
324 | * Router::handleModuleRequest |
||
325 | */ |
||
326 | public function handleSegmentsRequest() |
||
353 | } |
||
354 | } |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | |||
359 | // ------------------------------------------------------------------------ |
||
360 | |||
361 | /** |
||
362 | * Router::getPagesControllerClassName |
||
363 | * |
||
364 | * @return bool|string |
||
365 | */ |
||
366 | final protected function getPagesControllerClassName() |
||
367 | { |
||
368 | $modules = modules()->getArrayCopy(); |
||
369 | |||
370 | foreach ($modules as $module) { |
||
371 | $controllerClassName = $module->getNamespace() . 'Controllers\Pages'; |
||
372 | if ($module->getNamespace() === 'O2System\Framework\\') { |
||
373 | $controllerClassName = 'O2System\Framework\Http\Controllers\Pages'; |
||
374 | } |
||
375 | |||
376 | if (class_exists($controllerClassName)) { |
||
377 | return $controllerClassName; |
||
378 | break; |
||
379 | } |
||
380 | } |
||
381 | |||
382 | if (class_exists('O2System\Framework\Http\Controllers\Pages')) { |
||
383 | return 'O2System\Framework\Http\Controllers\Pages'; |
||
384 | } |
||
385 | |||
386 | return false; |
||
387 | } |
||
388 | |||
389 | // ------------------------------------------------------------------------ |
||
390 | |||
391 | /** |
||
392 | * Router::registerModule |
||
393 | * |
||
394 | * @param FrameworkModuleDataStructure $module |
||
395 | */ |
||
396 | final public function registerModule(FrameworkModuleDataStructure $module) |
||
397 | { |
||
398 | // Push Subdomain App Module |
||
399 | modules()->push($module); |
||
400 | |||
401 | // Add Config FilePath |
||
402 | config()->addFilePath($module->getRealPath()); |
||
403 | |||
404 | // Reload Config |
||
405 | config()->reload(); |
||
406 | |||
407 | // Load modular addresses config |
||
408 | if (false !== ($configDir = $module->getDir('config', true))) { |
||
409 | unset($addresses); |
||
410 | |||
411 | $reconfig = false; |
||
412 | if (is_file( |
||
413 | $filePath = $configDir . ucfirst( |
||
414 | strtolower(ENVIRONMENT) |
||
415 | ) . DIRECTORY_SEPARATOR . 'Addresses.php' |
||
416 | )) { |
||
417 | require($filePath); |
||
418 | $reconfig = true; |
||
419 | } elseif (is_file( |
||
420 | $filePath = $configDir . 'Addresses.php' |
||
421 | )) { |
||
422 | require($filePath); |
||
423 | $reconfig = true; |
||
424 | } |
||
425 | |||
426 | if ( ! $reconfig) { |
||
427 | $controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
||
428 | $controllerClassName = $controllerNamespace . studlycase($module->getParameter()); |
||
429 | |||
430 | if (class_exists($controllerClassName)) { |
||
431 | $this->addresses->any( |
||
432 | '/', |
||
433 | function () use ($controllerClassName) { |
||
434 | return new $controllerClassName(); |
||
435 | } |
||
436 | ); |
||
437 | } |
||
438 | } elseif (isset($addresses)) { |
||
439 | $this->addresses = $addresses; |
||
440 | } |
||
441 | } else { |
||
442 | $controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
||
443 | $controllerClassName = $controllerNamespace . studlycase($module->getParameter()); |
||
444 | |||
445 | if (class_exists($controllerClassName)) { |
||
446 | $this->addresses->any( |
||
447 | '/', |
||
448 | function () use ($controllerClassName) { |
||
449 | return new $controllerClassName(); |
||
450 | } |
||
451 | ); |
||
452 | } |
||
453 | } |
||
454 | } |
||
455 | |||
456 | // ------------------------------------------------------------------------ |
||
457 | |||
458 | /** |
||
459 | * Router::parseAction |
||
460 | * |
||
461 | * @param KernelActionDataStructure $action |
||
462 | * @param array $uriSegments |
||
463 | * |
||
464 | * @throws \ReflectionException |
||
465 | */ |
||
466 | protected function parseAction(KernelActionDataStructure $action, array $uriSegments = []) |
||
531 | } |
||
532 | } |
||
533 | } |
||
534 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.