1 | <?php |
||
26 | class Container implements ContainerInterface |
||
27 | { |
||
28 | const LOGIC_FUNCTION_NAME = 'diContainer'; |
||
29 | |||
30 | /** @var array[string] Collection of loaded services */ |
||
31 | protected $services = array(); |
||
32 | |||
33 | /** @var array[string] Collection of alias => class name for alias resolving */ |
||
34 | protected $aliases = array(); |
||
35 | |||
36 | /** @var array[string] Collection of class name dependencies trees */ |
||
37 | protected $dependencies = array(); |
||
38 | |||
39 | /** @var Generator */ |
||
40 | protected $generator; |
||
41 | |||
42 | /** |
||
43 | * Container constructor. |
||
44 | * |
||
45 | * @param Generator $generator |
||
46 | */ |
||
47 | 3 | public function __construct(Generator $generator) |
|
51 | |||
52 | /** |
||
53 | * Internal logic handler. Calls generated logic function |
||
54 | * for performing entity creation or search. This is encapsulated |
||
55 | * method for further overriding. |
||
56 | * |
||
57 | * @param string $alias Entity alias |
||
58 | * |
||
59 | * @return mixed Created instance or null |
||
60 | */ |
||
61 | 2 | protected function logic($alias) |
|
62 | { |
||
63 | 2 | return call_user_func(self::LOGIC_FUNCTION_NAME, $alias); |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * Get reflection paramater class name type hint if present without |
||
68 | * auto loading and throwing exceptions. |
||
69 | * |
||
70 | * @param \ReflectionParameter $param Parameter for parsing |
||
71 | * |
||
72 | * @return string|null Class name typehint or null |
||
73 | */ |
||
74 | 3 | protected function getClassName(\ReflectionParameter $param) |
|
81 | |||
82 | /** |
||
83 | * Recursively build class constructor dependencies tree. |
||
84 | * TODO: Analyze recurrent dependencies and throw an exception |
||
85 | * |
||
86 | * @param string $className Current class name for analyzing |
||
87 | * @param array $dependencies Reference to tree for filling up |
||
88 | * |
||
89 | * @return array [string] Multidimensional array as dependency tree |
||
90 | * @throws ClassNotFoundException |
||
91 | */ |
||
92 | 3 | protected function buildDependenciesTree($className, array &$dependencies) |
|
128 | |||
129 | /** |
||
130 | * Recursive object creation with dependencies. |
||
131 | * |
||
132 | * @param array $dependencies Collection of current class dependenices |
||
133 | * @param string $class Current class name |
||
134 | * |
||
135 | * @throws ConstructorParameterNotSetException |
||
136 | */ |
||
137 | |||
138 | 1 | public function generateLogicConditions(array &$dependencies, $class) |
|
183 | |||
184 | /** |
||
185 | * @param string $functionName |
||
186 | * |
||
187 | * @return string |
||
188 | * @throws ConstructorParameterNotSetException |
||
189 | */ |
||
190 | 1 | public function generateLogicFunction($functionName = self::LOGIC_FUNCTION_NAME) |
|
221 | |||
222 | /** |
||
223 | * Finds an entry of the container by its identifier and returns it. |
||
224 | * |
||
225 | * @param string $alias Identifier of the entry to look for. |
||
226 | * |
||
227 | * @throws NotFoundException No entry was found for this identifier. |
||
228 | * @throws ContainerException Error while retrieving the entry. |
||
229 | * |
||
230 | * @return mixed Entry. |
||
231 | */ |
||
232 | 2 | public function get($alias) |
|
247 | |||
248 | /** |
||
249 | * Returns true if the container can return an entry for the given identifier. |
||
250 | * Returns false otherwise. |
||
251 | * |
||
252 | * @param string $alias Identifier of the entry to look for. |
||
253 | * |
||
254 | * @return boolean |
||
255 | */ |
||
256 | 1 | public function has($alias) |
|
262 | |||
263 | /** |
||
264 | * Set dependency alias with callback function. |
||
265 | * |
||
266 | * @param callable $callable Callable to return dependency |
||
267 | * @param string $alias Dependency name |
||
268 | * |
||
269 | * @return self Chaining |
||
270 | */ |
||
271 | public function callback($callable, $alias = null) |
||
275 | |||
276 | /** |
||
277 | * Set service dependency. Upon first creation of this class instance |
||
278 | * it would be used everywhere where this dependency is needed. |
||
279 | * |
||
280 | * @param string $className Fully qualified class name |
||
281 | * @param string $alias Dependency name |
||
282 | * @param array $parameters Collection of parameters needed for dependency creation |
||
283 | * |
||
284 | * @return self Chaining |
||
285 | */ |
||
286 | 3 | public function service($className, $alias = null, array $parameters = array()) |
|
292 | |||
293 | /** |
||
294 | * Set service dependency by passing object instance. |
||
295 | * |
||
296 | * @param mixed $instance Instance that needs to be return by this dependency |
||
297 | * @param string $alias Dependency name |
||
298 | * @param array $parameters Collection of parameters needed for dependency creation |
||
299 | * |
||
300 | * @return self Chaining |
||
301 | */ |
||
302 | public function instance(&$instance, $alias = null, array $parameters = array()) |
||
307 | |||
308 | /** |
||
309 | * Set dependency. |
||
310 | * |
||
311 | * @param string $className Fully qualified class name |
||
312 | * @param string $alias Dependency name |
||
313 | * @param array $parameters Collection of parameters needed for dependency creation |
||
314 | * |
||
315 | * @return self Chaining |
||
316 | */ |
||
317 | 3 | public function set($className, $alias = null, array $parameters = array()) |
|
331 | } |
||
332 |