@@ -26,199 +26,199 @@ discard block |
||
26 | 26 | class Container implements ArrayAccess |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * Implement all basic manipulation methods, including the ArrayAccess interface methods |
|
31 | - * and all the magic acessor methods as __get or __set. |
|
32 | - */ |
|
33 | - |
|
34 | - use ContainerCollectionMethods; |
|
35 | - |
|
36 | - /** |
|
37 | - * Implement all abstraction method, such bind, bindIf, bindTo, share, extends, |
|
38 | - * isBound, isSingleton, flush. |
|
39 | - */ |
|
40 | - |
|
41 | - use ContainerAbstractionMethods; |
|
42 | - |
|
43 | - /** |
|
44 | - * Holds all resolved or resolvable instances into the container. |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
48 | - |
|
49 | - protected $collection; |
|
50 | - |
|
51 | - /** |
|
52 | - * Class specific defined dependencies. |
|
53 | - * |
|
54 | - * @var array |
|
55 | - */ |
|
56 | - |
|
57 | - protected $dependencies; |
|
58 | - |
|
59 | - /** |
|
60 | - * Cache of classes inspector and resolver. |
|
61 | - * |
|
62 | - * @var array |
|
63 | - */ |
|
64 | - |
|
65 | - protected $resolving; |
|
66 | - |
|
67 | - /** |
|
68 | - * Cache of classes dependencies in callbacks ready for resolution. |
|
69 | - * |
|
70 | - * @var array |
|
71 | - */ |
|
72 | - |
|
73 | - protected $resolved; |
|
74 | - |
|
75 | - /** |
|
76 | - * Reset the container, removing all the elements, cache and options. |
|
77 | - * |
|
78 | - * @return void |
|
79 | - */ |
|
80 | - |
|
81 | - public function flush() |
|
82 | - { |
|
83 | - $this->collection = []; |
|
84 | - $this->dependencies = []; |
|
85 | - $this->resolving = []; |
|
86 | - $this->resolved = []; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Call a user function injecting the dependencies. |
|
91 | - * |
|
92 | - * @param string|Closure $function The function or the user function name. |
|
93 | - * @param array $parameters The predefined dependencies. |
|
94 | - * |
|
95 | - * @return mixed |
|
96 | - */ |
|
97 | - |
|
98 | - public function call($function, $parameters = []) |
|
99 | - { |
|
100 | - $inspector = new ReflectionFunction($function); |
|
101 | - $dependencies = $inspector->getParameters(); |
|
102 | - $resolvedClosureDependencies = []; |
|
103 | - |
|
104 | - foreach ($dependencies as $dependency) { |
|
105 | - if (isset($parameters[$dependency->name])) { |
|
106 | - $resolvedClosureDependencies[] = $parameters[$dependency->name]; |
|
107 | - } else { |
|
108 | - if (($class = $dependency->getClass()) === null) { |
|
109 | - $resolvedClosureDependencies[] = $dependency->isOptional() ? $dependency->getDefaultValue() : null; |
|
110 | - } else $resolvedClosureDependencies[] = $this->make($class->name); |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - return call_user_func_array($function, $resolvedClosureDependencies); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Makes an element or class injecting automatically all the dependencies. |
|
119 | - * |
|
120 | - * @param string $abstract The class name or container element name to make. |
|
121 | - * @param array $parameters Specific parameters definition. |
|
122 | - * @param bool $force Specify if a new element must be given and the dependencies must have be recalculated. |
|
123 | - * |
|
124 | - * @throws ReflectionException |
|
125 | - * @return object|null |
|
126 | - */ |
|
127 | - |
|
128 | - public function make($abstract, $parameters = [], $force = false) |
|
129 | - { |
|
130 | - if ($force === false && isset($this->collection[$abstract])) { |
|
131 | - return $this->offsetGet($abstract); |
|
132 | - } |
|
133 | - |
|
134 | - if (isset($this->resolving[$abstract])) { |
|
135 | - return $this->resolving[$abstract]($abstract, $parameters); |
|
136 | - } |
|
137 | - |
|
138 | - $callable = $this->resolving[$abstract] = $this->construct($abstract, $force); |
|
139 | - return $callable($abstract, $parameters); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Construct a class and all the dependencies using the reflection library of PHP. |
|
144 | - * |
|
145 | - * @param string $abstract The class name or container element name to make. |
|
146 | - * @param bool $force Specify if a new element must be given and the dependencies must have be recalculated. |
|
147 | - * |
|
148 | - * @throws ReflectionException |
|
149 | - * @return Closure |
|
150 | - */ |
|
151 | - |
|
152 | - protected function construct($abstract, $force) |
|
153 | - { |
|
154 | - $inspector = new ReflectionClass($abstract); |
|
155 | - |
|
156 | - if ($constructor = $inspector->getConstructor()) { |
|
157 | - $dependencies = $constructor->getParameters(); |
|
158 | - |
|
159 | - return function ($abstract, $parameters) use ($inspector, $dependencies, $force) { |
|
160 | - $resolvedClassParameters = []; |
|
161 | - |
|
162 | - foreach ($dependencies as $dependency) { |
|
163 | - if (isset($parameters[$dependency->name])) { |
|
164 | - $resolvedClassParameters[] = $parameters[$dependency->name]; |
|
165 | - } else $resolvedClassParameters[] = $this->resolve($abstract, $dependency, $force); |
|
166 | - } |
|
167 | - |
|
168 | - return $inspector->newInstanceArgs($resolvedClassParameters); |
|
169 | - }; |
|
170 | - } |
|
29 | + /** |
|
30 | + * Implement all basic manipulation methods, including the ArrayAccess interface methods |
|
31 | + * and all the magic acessor methods as __get or __set. |
|
32 | + */ |
|
33 | + |
|
34 | + use ContainerCollectionMethods; |
|
35 | + |
|
36 | + /** |
|
37 | + * Implement all abstraction method, such bind, bindIf, bindTo, share, extends, |
|
38 | + * isBound, isSingleton, flush. |
|
39 | + */ |
|
40 | + |
|
41 | + use ContainerAbstractionMethods; |
|
42 | + |
|
43 | + /** |
|
44 | + * Holds all resolved or resolvable instances into the container. |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | + |
|
49 | + protected $collection; |
|
50 | + |
|
51 | + /** |
|
52 | + * Class specific defined dependencies. |
|
53 | + * |
|
54 | + * @var array |
|
55 | + */ |
|
56 | + |
|
57 | + protected $dependencies; |
|
58 | + |
|
59 | + /** |
|
60 | + * Cache of classes inspector and resolver. |
|
61 | + * |
|
62 | + * @var array |
|
63 | + */ |
|
64 | + |
|
65 | + protected $resolving; |
|
66 | + |
|
67 | + /** |
|
68 | + * Cache of classes dependencies in callbacks ready for resolution. |
|
69 | + * |
|
70 | + * @var array |
|
71 | + */ |
|
72 | + |
|
73 | + protected $resolved; |
|
74 | + |
|
75 | + /** |
|
76 | + * Reset the container, removing all the elements, cache and options. |
|
77 | + * |
|
78 | + * @return void |
|
79 | + */ |
|
80 | + |
|
81 | + public function flush() |
|
82 | + { |
|
83 | + $this->collection = []; |
|
84 | + $this->dependencies = []; |
|
85 | + $this->resolving = []; |
|
86 | + $this->resolved = []; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Call a user function injecting the dependencies. |
|
91 | + * |
|
92 | + * @param string|Closure $function The function or the user function name. |
|
93 | + * @param array $parameters The predefined dependencies. |
|
94 | + * |
|
95 | + * @return mixed |
|
96 | + */ |
|
97 | + |
|
98 | + public function call($function, $parameters = []) |
|
99 | + { |
|
100 | + $inspector = new ReflectionFunction($function); |
|
101 | + $dependencies = $inspector->getParameters(); |
|
102 | + $resolvedClosureDependencies = []; |
|
103 | + |
|
104 | + foreach ($dependencies as $dependency) { |
|
105 | + if (isset($parameters[$dependency->name])) { |
|
106 | + $resolvedClosureDependencies[] = $parameters[$dependency->name]; |
|
107 | + } else { |
|
108 | + if (($class = $dependency->getClass()) === null) { |
|
109 | + $resolvedClosureDependencies[] = $dependency->isOptional() ? $dependency->getDefaultValue() : null; |
|
110 | + } else $resolvedClosureDependencies[] = $this->make($class->name); |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + return call_user_func_array($function, $resolvedClosureDependencies); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Makes an element or class injecting automatically all the dependencies. |
|
119 | + * |
|
120 | + * @param string $abstract The class name or container element name to make. |
|
121 | + * @param array $parameters Specific parameters definition. |
|
122 | + * @param bool $force Specify if a new element must be given and the dependencies must have be recalculated. |
|
123 | + * |
|
124 | + * @throws ReflectionException |
|
125 | + * @return object|null |
|
126 | + */ |
|
127 | + |
|
128 | + public function make($abstract, $parameters = [], $force = false) |
|
129 | + { |
|
130 | + if ($force === false && isset($this->collection[$abstract])) { |
|
131 | + return $this->offsetGet($abstract); |
|
132 | + } |
|
133 | + |
|
134 | + if (isset($this->resolving[$abstract])) { |
|
135 | + return $this->resolving[$abstract]($abstract, $parameters); |
|
136 | + } |
|
137 | + |
|
138 | + $callable = $this->resolving[$abstract] = $this->construct($abstract, $force); |
|
139 | + return $callable($abstract, $parameters); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Construct a class and all the dependencies using the reflection library of PHP. |
|
144 | + * |
|
145 | + * @param string $abstract The class name or container element name to make. |
|
146 | + * @param bool $force Specify if a new element must be given and the dependencies must have be recalculated. |
|
147 | + * |
|
148 | + * @throws ReflectionException |
|
149 | + * @return Closure |
|
150 | + */ |
|
151 | + |
|
152 | + protected function construct($abstract, $force) |
|
153 | + { |
|
154 | + $inspector = new ReflectionClass($abstract); |
|
155 | + |
|
156 | + if ($constructor = $inspector->getConstructor()) { |
|
157 | + $dependencies = $constructor->getParameters(); |
|
158 | + |
|
159 | + return function ($abstract, $parameters) use ($inspector, $dependencies, $force) { |
|
160 | + $resolvedClassParameters = []; |
|
161 | + |
|
162 | + foreach ($dependencies as $dependency) { |
|
163 | + if (isset($parameters[$dependency->name])) { |
|
164 | + $resolvedClassParameters[] = $parameters[$dependency->name]; |
|
165 | + } else $resolvedClassParameters[] = $this->resolve($abstract, $dependency, $force); |
|
166 | + } |
|
167 | + |
|
168 | + return $inspector->newInstanceArgs($resolvedClassParameters); |
|
169 | + }; |
|
170 | + } |
|
171 | 171 | |
172 | - return function ($abstract) { |
|
173 | - return new $abstract; |
|
174 | - }; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Resolve all the given class reflected dependencies. |
|
179 | - * |
|
180 | - * @param string $abstract The class name or container element name to resolve dependencies. |
|
181 | - * @param ReflectionParameter $dependency The class dependency to be resolved. |
|
182 | - * @param bool $force Specify if the dependencies must be recalculated. |
|
183 | - * |
|
184 | - * @return Object |
|
185 | - */ |
|
186 | - |
|
187 | - protected function resolve($abstract, $dependency, $force) |
|
188 | - { |
|
189 | - $key = $abstract.$dependency->name; |
|
190 | - |
|
191 | - if (!isset($this->resolved[$key]) || $force === true) { |
|
192 | - $this->resolved[$key] = $this->generate($abstract, $dependency); |
|
193 | - } |
|
194 | - |
|
195 | - return $this->resolved[$key]($this); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Generate the dependencies callbacks to jump some conditions in every dependency creation. |
|
200 | - * |
|
201 | - * @param string $abstract The class name or container element name to resolve dependencies. |
|
202 | - * @param ReflectionParameter $dependency The class dependency to be resolved. |
|
203 | - * |
|
204 | - * @return Closure |
|
205 | - */ |
|
206 | - |
|
207 | - protected function generate($abstract, $dependency) |
|
208 | - { |
|
209 | - if ($class = $dependency->getClass()) { |
|
210 | - $classname = $class->name; |
|
211 | - $key = $abstract.$classname; |
|
212 | - |
|
213 | - if (isset($this->dependencies[$key])) { |
|
214 | - return $this->dependencies[$key]; |
|
215 | - } else return function () use ($classname) { |
|
216 | - return $this->make($classname); |
|
217 | - }; |
|
218 | - } |
|
219 | - |
|
220 | - return $class->getDefaultValue(); |
|
221 | - } |
|
172 | + return function ($abstract) { |
|
173 | + return new $abstract; |
|
174 | + }; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Resolve all the given class reflected dependencies. |
|
179 | + * |
|
180 | + * @param string $abstract The class name or container element name to resolve dependencies. |
|
181 | + * @param ReflectionParameter $dependency The class dependency to be resolved. |
|
182 | + * @param bool $force Specify if the dependencies must be recalculated. |
|
183 | + * |
|
184 | + * @return Object |
|
185 | + */ |
|
186 | + |
|
187 | + protected function resolve($abstract, $dependency, $force) |
|
188 | + { |
|
189 | + $key = $abstract.$dependency->name; |
|
190 | + |
|
191 | + if (!isset($this->resolved[$key]) || $force === true) { |
|
192 | + $this->resolved[$key] = $this->generate($abstract, $dependency); |
|
193 | + } |
|
194 | + |
|
195 | + return $this->resolved[$key]($this); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Generate the dependencies callbacks to jump some conditions in every dependency creation. |
|
200 | + * |
|
201 | + * @param string $abstract The class name or container element name to resolve dependencies. |
|
202 | + * @param ReflectionParameter $dependency The class dependency to be resolved. |
|
203 | + * |
|
204 | + * @return Closure |
|
205 | + */ |
|
206 | + |
|
207 | + protected function generate($abstract, $dependency) |
|
208 | + { |
|
209 | + if ($class = $dependency->getClass()) { |
|
210 | + $classname = $class->name; |
|
211 | + $key = $abstract.$classname; |
|
212 | + |
|
213 | + if (isset($this->dependencies[$key])) { |
|
214 | + return $this->dependencies[$key]; |
|
215 | + } else return function () use ($classname) { |
|
216 | + return $this->make($classname); |
|
217 | + }; |
|
218 | + } |
|
219 | + |
|
220 | + return $class->getDefaultValue(); |
|
221 | + } |
|
222 | 222 | |
223 | 223 | } |
224 | 224 | |
@@ -239,55 +239,55 @@ discard block |
||
239 | 239 | trait ContainerCollectionMethods |
240 | 240 | { |
241 | 241 | |
242 | - public function offsetGet($abstract) |
|
243 | - { |
|
244 | - if (isset($this->collection[$abstract])) { |
|
245 | - $concrete = $this->collection[$abstract]; |
|
246 | - |
|
247 | - if ($concrete instanceof Closure) { |
|
248 | - return $concrete($this); |
|
249 | - } else return $concrete; |
|
250 | - } |
|
251 | - |
|
252 | - return $this->make($abstract); |
|
253 | - } |
|
254 | - |
|
255 | - public function offsetSet($abstract, $value) |
|
256 | - { |
|
257 | - if (is_object($value)) { |
|
258 | - $this->instance($abstract, $value); |
|
259 | - } else $this->bind($abstract, $value); |
|
260 | - } |
|
261 | - |
|
262 | - public function offsetExists($abstract) |
|
263 | - { |
|
264 | - return isset($this->collection[$abstract]); |
|
265 | - } |
|
266 | - |
|
267 | - public function offsetUnset($abstract) |
|
268 | - { |
|
269 | - unset($this->collection[$abstract]); |
|
270 | - } |
|
271 | - |
|
272 | - public function __get($offset) |
|
273 | - { |
|
274 | - return $this->offsetGet(str_replace('_', '.', $offset)); |
|
275 | - } |
|
276 | - |
|
277 | - public function __set($offset, $value) |
|
278 | - { |
|
279 | - $this->offsetSet(str_replace('_', '.', $offset), $value); |
|
280 | - } |
|
281 | - |
|
282 | - public function __unset($offset) |
|
283 | - { |
|
284 | - $this->offsetUnset(str_replace('_', '.', $offset)); |
|
285 | - } |
|
286 | - |
|
287 | - public function __isset($offset) |
|
288 | - { |
|
289 | - return $this->offsetExists(str_replace('_', '.', $offset)); |
|
290 | - } |
|
242 | + public function offsetGet($abstract) |
|
243 | + { |
|
244 | + if (isset($this->collection[$abstract])) { |
|
245 | + $concrete = $this->collection[$abstract]; |
|
246 | + |
|
247 | + if ($concrete instanceof Closure) { |
|
248 | + return $concrete($this); |
|
249 | + } else return $concrete; |
|
250 | + } |
|
251 | + |
|
252 | + return $this->make($abstract); |
|
253 | + } |
|
254 | + |
|
255 | + public function offsetSet($abstract, $value) |
|
256 | + { |
|
257 | + if (is_object($value)) { |
|
258 | + $this->instance($abstract, $value); |
|
259 | + } else $this->bind($abstract, $value); |
|
260 | + } |
|
261 | + |
|
262 | + public function offsetExists($abstract) |
|
263 | + { |
|
264 | + return isset($this->collection[$abstract]); |
|
265 | + } |
|
266 | + |
|
267 | + public function offsetUnset($abstract) |
|
268 | + { |
|
269 | + unset($this->collection[$abstract]); |
|
270 | + } |
|
271 | + |
|
272 | + public function __get($offset) |
|
273 | + { |
|
274 | + return $this->offsetGet(str_replace('_', '.', $offset)); |
|
275 | + } |
|
276 | + |
|
277 | + public function __set($offset, $value) |
|
278 | + { |
|
279 | + $this->offsetSet(str_replace('_', '.', $offset), $value); |
|
280 | + } |
|
281 | + |
|
282 | + public function __unset($offset) |
|
283 | + { |
|
284 | + $this->offsetUnset(str_replace('_', '.', $offset)); |
|
285 | + } |
|
286 | + |
|
287 | + public function __isset($offset) |
|
288 | + { |
|
289 | + return $this->offsetExists(str_replace('_', '.', $offset)); |
|
290 | + } |
|
291 | 291 | |
292 | 292 | } |
293 | 293 | |
@@ -307,176 +307,176 @@ discard block |
||
307 | 307 | trait ContainerAbstractionMethods |
308 | 308 | { |
309 | 309 | |
310 | - /** |
|
311 | - * Verify if an element exists in container. |
|
312 | - * |
|
313 | - * @return bool |
|
314 | - */ |
|
315 | - |
|
316 | - public function isBound($abstract) |
|
317 | - { |
|
318 | - return isset($this->collection[$abstract]); |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * Verify if an element has a singleton instance. |
|
323 | - * |
|
324 | - * @return bool |
|
325 | - */ |
|
326 | - |
|
327 | - public function isSingleton($abstract) |
|
328 | - { |
|
329 | - return isset($this->collection[$abstract]); |
|
330 | - } |
|
331 | - |
|
332 | - /** |
|
333 | - * Bind a new element to the container. |
|
334 | - * |
|
335 | - * @param string $abstract The alias name that will be used to call the element. |
|
336 | - * @param string|closure $concrete The element class name, or an closure that makes the element. |
|
337 | - * @param bool $shared Define if the element will be a singleton instance. |
|
338 | - * |
|
339 | - * @return \Codeburner\Container\Container |
|
340 | - */ |
|
341 | - |
|
342 | - public function bind($abstract, $concrete, $shared = false) |
|
343 | - { |
|
344 | - if ($concrete instanceof Closure === false) { |
|
345 | - $concrete = function (Container $container) use ($concrete) { |
|
346 | - return $container->make($concrete); |
|
347 | - }; |
|
348 | - } |
|
349 | - |
|
350 | - if ($shared === true) { |
|
351 | - $this->collection[$abstract] = $concrete($this); |
|
352 | - } else $this->collection[$abstract] = $concrete; |
|
353 | - |
|
354 | - return $this; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Bind a new element to the container IF the element name not exists in the container. |
|
359 | - * |
|
360 | - * @param string $abstract The alias name that will be used to call the element. |
|
361 | - * @param string|closure $concrete The element class name, or an closure that makes the element. |
|
362 | - * @param bool $shared Define if the element will be a singleton instance. |
|
363 | - * |
|
364 | - * @return \Codeburner\Container\Container |
|
365 | - */ |
|
366 | - |
|
367 | - public function bindIf($abstract, $concrete, $shared = false) |
|
368 | - { |
|
369 | - if (!isset($this->collection[$abstract])) { |
|
370 | - $this->bind($abstract, $concrete, $shared); |
|
371 | - } |
|
372 | - |
|
373 | - return $this; |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * Bind an specific instance to a class dependency. |
|
378 | - * |
|
379 | - * @param string $class The class full name. |
|
380 | - * @param string $dependencyName The dependency full name. |
|
381 | - * @param string|closure $dependency The specific object class name or a classure that makes the element. |
|
382 | - * |
|
383 | - * @return \Codeburner\Container\Container |
|
384 | - */ |
|
385 | - |
|
386 | - public function bindTo($class, $dependencyName, $dependency) |
|
387 | - { |
|
388 | - if ($dependency instanceof Closure === false) { |
|
389 | - if (is_object($dependency)) { |
|
390 | - $dependency = function () use ($dependency) { |
|
391 | - return $dependency; |
|
392 | - }; |
|
393 | - } else { |
|
394 | - $dependency = function () use ($dependency) { |
|
395 | - return $this->offsetGet($dependency); |
|
396 | - }; |
|
397 | - } |
|
398 | - } |
|
399 | - |
|
400 | - $this->dependencies[$class.$dependencyName] = $dependency; |
|
401 | - |
|
402 | - return $this; |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * Bind an element that will be construct only one time, and every call for the element, |
|
407 | - * the same instance will be given. |
|
408 | - * |
|
409 | - * @param string $abstract The alias name that will be used to call the element. |
|
410 | - * @param string|closure $concrete The element class name, or an closure that makes the element. |
|
411 | - * |
|
412 | - * @return \Codeburner\Container\Container |
|
413 | - */ |
|
414 | - |
|
415 | - public function singleton($abstract, $concrete) |
|
416 | - { |
|
417 | - $this->bind($abstract, $concrete, true); |
|
418 | - |
|
419 | - return $this; |
|
420 | - } |
|
421 | - |
|
422 | - /** |
|
423 | - * Bind an object to the container. |
|
424 | - * |
|
425 | - * @param string $abstract The alias name that will be used to call the object. |
|
426 | - * @param object $instance The object that will be inserted. |
|
427 | - * |
|
428 | - * @return \Codeburner\Container\Container |
|
429 | - */ |
|
430 | - |
|
431 | - public function instance($abstract, $instance) |
|
432 | - { |
|
433 | - if (is_object($instance)) { |
|
434 | - $this->collection[$abstract] = $instance; |
|
435 | - } |
|
436 | - |
|
437 | - return $this; |
|
438 | - } |
|
439 | - |
|
440 | - /** |
|
441 | - * Modify an element with a given function that receive the old element as argument. |
|
442 | - * |
|
443 | - * @param string $abstract The alias name that will be used to call the element. |
|
444 | - * @param closure $extension The function that receives the old element and return a new or modified one. |
|
445 | - * |
|
446 | - * @return \Codeburner\Container\Container |
|
447 | - */ |
|
448 | - |
|
449 | - public function extend($abstract, $extension) |
|
450 | - { |
|
451 | - if (isset($this->collection[$abstract])) { |
|
452 | - $object = $this->collection[$abstract]; |
|
453 | - |
|
454 | - if ($object instanceof Closure === false) { |
|
455 | - $this->collection[$abstract] = $extension($object, $this); |
|
456 | - } else $this->collection[$abstract] = function () use ($object, $extension) { |
|
457 | - return $extension($object($this), $this); |
|
458 | - }; |
|
459 | - } |
|
460 | - |
|
461 | - return $this; |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Makes an resolvable element an singleton. |
|
466 | - * |
|
467 | - * @param string $abstract The alias name that will be used to call the element. |
|
468 | - * |
|
469 | - * @return \Codeburner\Container\Container |
|
470 | - */ |
|
471 | - |
|
472 | - public function share($abstract) |
|
473 | - { |
|
474 | - if (isset($this->collection[$abstract]) && $this->collection[$abstract] instanceof Closure) { |
|
475 | - $this->collection[$abstract] = $this->collection[$abstract]($this); |
|
476 | - } |
|
477 | - |
|
478 | - return $this; |
|
479 | - } |
|
310 | + /** |
|
311 | + * Verify if an element exists in container. |
|
312 | + * |
|
313 | + * @return bool |
|
314 | + */ |
|
315 | + |
|
316 | + public function isBound($abstract) |
|
317 | + { |
|
318 | + return isset($this->collection[$abstract]); |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * Verify if an element has a singleton instance. |
|
323 | + * |
|
324 | + * @return bool |
|
325 | + */ |
|
326 | + |
|
327 | + public function isSingleton($abstract) |
|
328 | + { |
|
329 | + return isset($this->collection[$abstract]); |
|
330 | + } |
|
331 | + |
|
332 | + /** |
|
333 | + * Bind a new element to the container. |
|
334 | + * |
|
335 | + * @param string $abstract The alias name that will be used to call the element. |
|
336 | + * @param string|closure $concrete The element class name, or an closure that makes the element. |
|
337 | + * @param bool $shared Define if the element will be a singleton instance. |
|
338 | + * |
|
339 | + * @return \Codeburner\Container\Container |
|
340 | + */ |
|
341 | + |
|
342 | + public function bind($abstract, $concrete, $shared = false) |
|
343 | + { |
|
344 | + if ($concrete instanceof Closure === false) { |
|
345 | + $concrete = function (Container $container) use ($concrete) { |
|
346 | + return $container->make($concrete); |
|
347 | + }; |
|
348 | + } |
|
349 | + |
|
350 | + if ($shared === true) { |
|
351 | + $this->collection[$abstract] = $concrete($this); |
|
352 | + } else $this->collection[$abstract] = $concrete; |
|
353 | + |
|
354 | + return $this; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Bind a new element to the container IF the element name not exists in the container. |
|
359 | + * |
|
360 | + * @param string $abstract The alias name that will be used to call the element. |
|
361 | + * @param string|closure $concrete The element class name, or an closure that makes the element. |
|
362 | + * @param bool $shared Define if the element will be a singleton instance. |
|
363 | + * |
|
364 | + * @return \Codeburner\Container\Container |
|
365 | + */ |
|
366 | + |
|
367 | + public function bindIf($abstract, $concrete, $shared = false) |
|
368 | + { |
|
369 | + if (!isset($this->collection[$abstract])) { |
|
370 | + $this->bind($abstract, $concrete, $shared); |
|
371 | + } |
|
372 | + |
|
373 | + return $this; |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * Bind an specific instance to a class dependency. |
|
378 | + * |
|
379 | + * @param string $class The class full name. |
|
380 | + * @param string $dependencyName The dependency full name. |
|
381 | + * @param string|closure $dependency The specific object class name or a classure that makes the element. |
|
382 | + * |
|
383 | + * @return \Codeburner\Container\Container |
|
384 | + */ |
|
385 | + |
|
386 | + public function bindTo($class, $dependencyName, $dependency) |
|
387 | + { |
|
388 | + if ($dependency instanceof Closure === false) { |
|
389 | + if (is_object($dependency)) { |
|
390 | + $dependency = function () use ($dependency) { |
|
391 | + return $dependency; |
|
392 | + }; |
|
393 | + } else { |
|
394 | + $dependency = function () use ($dependency) { |
|
395 | + return $this->offsetGet($dependency); |
|
396 | + }; |
|
397 | + } |
|
398 | + } |
|
399 | + |
|
400 | + $this->dependencies[$class.$dependencyName] = $dependency; |
|
401 | + |
|
402 | + return $this; |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * Bind an element that will be construct only one time, and every call for the element, |
|
407 | + * the same instance will be given. |
|
408 | + * |
|
409 | + * @param string $abstract The alias name that will be used to call the element. |
|
410 | + * @param string|closure $concrete The element class name, or an closure that makes the element. |
|
411 | + * |
|
412 | + * @return \Codeburner\Container\Container |
|
413 | + */ |
|
414 | + |
|
415 | + public function singleton($abstract, $concrete) |
|
416 | + { |
|
417 | + $this->bind($abstract, $concrete, true); |
|
418 | + |
|
419 | + return $this; |
|
420 | + } |
|
421 | + |
|
422 | + /** |
|
423 | + * Bind an object to the container. |
|
424 | + * |
|
425 | + * @param string $abstract The alias name that will be used to call the object. |
|
426 | + * @param object $instance The object that will be inserted. |
|
427 | + * |
|
428 | + * @return \Codeburner\Container\Container |
|
429 | + */ |
|
430 | + |
|
431 | + public function instance($abstract, $instance) |
|
432 | + { |
|
433 | + if (is_object($instance)) { |
|
434 | + $this->collection[$abstract] = $instance; |
|
435 | + } |
|
436 | + |
|
437 | + return $this; |
|
438 | + } |
|
439 | + |
|
440 | + /** |
|
441 | + * Modify an element with a given function that receive the old element as argument. |
|
442 | + * |
|
443 | + * @param string $abstract The alias name that will be used to call the element. |
|
444 | + * @param closure $extension The function that receives the old element and return a new or modified one. |
|
445 | + * |
|
446 | + * @return \Codeburner\Container\Container |
|
447 | + */ |
|
448 | + |
|
449 | + public function extend($abstract, $extension) |
|
450 | + { |
|
451 | + if (isset($this->collection[$abstract])) { |
|
452 | + $object = $this->collection[$abstract]; |
|
453 | + |
|
454 | + if ($object instanceof Closure === false) { |
|
455 | + $this->collection[$abstract] = $extension($object, $this); |
|
456 | + } else $this->collection[$abstract] = function () use ($object, $extension) { |
|
457 | + return $extension($object($this), $this); |
|
458 | + }; |
|
459 | + } |
|
460 | + |
|
461 | + return $this; |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Makes an resolvable element an singleton. |
|
466 | + * |
|
467 | + * @param string $abstract The alias name that will be used to call the element. |
|
468 | + * |
|
469 | + * @return \Codeburner\Container\Container |
|
470 | + */ |
|
471 | + |
|
472 | + public function share($abstract) |
|
473 | + { |
|
474 | + if (isset($this->collection[$abstract]) && $this->collection[$abstract] instanceof Closure) { |
|
475 | + $this->collection[$abstract] = $this->collection[$abstract]($this); |
|
476 | + } |
|
477 | + |
|
478 | + return $this; |
|
479 | + } |
|
480 | 480 | |
481 | 481 | } |
482 | 482 |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | if ($constructor = $inspector->getConstructor()) { |
157 | 157 | $dependencies = $constructor->getParameters(); |
158 | 158 | |
159 | - return function ($abstract, $parameters) use ($inspector, $dependencies, $force) { |
|
159 | + return function($abstract, $parameters) use ($inspector, $dependencies, $force) { |
|
160 | 160 | $resolvedClassParameters = []; |
161 | 161 | |
162 | 162 | foreach ($dependencies as $dependency) { |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | }; |
170 | 170 | } |
171 | 171 | |
172 | - return function ($abstract) { |
|
172 | + return function($abstract) { |
|
173 | 173 | return new $abstract; |
174 | 174 | }; |
175 | 175 | } |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | |
213 | 213 | if (isset($this->dependencies[$key])) { |
214 | 214 | return $this->dependencies[$key]; |
215 | - } else return function () use ($classname) { |
|
215 | + } else return function() use ($classname) { |
|
216 | 216 | return $this->make($classname); |
217 | 217 | }; |
218 | 218 | } |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | public function bind($abstract, $concrete, $shared = false) |
343 | 343 | { |
344 | 344 | if ($concrete instanceof Closure === false) { |
345 | - $concrete = function (Container $container) use ($concrete) { |
|
345 | + $concrete = function(Container $container) use ($concrete) { |
|
346 | 346 | return $container->make($concrete); |
347 | 347 | }; |
348 | 348 | } |
@@ -387,11 +387,11 @@ discard block |
||
387 | 387 | { |
388 | 388 | if ($dependency instanceof Closure === false) { |
389 | 389 | if (is_object($dependency)) { |
390 | - $dependency = function () use ($dependency) { |
|
390 | + $dependency = function() use ($dependency) { |
|
391 | 391 | return $dependency; |
392 | 392 | }; |
393 | 393 | } else { |
394 | - $dependency = function () use ($dependency) { |
|
394 | + $dependency = function() use ($dependency) { |
|
395 | 395 | return $this->offsetGet($dependency); |
396 | 396 | }; |
397 | 397 | } |
@@ -453,7 +453,7 @@ discard block |
||
453 | 453 | |
454 | 454 | if ($object instanceof Closure === false) { |
455 | 455 | $this->collection[$abstract] = $extension($object, $this); |
456 | - } else $this->collection[$abstract] = function () use ($object, $extension) { |
|
456 | + } else $this->collection[$abstract] = function() use ($object, $extension) { |
|
457 | 457 | return $extension($object($this), $this); |
458 | 458 | }; |
459 | 459 | } |
@@ -19,18 +19,18 @@ |
||
19 | 19 | interface ContainerAwareInterface |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * Inject the container instance into the class. |
|
24 | - * |
|
25 | - * @param \Codeburner\Container\Container $container |
|
26 | - */ |
|
27 | - public function setContainer(Container $container); |
|
22 | + /** |
|
23 | + * Inject the container instance into the class. |
|
24 | + * |
|
25 | + * @param \Codeburner\Container\Container $container |
|
26 | + */ |
|
27 | + public function setContainer(Container $container); |
|
28 | 28 | |
29 | - /** |
|
30 | - * get the container instance. |
|
31 | - * |
|
32 | - * @return \Codeburner\Container\Container $container |
|
33 | - */ |
|
34 | - public function getContainer(); |
|
29 | + /** |
|
30 | + * get the container instance. |
|
31 | + * |
|
32 | + * @return \Codeburner\Container\Container $container |
|
33 | + */ |
|
34 | + public function getContainer(); |
|
35 | 35 | |
36 | 36 | } |
@@ -19,34 +19,34 @@ |
||
19 | 19 | trait ContainerAwareTrait |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * The instance of container. |
|
24 | - * |
|
25 | - * @var \Codeburner\Container\Container |
|
26 | - */ |
|
27 | - protected $container; |
|
28 | - |
|
29 | - /** |
|
30 | - * Set a container. |
|
31 | - * |
|
32 | - * @param \Codeburner\Container\Container $container |
|
33 | - * @return mixed |
|
34 | - */ |
|
35 | - public function setContainer(Container $container) |
|
36 | - { |
|
37 | - $this->container = $container; |
|
38 | - |
|
39 | - return $this; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Get the container. |
|
44 | - * |
|
45 | - * @return \Codeburner\Container\Container |
|
46 | - */ |
|
47 | - public function getContainer() |
|
48 | - { |
|
49 | - return $this->container; |
|
50 | - } |
|
22 | + /** |
|
23 | + * The instance of container. |
|
24 | + * |
|
25 | + * @var \Codeburner\Container\Container |
|
26 | + */ |
|
27 | + protected $container; |
|
28 | + |
|
29 | + /** |
|
30 | + * Set a container. |
|
31 | + * |
|
32 | + * @param \Codeburner\Container\Container $container |
|
33 | + * @return mixed |
|
34 | + */ |
|
35 | + public function setContainer(Container $container) |
|
36 | + { |
|
37 | + $this->container = $container; |
|
38 | + |
|
39 | + return $this; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Get the container. |
|
44 | + * |
|
45 | + * @return \Codeburner\Container\Container |
|
46 | + */ |
|
47 | + public function getContainer() |
|
48 | + { |
|
49 | + return $this->container; |
|
50 | + } |
|
51 | 51 | |
52 | 52 | } |