PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.0
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.0
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / vendor / wpfluent / framework / src / WPFluent / Http / SubstituteRouteParametersTrait.php

SubstituteRouteParametersTrait.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.10.0, at vendor/wpfluent/framework/src/WPFluent/Http/SubstituteRouteParametersTrait.php

82 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\Framework\Http;
4
5 use Closure;
6 use ReflectionMethod;
7 use ReflectionFunction;
8 use InvalidArgumentException;
9 use FluentSupport\Framework\Container\Util;
10 use FluentSupport\Framework\Support\Reflector;
11 use FluentSupport\Framework\Support\UrlRoutable;
12
13 trait SubstituteRouteParametersTrait
14 {
15 protected function SubstituteParameters($routeParameters)
16 {
17 $resolved = [];
18
19 $signatureParameters = $this->getParametersFromRouteAction();
20
21 if ($signatureParameters) {
22
23 foreach ($signatureParameters as $signatureParameter) {
24
25 $name = $signatureParameter->getName();
26
27 if ($this->boundModel($name, $signatureParameter, $routeParameters)) {
28
29 $class = Util::getParameterClassName($signatureParameter);
30
31 $resolved[$name] = $this->app->make($class)->findOrFail(
32 $routeParameters[$name]
33 );
34
35 unset($routeParameters[$name]);
36 }
37
38 }
39 }
40
41 $remainingParams = [];
42
43 $signatureParameters = array_filter($signatureParameters, function($param) {
44 return !class_exists(Reflector::getParameterClassName($param) ?: '');
45 });
46
47
48 if (($params = count($signatureParameters)) && $params != count($routeParameters)) {
49 throw new InvalidArgumentException(
50 'Invalid route action, route parameters doesn\'t match with method signature.'
51 );
52 }
53
54 foreach ($routeParameters as $param) {
55
56 if ($dep = array_shift($signatureParameters)) {
57 $remainingParams[$dep->getName()] = $param;
58 }
59 }
60
61 return $resolved + $remainingParams;
62 }
63
64 protected function boundModel($name, $parameter, $routeParameters)
65 {
66 if (array_key_exists($name, $routeParameters)) {
67 return Reflector::isParameterSubclassOf($parameter, UrlRoutable::class);
68 }
69 }
70
71 protected function getParametersFromRouteAction()
72 {
73 if ($this->action instanceof Closure) {
74 return (new ReflectionFunction($this->action))->getParameters();
75 }
76
77 list($class, $method) = explode('@', $this->action);
78
79 return (new ReflectionMethod($class, $method))->getParameters();
80 }
81 }
82