1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,183 +0,0 @@ |
1 |
-<?php |
|
2 |
- |
|
3 |
-namespace Illuminate\Container; |
|
4 |
- |
|
5 |
-use Closure; |
|
6 |
-use ReflectionMethod; |
|
7 |
-use ReflectionFunction; |
|
8 |
-use InvalidArgumentException; |
|
9 |
- |
|
10 |
-class BoundMethod |
|
11 |
-{ |
|
12 |
- /** |
|
13 |
- * Call the given Closure / class@method and inject its dependencies. |
|
14 |
- * |
|
15 |
- * @param \Illuminate\Container\Container $container |
|
16 |
- * @param callable|string $callback |
|
17 |
- * @param array $parameters |
|
18 |
- * @param string|null $defaultMethod |
|
19 |
- * @return mixed |
|
20 |
- * |
|
21 |
- * @throws \ReflectionException |
|
22 |
- * @throws \InvalidArgumentException |
|
23 |
- */ |
|
24 |
- public static function call($container, $callback, array $parameters = [], $defaultMethod = null) |
|
25 |
- { |
|
26 |
- if (static::isCallableWithAtSign($callback) || $defaultMethod) { |
|
27 |
- return static::callClass($container, $callback, $parameters, $defaultMethod); |
|
28 |
- } |
|
29 |
- |
|
30 |
- return static::callBoundMethod($container, $callback, function () use ($container, $callback, $parameters) { |
|
31 |
- return call_user_func_array( |
|
32 |
- $callback, static::getMethodDependencies($container, $callback, $parameters) |
|
33 |
- ); |
|
34 |
- }); |
|
35 |
- } |
|
36 |
- |
|
37 |
- /** |
|
38 |
- * Call a string reference to a class using Class@method syntax. |
|
39 |
- * |
|
40 |
- * @param \Illuminate\Container\Container $container |
|
41 |
- * @param string $target |
|
42 |
- * @param array $parameters |
|
43 |
- * @param string|null $defaultMethod |
|
44 |
- * @return mixed |
|
45 |
- * |
|
46 |
- * @throws \InvalidArgumentException |
|
47 |
- */ |
|
48 |
- protected static function callClass($container, $target, array $parameters = [], $defaultMethod = null) |
|
49 |
- { |
|
50 |
- $segments = explode('@', $target); |
|
51 |
- |
|
52 |
- // We will assume an @ sign is used to delimit the class name from the method |
|
53 |
- // name. We will split on this @ sign and then build a callable array that |
|
54 |
- // we can pass right back into the "call" method for dependency binding. |
|
55 |
- $method = count($segments) === 2 |
|
56 |
- ? $segments[1] : $defaultMethod; |
|
57 |
- |
|
58 |
- if (is_null($method)) { |
|
59 |
- throw new InvalidArgumentException('Method not provided.'); |
|
60 |
- } |
|
61 |
- |
|
62 |
- return static::call( |
|
63 |
- $container, [$container->make($segments[0]), $method], $parameters |
|
64 |
- ); |
|
65 |
- } |
|
66 |
- |
|
67 |
- /** |
|
68 |
- * Call a method that has been bound to the container. |
|
69 |
- * |
|
70 |
- * @param \Illuminate\Container\Container $container |
|
71 |
- * @param callable $callback |
|
72 |
- * @param mixed $default |
|
73 |
- * @return mixed |
|
74 |
- */ |
|
75 |
- protected static function callBoundMethod($container, $callback, $default) |
|
76 |
- { |
|
77 |
- if (! is_array($callback)) { |
|
78 |
- return $default instanceof Closure ? $default() : $default; |
|
79 |
- } |
|
80 |
- |
|
81 |
- // Here we need to turn the array callable into a Class@method string we can use to |
|
82 |
- // examine the container and see if there are any method bindings for this given |
|
83 |
- // method. If there are, we can call this method binding callback immediately. |
|
84 |
- $method = static::normalizeMethod($callback); |
|
85 |
- |
|
86 |
- if ($container->hasMethodBinding($method)) { |
|
87 |
- return $container->callMethodBinding($method, $callback[0]); |
|
88 |
- } |
|
89 |
- |
|
90 |
- return $default instanceof Closure ? $default() : $default; |
|
91 |
- } |
|
92 |
- |
|
93 |
- /** |
|
94 |
- * Normalize the given callback into a Class@method string. |
|
95 |
- * |
|
96 |
- * @param callable $callback |
|
97 |
- * @return string |
|
98 |
- */ |
|
99 |
- protected static function normalizeMethod($callback) |
|
100 |
- { |
|
101 |
- $class = is_string($callback[0]) ? $callback[0] : get_class($callback[0]); |
|
102 |
- |
|
103 |
- return "{$class}@{$callback[1]}"; |
|
104 |
- } |
|
105 |
- |
|
106 |
- /** |
|
107 |
- * Get all dependencies for a given method. |
|
108 |
- * |
|
109 |
- * @param \Illuminate\Container\Container $container |
|
110 |
- * @param callable|string $callback |
|
111 |
- * @param array $parameters |
|
112 |
- * @return array |
|
113 |
- * |
|
114 |
- * @throws \ReflectionException |
|
115 |
- */ |
|
116 |
- protected static function getMethodDependencies($container, $callback, array $parameters = []) |
|
117 |
- { |
|
118 |
- $dependencies = []; |
|
119 |
- |
|
120 |
- foreach (static::getCallReflector($callback)->getParameters() as $parameter) { |
|
121 |
- static::addDependencyForCallParameter($container, $parameter, $parameters, $dependencies); |
|
122 |
- } |
|
123 |
- |
|
124 |
- return array_merge($dependencies, $parameters); |
|
125 |
- } |
|
126 |
- |
|
127 |
- /** |
|
128 |
- * Get the proper reflection instance for the given callback. |
|
129 |
- * |
|
130 |
- * @param callable|string $callback |
|
131 |
- * @return \ReflectionFunctionAbstract |
|
132 |
- * |
|
133 |
- * @throws \ReflectionException |
|
134 |
- */ |
|
135 |
- protected static function getCallReflector($callback) |
|
136 |
- { |
|
137 |
- if (is_string($callback) && strpos($callback, '::') !== false) { |
|
138 |
- $callback = explode('::', $callback); |
|
139 |
- } |
|
140 |
- |
|
141 |
- return is_array($callback) |
|
142 |
- ? new ReflectionMethod($callback[0], $callback[1]) |
|
143 |
- : new ReflectionFunction($callback); |
|
144 |
- } |
|
145 |
- |
|
146 |
- /** |
|
147 |
- * Get the dependency for the given call parameter. |
|
148 |
- * |
|
149 |
- * @param \Illuminate\Container\Container $container |
|
150 |
- * @param \ReflectionParameter $parameter |
|
151 |
- * @param array $parameters |
|
152 |
- * @param array $dependencies |
|
153 |
- * @return void |
|
154 |
- */ |
|
155 |
- protected static function addDependencyForCallParameter($container, $parameter, |
|
156 |
- array &$parameters, &$dependencies) |
|
157 |
- { |
|
158 |
- if (array_key_exists($parameter->name, $parameters)) { |
|
159 |
- $dependencies[] = $parameters[$parameter->name]; |
|
160 |
- |
|
161 |
- unset($parameters[$parameter->name]); |
|
162 |
- } elseif ($parameter->getClass() && array_key_exists($parameter->getClass()->name, $parameters)) { |
|
163 |
- $dependencies[] = $parameters[$parameter->getClass()->name]; |
|
164 |
- |
|
165 |
- unset($parameters[$parameter->getClass()->name]); |
|
166 |
- } elseif ($parameter->getClass()) { |
|
167 |
- $dependencies[] = $container->make($parameter->getClass()->name); |
|
168 |
- } elseif ($parameter->isDefaultValueAvailable()) { |
|
169 |
- $dependencies[] = $parameter->getDefaultValue(); |
|
170 |
- } |
|
171 |
- } |
|
172 |
- |
|
173 |
- /** |
|
174 |
- * Determine if the given string is in Class@method syntax. |
|
175 |
- * |
|
176 |
- * @param mixed $callback |
|
177 |
- * @return bool |
|
178 |
- */ |
|
179 |
- protected static function isCallableWithAtSign($callback) |
|
180 |
- { |
|
181 |
- return is_string($callback) && strpos($callback, '@') !== false; |
|
182 |
- } |
|
183 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,183 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+namespace Illuminate\Container; |
|
4 |
+ |
|
5 |
+use Closure; |
|
6 |
+use ReflectionMethod; |
|
7 |
+use ReflectionFunction; |
|
8 |
+use InvalidArgumentException; |
|
9 |
+ |
|
10 |
+class BoundMethod |
|
11 |
+{ |
|
12 |
+ /** |
|
13 |
+ * Call the given Closure / class@method and inject its dependencies. |
|
14 |
+ * |
|
15 |
+ * @param \Illuminate\Container\Container $container |
|
16 |
+ * @param callable|string $callback |
|
17 |
+ * @param array $parameters |
|
18 |
+ * @param string|null $defaultMethod |
|
19 |
+ * @return mixed |
|
20 |
+ * |
|
21 |
+ * @throws \ReflectionException |
|
22 |
+ * @throws \InvalidArgumentException |
|
23 |
+ */ |
|
24 |
+ public static function call($container, $callback, array $parameters = [], $defaultMethod = null) |
|
25 |
+ { |
|
26 |
+ if (static::isCallableWithAtSign($callback) || $defaultMethod) { |
|
27 |
+ return static::callClass($container, $callback, $parameters, $defaultMethod); |
|
28 |
+ } |
|
29 |
+ |
|
30 |
+ return static::callBoundMethod($container, $callback, function () use ($container, $callback, $parameters) { |
|
31 |
+ return call_user_func_array( |
|
32 |
+ $callback, static::getMethodDependencies($container, $callback, $parameters) |
|
33 |
+ ); |
|
34 |
+ }); |
|
35 |
+ } |
|
36 |
+ |
|
37 |
+ /** |
|
38 |
+ * Call a string reference to a class using Class@method syntax. |
|
39 |
+ * |
|
40 |
+ * @param \Illuminate\Container\Container $container |
|
41 |
+ * @param string $target |
|
42 |
+ * @param array $parameters |
|
43 |
+ * @param string|null $defaultMethod |
|
44 |
+ * @return mixed |
|
45 |
+ * |
|
46 |
+ * @throws \InvalidArgumentException |
|
47 |
+ */ |
|
48 |
+ protected static function callClass($container, $target, array $parameters = [], $defaultMethod = null) |
|
49 |
+ { |
|
50 |
+ $segments = explode('@', $target); |
|
51 |
+ |
|
52 |
+ // We will assume an @ sign is used to delimit the class name from the method |
|
53 |
+ // name. We will split on this @ sign and then build a callable array that |
|
54 |
+ // we can pass right back into the "call" method for dependency binding. |
|
55 |
+ $method = count($segments) === 2 |
|
56 |
+ ? $segments[1] : $defaultMethod; |
|
57 |
+ |
|
58 |
+ if (is_null($method)) { |
|
59 |
+ throw new InvalidArgumentException('Method not provided.'); |
|
60 |
+ } |
|
61 |
+ |
|
62 |
+ return static::call( |
|
63 |
+ $container, [$container->make($segments[0]), $method], $parameters |
|
64 |
+ ); |
|
65 |
+ } |
|
66 |
+ |
|
67 |
+ /** |
|
68 |
+ * Call a method that has been bound to the container. |
|
69 |
+ * |
|
70 |
+ * @param \Illuminate\Container\Container $container |
|
71 |
+ * @param callable $callback |
|
72 |
+ * @param mixed $default |
|
73 |
+ * @return mixed |
|
74 |
+ */ |
|
75 |
+ protected static function callBoundMethod($container, $callback, $default) |
|
76 |
+ { |
|
77 |
+ if (! is_array($callback)) { |
|
78 |
+ return $default instanceof Closure ? $default() : $default; |
|
79 |
+ } |
|
80 |
+ |
|
81 |
+ // Here we need to turn the array callable into a Class@method string we can use to |
|
82 |
+ // examine the container and see if there are any method bindings for this given |
|
83 |
+ // method. If there are, we can call this method binding callback immediately. |
|
84 |
+ $method = static::normalizeMethod($callback); |
|
85 |
+ |
|
86 |
+ if ($container->hasMethodBinding($method)) { |
|
87 |
+ return $container->callMethodBinding($method, $callback[0]); |
|
88 |
+ } |
|
89 |
+ |
|
90 |
+ return $default instanceof Closure ? $default() : $default; |
|
91 |
+ } |
|
92 |
+ |
|
93 |
+ /** |
|
94 |
+ * Normalize the given callback into a Class@method string. |
|
95 |
+ * |
|
96 |
+ * @param callable $callback |
|
97 |
+ * @return string |
|
98 |
+ */ |
|
99 |
+ protected static function normalizeMethod($callback) |
|
100 |
+ { |
|
101 |
+ $class = is_string($callback[0]) ? $callback[0] : get_class($callback[0]); |
|
102 |
+ |
|
103 |
+ return "{$class}@{$callback[1]}"; |
|
104 |
+ } |
|
105 |
+ |
|
106 |
+ /** |
|
107 |
+ * Get all dependencies for a given method. |
|
108 |
+ * |
|
109 |
+ * @param \Illuminate\Container\Container $container |
|
110 |
+ * @param callable|string $callback |
|
111 |
+ * @param array $parameters |
|
112 |
+ * @return array |
|
113 |
+ * |
|
114 |
+ * @throws \ReflectionException |
|
115 |
+ */ |
|
116 |
+ protected static function getMethodDependencies($container, $callback, array $parameters = []) |
|
117 |
+ { |
|
118 |
+ $dependencies = []; |
|
119 |
+ |
|
120 |
+ foreach (static::getCallReflector($callback)->getParameters() as $parameter) { |
|
121 |
+ static::addDependencyForCallParameter($container, $parameter, $parameters, $dependencies); |
|
122 |
+ } |
|
123 |
+ |
|
124 |
+ return array_merge($dependencies, $parameters); |
|
125 |
+ } |
|
126 |
+ |
|
127 |
+ /** |
|
128 |
+ * Get the proper reflection instance for the given callback. |
|
129 |
+ * |
|
130 |
+ * @param callable|string $callback |
|
131 |
+ * @return \ReflectionFunctionAbstract |
|
132 |
+ * |
|
133 |
+ * @throws \ReflectionException |
|
134 |
+ */ |
|
135 |
+ protected static function getCallReflector($callback) |
|
136 |
+ { |
|
137 |
+ if (is_string($callback) && strpos($callback, '::') !== false) { |
|
138 |
+ $callback = explode('::', $callback); |
|
139 |
+ } |
|
140 |
+ |
|
141 |
+ return is_array($callback) |
|
142 |
+ ? new ReflectionMethod($callback[0], $callback[1]) |
|
143 |
+ : new ReflectionFunction($callback); |
|
144 |
+ } |
|
145 |
+ |
|
146 |
+ /** |
|
147 |
+ * Get the dependency for the given call parameter. |
|
148 |
+ * |
|
149 |
+ * @param \Illuminate\Container\Container $container |
|
150 |
+ * @param \ReflectionParameter $parameter |
|
151 |
+ * @param array $parameters |
|
152 |
+ * @param array $dependencies |
|
153 |
+ * @return void |
|
154 |
+ */ |
|
155 |
+ protected static function addDependencyForCallParameter($container, $parameter, |
|
156 |
+ array &$parameters, &$dependencies) |
|
157 |
+ { |
|
158 |
+ if (array_key_exists($parameter->name, $parameters)) { |
|
159 |
+ $dependencies[] = $parameters[$parameter->name]; |
|
160 |
+ |
|
161 |
+ unset($parameters[$parameter->name]); |
|
162 |
+ } elseif ($parameter->getClass() && array_key_exists($parameter->getClass()->name, $parameters)) { |
|
163 |
+ $dependencies[] = $parameters[$parameter->getClass()->name]; |
|
164 |
+ |
|
165 |
+ unset($parameters[$parameter->getClass()->name]); |
|
166 |
+ } elseif ($parameter->getClass()) { |
|
167 |
+ $dependencies[] = $container->make($parameter->getClass()->name); |
|
168 |
+ } elseif ($parameter->isDefaultValueAvailable()) { |
|
169 |
+ $dependencies[] = $parameter->getDefaultValue(); |
|
170 |
+ } |
|
171 |
+ } |
|
172 |
+ |
|
173 |
+ /** |
|
174 |
+ * Determine if the given string is in Class@method syntax. |
|
175 |
+ * |
|
176 |
+ * @param mixed $callback |
|
177 |
+ * @return bool |
|
178 |
+ */ |
|
179 |
+ protected static function isCallableWithAtSign($callback) |
|
180 |
+ { |
|
181 |
+ return is_string($callback) && strpos($callback, '@') !== false; |
|
182 |
+ } |
|
183 |
+} |