Total Complexity | 77 |
Total Lines | 735 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like Route 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 Route, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class Route |
||
42 | { |
||
43 | use Concerns\RouteCondition, |
||
|
|||
44 | Concerns\RouteDependencyResolver; |
||
45 | |||
46 | /** |
||
47 | * Action that the route will use when called. |
||
48 | * |
||
49 | * @var \Closure|string|array $action |
||
50 | */ |
||
51 | public $action; |
||
52 | |||
53 | /** |
||
54 | * The computed gathered middleware. |
||
55 | * |
||
56 | * @var array|null $computedMiddleware |
||
57 | */ |
||
58 | public $computedMiddleware; |
||
59 | |||
60 | /** |
||
61 | * The container instance used by the route. |
||
62 | * |
||
63 | * @var \Syscodes\Container\Container $container |
||
64 | */ |
||
65 | protected $container; |
||
66 | |||
67 | /** |
||
68 | * The controller instance. |
||
69 | * |
||
70 | * @var string $controller |
||
71 | */ |
||
72 | public $controller; |
||
73 | |||
74 | /** |
||
75 | * The default values for the route. |
||
76 | * |
||
77 | * @var array $defaults |
||
78 | */ |
||
79 | public $defaults = []; |
||
80 | |||
81 | /** |
||
82 | * Variable of HTTP method. |
||
83 | * |
||
84 | * @var array|string $method |
||
85 | */ |
||
86 | public $method; |
||
87 | |||
88 | /** |
||
89 | * The array of matched parameters. |
||
90 | * |
||
91 | * @var array $parameters |
||
92 | */ |
||
93 | public $parameters = []; |
||
94 | |||
95 | /** |
||
96 | * The parameter names for the route. |
||
97 | * |
||
98 | * @var string|null $parameterNames |
||
99 | */ |
||
100 | public $parameterNames; |
||
101 | |||
102 | /** |
||
103 | * Patterns that should be replaced. |
||
104 | * |
||
105 | * @var array $patterns |
||
106 | */ |
||
107 | public $patterns = [ |
||
108 | '~/~' => '\/', // Slash |
||
109 | '~{an:[^\/{}]+}~' => '([0-9a-zA-Z]++)', // Placeholder accepts alphabetic and numeric chars |
||
110 | '~{n:[^\/{}]+}~' => '([0-9]++)', // Placeholder accepts only numeric |
||
111 | '~{a:[^\/{}]+}~' => '([a-zA-Z]++)', // Placeholder accepts only alphabetic chars |
||
112 | '~{w:[^\/{}]+}~' => '([0-9a-zA-Z-_]++)', // Placeholder accepts alphanumeric and underscore |
||
113 | '~{\*:[^\/{}]+}~' => '(.++)', // Placeholder match rest of url |
||
114 | '~(\\\/)?{\?:[^\/{}]+}~' => '\/?([^\/]*)', // Optional placeholder |
||
115 | '~{[^\/{}]+}~' => '([^\/]++)' // Normal placeholder |
||
116 | ]; |
||
117 | |||
118 | /** |
||
119 | * The URI pattern the route responds to. |
||
120 | * |
||
121 | * @var array $uri |
||
122 | */ |
||
123 | public $uri = []; |
||
124 | |||
125 | /** |
||
126 | * Contains the arguments of the current route. |
||
127 | * |
||
128 | * @var array $where |
||
129 | */ |
||
130 | public $wheres = []; |
||
131 | |||
132 | /** |
||
133 | * Constructor. Initialize route. |
||
134 | * |
||
135 | * @param array|string|null $method |
||
136 | * @param string|null $uri |
||
137 | * @param \Closure|string|null $action |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | public function __construct($method = null, $uri = null, $action = null) |
||
142 | { |
||
143 | $this->uri = $uri; |
||
144 | |||
145 | // Set the method |
||
146 | $this->parseMethod($method); |
||
147 | |||
148 | // Set the action |
||
149 | $this->action = Arr::except($this->parseAction($action), ['prefix']); |
||
150 | |||
151 | if (is_null($prefix = Arr::get($this->action, 'prefix'))) { |
||
152 | $this->prefix($prefix); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | // Getters |
||
157 | |||
158 | /** |
||
159 | * Get the action of the current route. |
||
160 | * |
||
161 | * @return \Closure|string|array |
||
162 | */ |
||
163 | public function getAction() |
||
164 | { |
||
165 | return $this->action; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Get the arguments of the current route. |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | public function getArguments() |
||
174 | { |
||
175 | return $this->wheres; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Get the controller instance for the route. |
||
180 | * |
||
181 | * @return mixed |
||
182 | */ |
||
183 | public function getController() |
||
184 | { |
||
185 | if ( ! $this->controller) { |
||
186 | $class = $this->parseControllerCallback()[0]; |
||
187 | |||
188 | $this->controller = $this->container->make(ltrim($class, '\\')); |
||
189 | } |
||
190 | |||
191 | return $this->controller; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Get the controller method used for the route. |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getControllerMethod() |
||
200 | { |
||
201 | return $this->parseControllerCallback()[1]; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Get the request method of the current route. |
||
206 | * |
||
207 | * @return array|string |
||
208 | */ |
||
209 | public function getMethod() |
||
210 | { |
||
211 | return $this->method; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Get the url of the current route. |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | public function getName() |
||
220 | { |
||
221 | return $this->action['as'] ?? null; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Get the url of the current route. |
||
226 | * |
||
227 | * @return array |
||
228 | */ |
||
229 | public function getRoute() |
||
230 | { |
||
231 | return $this->uri; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Get or set the domain for the route. |
||
236 | * |
||
237 | * @param string|null $domain |
||
238 | * |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function domain($domain = null) |
||
242 | { |
||
243 | if (is_null($domain)) { |
||
244 | return $this->getDomain(); |
||
245 | } |
||
246 | |||
247 | $this->action['domain'] = $this->parseRoute($domain); |
||
248 | |||
249 | return $this; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Get the domain defined for the route. |
||
254 | * |
||
255 | * @return string|null |
||
256 | */ |
||
257 | public function getDomain() |
||
258 | { |
||
259 | return isset($this->action['domain']) |
||
260 | ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
||
261 | : null; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Parse the controller. |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | public function parseControllerCallback() |
||
270 | { |
||
271 | return Str::parseCallback($this->action['uses']); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Checks whether the route's action is a controller. |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | public function isControllerAction() |
||
280 | { |
||
281 | return is_string($this->action['uses']); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Get the dispatcher for the route's controller. |
||
286 | * |
||
287 | * @return \Syscodes\Controller\ControllerDispatcher |
||
288 | */ |
||
289 | private function controllerDispatcher() |
||
290 | { |
||
291 | return new ControllerDispatcher($this->container); |
||
292 | } |
||
293 | |||
294 | // Setters |
||
295 | |||
296 | /** |
||
297 | * Run the route action and return the response. |
||
298 | * |
||
299 | * @return mixed |
||
300 | */ |
||
301 | public function runResolver() |
||
302 | { |
||
303 | $this->container = $this->container ?: new Container; |
||
304 | |||
305 | try { |
||
306 | if ($this->isControllerAction()) { |
||
307 | return $this->runResolverController(); |
||
308 | } |
||
309 | |||
310 | return $this->runResolverCallable(); |
||
311 | } catch (HttpResponseException $e) { |
||
312 | return $e->getResponse(); |
||
313 | } |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * Run the route action and return the response. |
||
318 | * |
||
319 | * @return mixed |
||
320 | */ |
||
321 | protected function runResolverCallable() |
||
322 | { |
||
323 | $callable = $this->action['uses']; |
||
324 | |||
325 | return $callable(...array_values($this->resolveMethodDependencies( |
||
326 | $this->parametersWithouNulls(), new ReflectionFunction($this->action['uses']) |
||
327 | ))); |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Run the route action and return the response. |
||
332 | * |
||
333 | * @return mixed |
||
334 | */ |
||
335 | protected function runResolverController() |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Set the action. |
||
342 | * |
||
343 | * @param \Closure|string $action |
||
344 | * |
||
345 | * @return $this |
||
346 | * |
||
347 | * @throws \InvalidArgumentException |
||
348 | */ |
||
349 | public function parseAction($action) |
||
350 | { |
||
351 | if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
||
352 | throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
||
353 | } |
||
354 | |||
355 | return RouteAction::parse($this->uri, $action); |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Set the method of the current route. |
||
360 | * |
||
361 | * @param array $method |
||
362 | * |
||
363 | * @return string $this |
||
364 | * |
||
365 | * @throws \InvalidArgumentException |
||
366 | */ |
||
367 | public function parseMethod($method) |
||
368 | { |
||
369 | if ($method === null || empty($method)) { |
||
370 | throw new InvalidArgumentException(__('route.methodNotProvided')); |
||
371 | } |
||
372 | |||
373 | foreach ((array) $method as $httpMethod) { |
||
374 | if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
||
375 | throw new InvalidArgumentException(__('route.methodNotAllowed')); |
||
376 | } |
||
377 | } |
||
378 | |||
379 | $this->method = $method; |
||
380 | |||
381 | return $this; |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Set the route. |
||
386 | * |
||
387 | * @param string|array|null $uri |
||
388 | * |
||
389 | * @return string |
||
390 | * |
||
391 | * @throws \InvalidArgumentException |
||
392 | */ |
||
393 | public function parseRoute($uri) |
||
394 | { |
||
395 | if ($uri === null) { |
||
396 | throw new InvalidArgumentException(__('route.uriNotProvided')); |
||
397 | } |
||
398 | |||
399 | $this->uri = $this->parseRoutePath($uri); |
||
400 | |||
401 | return $this; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * Replace word patterns with regex in route uri. |
||
406 | * |
||
407 | * @param string $uri |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | protected function parseRoutePath($uri) |
||
412 | { |
||
413 | $uri = trim($uri, '\/?'); |
||
414 | $uri = trim($uri, '\/'); |
||
415 | |||
416 | preg_match_all('/\{([\w\:]+?)\??\}/', $uri, $matches); |
||
417 | |||
418 | foreach ($matches[1] as $match) { |
||
419 | if (strpos($match, ':') === false) { |
||
420 | continue; |
||
421 | } |
||
422 | |||
423 | $pattern = array_keys($this->patterns); |
||
424 | $replace = array_values($this->patterns); |
||
425 | $segments = explode(':', trim($match, '{}?')); |
||
426 | |||
427 | $uri = strpos($match, ':') !== false |
||
428 | ? preg_replace($pattern, $replace, $uri) |
||
429 | : str_replace($match, '{'.$segments[0].'}', $uri); |
||
430 | } |
||
431 | |||
432 | return $uri; |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Add a prefix to the route URI. |
||
437 | * |
||
438 | * @param string $prefix |
||
439 | * |
||
440 | * @return $this |
||
441 | */ |
||
442 | public function prefix($prefix) |
||
443 | { |
||
444 | if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
||
445 | $this->action['prefix'] = $newPrefix; |
||
446 | } |
||
447 | |||
448 | $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
||
449 | |||
450 | return $this->parseRoute($uri !== '/' ? trim($uri, '/') : $uri); |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Set the action array for the route. |
||
455 | * |
||
456 | * @param array $action |
||
457 | * |
||
458 | * @return $this |
||
459 | */ |
||
460 | public function setAction(array $action) |
||
461 | { |
||
462 | $this->action = $action; |
||
463 | |||
464 | if (isset($this->action['domain'])) { |
||
465 | $this->domain($this->action['domain']); |
||
466 | } |
||
467 | |||
468 | return $this; |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Set the name. |
||
473 | * |
||
474 | * @param string $name |
||
475 | * |
||
476 | * @return $this |
||
477 | */ |
||
478 | public function name($name) |
||
479 | { |
||
480 | $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
||
481 | |||
482 | return $this; |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Determine whether the route's name matches the given patterns. |
||
487 | * |
||
488 | * @param mixed ...$patterns |
||
489 | * |
||
490 | * @return bool |
||
491 | */ |
||
492 | public function named(...$patterns) |
||
493 | { |
||
494 | if (is_null($routeName = $this->getName())) { |
||
495 | return false; |
||
496 | } |
||
497 | |||
498 | foreach ($patterns as $pattern) { |
||
499 | if (Str::is($pattern, $routeName)) { |
||
500 | return true; |
||
501 | } |
||
502 | } |
||
503 | |||
504 | return false; |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * Set a default value for the route. |
||
509 | * |
||
510 | * @param string $key |
||
511 | * @param mixed $value |
||
512 | * |
||
513 | * @return $this |
||
514 | */ |
||
515 | public function defaults($key, $value) |
||
516 | { |
||
517 | $this->defaults[$key] = $value; |
||
518 | |||
519 | return $this; |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * Set a default values for the route. |
||
524 | * |
||
525 | * @param string $defaults |
||
526 | * |
||
527 | * @return $this |
||
528 | */ |
||
529 | public function setDefaults(array $defaults) |
||
530 | { |
||
531 | $this->defaults = $defaults; |
||
532 | |||
533 | return $this; |
||
534 | } |
||
535 | |||
536 | /** |
||
537 | * Set the where. |
||
538 | * |
||
539 | * @param array|string $name |
||
540 | * @param string|null $expression |
||
541 | * |
||
542 | * @return $this |
||
543 | */ |
||
544 | public function where($name, string $expression = null) |
||
545 | { |
||
546 | $wheres = is_array($name) ? $name : [$name => $expression]; |
||
547 | |||
548 | foreach ($wheres as $name => $expression) { |
||
549 | $this->wheres[$name] = $expression; |
||
550 | } |
||
551 | |||
552 | return $this; |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | * Bind the route to a given request for execution. |
||
557 | * |
||
558 | * @param \Syscodes\Http\Request $request |
||
559 | * |
||
560 | * @return $this |
||
561 | */ |
||
562 | public function bind(Request $request) |
||
563 | { |
||
564 | $this->parameters = (new RouteParamBinding($this))->parameters($request); |
||
565 | |||
566 | return $this; |
||
567 | } |
||
568 | |||
569 | /** |
||
570 | * Get all of the parameter names for the route. |
||
571 | * |
||
572 | * @return array |
||
573 | */ |
||
574 | public function parameterNames() |
||
575 | { |
||
576 | if (isset($this->parameterNames)) { |
||
577 | return $this->parameterNames; |
||
578 | } |
||
579 | |||
580 | return $this->parameterNames = $this->compileParamNames(); |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Get the parameter names for the route. |
||
585 | * |
||
586 | * @return array |
||
587 | */ |
||
588 | protected function compileParamNames() |
||
589 | { |
||
590 | preg_match_all('~[^\/\{(.*?)\}]~', $this->domain().$this->uri, $matches); |
||
591 | |||
592 | return array_map(function ($match) { |
||
593 | return trim($match, '?'); |
||
594 | }, $matches[0]); |
||
595 | } |
||
596 | |||
597 | /** |
||
598 | * Get a given parameter from the route. |
||
599 | * |
||
600 | * @param string $name |
||
601 | * @param mixed $default |
||
602 | * |
||
603 | * @return array |
||
604 | */ |
||
605 | public function parameter($name, $default = null) |
||
606 | { |
||
607 | return Arr::get($this->parameters(), $name, $default); |
||
608 | } |
||
609 | |||
610 | /** |
||
611 | * Set a parameter to the given value. |
||
612 | * |
||
613 | * @param string $name |
||
614 | * @param mixed $value |
||
615 | * |
||
616 | * @return array |
||
617 | */ |
||
618 | public function setParameter($name, $value) |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Get the key / value list of parameters without null values. |
||
627 | * |
||
628 | * @return array |
||
629 | */ |
||
630 | public function parametersWithouNulls() |
||
631 | { |
||
632 | return array_filter($this->parameters(), function ($parameter) { |
||
633 | return ! is_null($parameter); |
||
634 | }); |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * Get the key / value list of parameters for the route. |
||
639 | * |
||
640 | * @return array |
||
641 | */ |
||
642 | public function parameters() |
||
643 | { |
||
644 | if (isset($this->parameters)) { |
||
645 | return $this->parameters; |
||
646 | } |
||
647 | |||
648 | throw new LogicException('The route is not bound.'); |
||
649 | } |
||
650 | |||
651 | /** |
||
652 | * Get all middleware, including the ones from the controller. |
||
653 | * |
||
654 | * @return array |
||
655 | */ |
||
656 | public function gatherMiddleware() |
||
657 | { |
||
658 | if ( ! is_null($this->computedMiddleware)) { |
||
659 | return $this->computedMiddleware; |
||
660 | } |
||
661 | |||
662 | $this->computedMiddleware = []; |
||
663 | |||
664 | return $this->computedMiddleware = array_unique(array_merge( |
||
665 | $this->middleware(), |
||
666 | $this->controllerMiddleware() |
||
667 | ), SORT_REGULAR); |
||
668 | } |
||
669 | |||
670 | /** |
||
671 | * Get or set the middlewares attached to the route. |
||
672 | * |
||
673 | * @param array|string|null $middleware |
||
674 | * |
||
675 | * @return $this|array |
||
676 | */ |
||
677 | public function middleware($middleware = null) |
||
693 | } |
||
694 | |||
695 | /** |
||
696 | * Get the middlewares attached to the route. |
||
697 | * |
||
698 | * @return array |
||
699 | */ |
||
700 | protected function getMiddleware() |
||
701 | { |
||
702 | return (array) ($this->action['middleware'] ?? []); |
||
703 | } |
||
704 | |||
705 | /** |
||
706 | * Get the middleware for the route's controller. |
||
707 | * |
||
708 | * @return array |
||
709 | */ |
||
710 | public function controllerMiddleware() |
||
711 | { |
||
712 | if ( ! $this->isControllerAction()) { |
||
713 | return []; |
||
714 | } |
||
715 | |||
716 | return $this->controllerDispatcher()->getMiddleware( |
||
717 | $this->getController(), |
||
718 | $this->getControllerMethod() |
||
719 | ); |
||
720 | } |
||
721 | |||
722 | /** |
||
723 | * Determine if the route only responds to HTTP requests. |
||
724 | * |
||
725 | * @return bool |
||
726 | */ |
||
727 | public function httpOnly() |
||
728 | { |
||
729 | return in_array('http', $this->action, true); |
||
730 | } |
||
731 | |||
732 | /** |
||
733 | * Determine if the route only responds to HTTPS requests. |
||
734 | * |
||
735 | * @return bool |
||
736 | */ |
||
737 | public function httpsOnly() |
||
740 | } |
||
741 | |||
742 | /** |
||
743 | * Determine if the route only responds to HTTPS requests. |
||
744 | * |
||
745 | * @return bool |
||
746 | */ |
||
747 | public function secure() |
||
750 | } |
||
751 | |||
752 | /** |
||
753 | * Set the container instance on the route. |
||
754 | * |
||
755 | * @param \Syscodes\Container\Container $container |
||
756 | * |
||
757 | * @return $this |
||
758 | */ |
||
759 | public function setContainer(Container $container) |
||
760 | { |
||
761 | $this->container = $container; |
||
762 | |||
763 | return $this; |
||
764 | } |
||
765 | |||
766 | /** |
||
767 | * Dynamically access route parameters. |
||
768 | * |
||
769 | * @param string $key |
||
770 | * |
||
771 | * @return mixed |
||
772 | */ |
||
773 | public function __get($key) |
||
776 | } |
||
777 | } |