PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.92
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.92
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Http / SubstituteRouteParametersTrait.php

SubstituteRouteParametersTrait.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.92, at vendor/wpfluent/framework/src/WPFluent/Http/SubstituteRouteParametersTrait.php

85 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 FluentCommunity\Framework\Http;
4
5 use Closure;
6 use ReflectionMethod;
7 use ReflectionFunction;
8 use InvalidArgumentException;
9 use FluentCommunity\Framework\Container\Util;
10 use FluentCommunity\Framework\Support\Reflector;
11 use FluentCommunity\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 $signatureParam) {
24
25 $name = $signatureParam->getName();
26
27 if ($this->boundModel($name, $signatureParam, $routeParameters)) {
28
29 $class = Util::getParameterClassName($signatureParam);
30
31 $instance = $this->app->make($class);
32
33 $resolved[$name] = $instance->resolveRouteBinding(
34 $routeParameters[$name]
35 );
36
37 unset($routeParameters[$name]);
38 }
39 }
40 }
41
42 $remainingParams = [];
43
44 $signatureParameters = array_filter($signatureParameters, function($param) {
45 return !class_exists(Reflector::getParameterClassName($param) ?: '');
46 });
47
48 $params = count($signatureParameters);
49
50 if ($params && $params != count($routeParameters)) {
51 throw new InvalidArgumentException(
52 'Route parameters doesn\'t match with method signature.'
53 );
54 }
55
56 foreach ($routeParameters as $param) {
57 if ($dep = array_shift($signatureParameters)) {
58 $remainingParams[$dep->getName()] = $param;
59 }
60 }
61
62 return $resolved + $remainingParams;
63 }
64
65 protected function boundModel($name, $parameter, $routeParameters)
66 {
67 if (array_key_exists($name, $routeParameters)) {
68 return Reflector::isParameterSubclassOf(
69 $parameter, UrlRoutable::class
70 );
71 }
72 }
73
74 protected function getParametersFromRouteAction()
75 {
76 if ($this->action instanceof Closure) {
77 return (new ReflectionFunction($this->action))->getParameters();
78 }
79
80 list($class, $method) = explode('@', $this->action);
81
82 return (new ReflectionMethod($class, $method))->getParameters();
83 }
84 }
85