1 | <?php |
||
24 | class Container implements ContainerInterface |
||
25 | { |
||
26 | const LOGIC_FUNCTION_NAME = 'diContainer'; |
||
27 | |||
28 | /** @var array[string] Collection of alias => class name for alias resolving */ |
||
29 | protected $aliases = array(); |
||
30 | |||
31 | /** @var array[string] Collection of alias => closure for alias resolving */ |
||
32 | protected $callbacks = array(); |
||
33 | |||
34 | /** @var array[string] Collection of loaded services */ |
||
35 | protected $services = array(); |
||
36 | |||
37 | /** @var array[string] Collection of class name dependencies trees */ |
||
38 | protected $dependencies = array(); |
||
39 | |||
40 | /** @var Generator */ |
||
41 | protected $generator; |
||
42 | |||
43 | /** |
||
44 | * Container constructor. |
||
45 | * |
||
46 | * @param Generator $generator |
||
47 | */ |
||
48 | 4 | public function __construct(Generator $generator) |
|
52 | |||
53 | /** |
||
54 | * Internal logic handler. Calls generated logic function |
||
55 | * for performing entity creation or search. This is encapsulated |
||
56 | * method for further overriding. |
||
57 | * |
||
58 | * @param string $alias Entity alias |
||
59 | * |
||
60 | * @return mixed Created instance or null |
||
61 | */ |
||
62 | 3 | protected function logic($alias) |
|
66 | |||
67 | /** |
||
68 | * Get reflection paramater class name type hint if present without |
||
69 | * auto loading and throwing exceptions. |
||
70 | * |
||
71 | * @param \ReflectionParameter $param Parameter for parsing |
||
72 | * |
||
73 | * @return string|null Class name typehint or null |
||
74 | */ |
||
75 | 4 | protected function getClassName(\ReflectionParameter $param) |
|
82 | |||
83 | /** |
||
84 | * Recursively build class constructor dependencies tree. |
||
85 | * TODO: Analyze recurrent dependencies and throw an exception |
||
86 | * |
||
87 | * @param string $className Current class name for analyzing |
||
88 | * @param array $dependencies Reference to tree for filling up |
||
89 | * |
||
90 | * @return array [string] Multidimensional array as dependency tree |
||
91 | * @throws ClassNotFoundException |
||
92 | */ |
||
93 | 4 | protected function buildDependenciesTree($className, array &$dependencies) |
|
129 | |||
130 | /** |
||
131 | * Recursive object creation with dependencies. |
||
132 | * |
||
133 | * @param array $dependencies Collection of current class dependenices |
||
134 | * @param string $class Current class name |
||
135 | * |
||
136 | * @throws ConstructorParameterNotSetException |
||
137 | */ |
||
138 | |||
139 | 1 | public function generateLogicConditions(array &$dependencies, $class) |
|
184 | |||
185 | /** |
||
186 | * @param string $functionName |
||
187 | * |
||
188 | * @return string |
||
189 | * @throws ConstructorParameterNotSetException |
||
190 | */ |
||
191 | 1 | public function generateLogicFunction($functionName = self::LOGIC_FUNCTION_NAME) |
|
251 | |||
252 | /** |
||
253 | * Finds an entry of the container by its identifier and returns it. |
||
254 | * |
||
255 | * @param string $alias Identifier of the entry to look for. |
||
256 | * |
||
257 | * @throws NotFoundException No entry was found for this identifier. |
||
258 | * @throws ContainerException Error while retrieving the entry. |
||
259 | * |
||
260 | * @return mixed Entry. |
||
261 | */ |
||
262 | 3 | public function get($alias) |
|
277 | |||
278 | /** |
||
279 | * Returns true if the container can return an entry for the given identifier. |
||
280 | * Returns false otherwise. |
||
281 | * |
||
282 | * @param string $alias Identifier of the entry to look for. |
||
283 | * |
||
284 | * @return boolean |
||
285 | */ |
||
286 | 1 | public function has($alias) |
|
292 | |||
293 | /** |
||
294 | * Set dependency alias with callback function. |
||
295 | * |
||
296 | * @param callable $callable Callable to return dependency |
||
297 | * @param string $alias Dependency name |
||
298 | * |
||
299 | * @return self Chaining |
||
300 | */ |
||
301 | 4 | public function callback($callable, $alias = null) |
|
312 | |||
313 | /** |
||
314 | * Set service dependency. Upon first creation of this class instance |
||
315 | * it would be used everywhere where this dependency is needed. |
||
316 | * |
||
317 | * @param string $className Fully qualified class name |
||
318 | * @param string $alias Dependency name |
||
319 | * @param array $parameters Collection of parameters needed for dependency creation |
||
320 | * |
||
321 | * @return self Chaining |
||
322 | */ |
||
323 | 4 | public function service($className, $alias = null, array $parameters = array()) |
|
329 | |||
330 | /** |
||
331 | * Set service dependency by passing object instance. |
||
332 | * |
||
333 | * @param mixed $instance Instance that needs to be return by this dependency |
||
334 | * @param string $alias Dependency name |
||
335 | * @param array $parameters Collection of parameters needed for dependency creation |
||
336 | * |
||
337 | * @return self Chaining |
||
338 | */ |
||
339 | public function instance(&$instance, $alias = null, array $parameters = array()) |
||
344 | |||
345 | /** |
||
346 | * Set dependency. |
||
347 | * |
||
348 | * @param string $className Fully qualified class name |
||
349 | * @param string $alias Dependency name |
||
350 | * @param array $parameters Collection of parameters needed for dependency creation |
||
351 | * |
||
352 | * @return self Chaining |
||
353 | */ |
||
354 | 4 | public function set($className, $alias = null, array $parameters = array()) |
|
368 | } |
||
369 |