1 | <?php |
||
25 | class Container implements ContainerInterface |
||
26 | { |
||
27 | const LOGIC_FUNCTION_NAME = 'diContainer'; |
||
28 | |||
29 | /** @var array[string] Collection of loaded services */ |
||
30 | protected $services = array(); |
||
31 | |||
32 | /** @var array[string] Collection of alias => class name for alias resolving */ |
||
33 | protected $aliases = array(); |
||
34 | |||
35 | /** @var array[string] Collection of class name dependencies trees */ |
||
36 | protected $dependencies = array(); |
||
37 | |||
38 | /** @var Generator */ |
||
39 | protected $generator; |
||
40 | |||
41 | /** |
||
42 | * Container constructor. |
||
43 | * |
||
44 | * @param Generator $generator |
||
45 | */ |
||
46 | 1 | public function __construct(Generator $generator) |
|
47 | { |
||
48 | 1 | $this->generator = $generator; |
|
49 | 1 | } |
|
50 | |||
51 | /** |
||
52 | * Internal logic handler. Calls generated logic function |
||
53 | * for performing entity creation or search. This is encapsulated |
||
54 | * method for further overriding. |
||
55 | * |
||
56 | * @param string $alias Entity alias |
||
57 | * |
||
58 | * @return mixed Created instance or null |
||
59 | */ |
||
60 | protected function logic($alias) |
||
61 | { |
||
62 | return call_user_func(self::LOGIC_FUNCTION_NAME, $alias); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Get reflection paramater class name type hint if present without |
||
67 | * auto loading and throwing exceptions. |
||
68 | * |
||
69 | * @param \ReflectionParameter $param Parameter for parsing |
||
70 | * |
||
71 | * @return string|null Class name typehint or null |
||
72 | */ |
||
73 | 1 | protected function getClassName(\ReflectionParameter $param) |
|
80 | |||
81 | /** |
||
82 | * Recursively build class constructor dependencies tree. |
||
83 | * TODO: Analyze recurrent dependencies and throw an exception |
||
84 | * |
||
85 | * @param string $className Current class name for analyzing |
||
86 | * @param array $dependencies Reference to tree for filling up |
||
87 | * |
||
88 | * @return array [string] Multidimensional array as dependency tree |
||
89 | * @throws ClassNotFoundException |
||
90 | */ |
||
91 | 1 | protected function buildDependenciesTree($className, array &$dependencies) |
|
127 | |||
128 | /** |
||
129 | * Recursive object creation with dependencies. |
||
130 | * |
||
131 | * @param array $dependencies Collection of current class dependenices |
||
132 | * @param string $class Current class name |
||
133 | * |
||
134 | * @throws ConstructorParameterNotSetException |
||
135 | */ |
||
136 | |||
137 | 1 | public function generateLogicConditions(array &$dependencies, $class) |
|
174 | |||
175 | /** |
||
176 | * @param string $functionName |
||
177 | * |
||
178 | * @return string |
||
179 | * @throws ConstructorParameterNotSetException |
||
180 | */ |
||
181 | 1 | public function generateLogicFunction($functionName = self::LOGIC_FUNCTION_NAME) |
|
212 | |||
213 | /** |
||
214 | * Finds an entry of the container by its identifier and returns it. |
||
215 | * |
||
216 | * @param string $alias Identifier of the entry to look for. |
||
217 | * |
||
218 | * @throws NotFoundException No entry was found for this identifier. |
||
219 | * @throws ContainerException Error while retrieving the entry. |
||
220 | * |
||
221 | * @return mixed Entry. |
||
222 | */ |
||
223 | 1 | public function get($alias) |
|
238 | |||
239 | /** |
||
240 | * Returns true if the container can return an entry for the given identifier. |
||
241 | * Returns false otherwise. |
||
242 | * |
||
243 | * @param string $alias Identifier of the entry to look for. |
||
244 | * |
||
245 | * @return boolean |
||
246 | */ |
||
247 | public function has($alias) |
||
252 | |||
253 | /** |
||
254 | * Set dependency alias with callback function. |
||
255 | * |
||
256 | * @param callable $callable Callable to return dependency |
||
257 | * @param string $alias Dependency name |
||
258 | * |
||
259 | * @return self Chaining |
||
260 | */ |
||
261 | public function callback($callable, $alias = null) |
||
265 | |||
266 | /** |
||
267 | * Set service dependency. Upon first creation of this class instance |
||
268 | * it would be used everywhere where this dependency is needed. |
||
269 | * |
||
270 | * @param string $className Fully qualified class name |
||
271 | * @param string $alias Dependency name |
||
272 | * @param array $parameters Collection of parameters needed for dependency creation |
||
273 | * |
||
274 | * @return self Chaining |
||
275 | */ |
||
276 | public function service($className, $alias = null, array $parameters = array()) |
||
280 | |||
281 | /** |
||
282 | * Set service dependency by passing object instance. |
||
283 | * |
||
284 | * @param mixed $instance Instance that needs to be return by this dependency |
||
285 | * @param string $alias Dependency name |
||
286 | * @param array $parameters Collection of parameters needed for dependency creation |
||
287 | * |
||
288 | * @return self Chaining |
||
289 | */ |
||
290 | public function instance(&$instance, $alias = null, array $parameters = array()) |
||
295 | |||
296 | /** |
||
297 | * Set dependency. |
||
298 | * |
||
299 | * @param string $className Fully qualified class name |
||
300 | * @param string $alias Dependency name |
||
301 | * @param array $parameters Collection of parameters needed for dependency creation |
||
302 | * |
||
303 | * @return ContainerInterface Chaining |
||
304 | */ |
||
305 | 1 | public function set($className, $alias = null, array $parameters = array()) |
|
319 | } |
||
320 |