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 | 1 | protected $generator; |
|
40 | |||
41 | 1 | public function __construct(Generator $generator) |
|
45 | |||
46 | /** |
||
47 | * Internal logic handler. Calls generated logic function |
||
48 | * for performing entity creation or search. This is encapsulated |
||
49 | * method for further overriding. |
||
50 | * |
||
51 | * @param string $alias Entity alias |
||
52 | 1 | * |
|
53 | * @return mixed Created instance or null |
||
54 | 1 | */ |
|
55 | 1 | protected function logic($alias) |
|
59 | |||
60 | /** |
||
61 | * Get reflection paramater class name type hint if present without |
||
62 | * auto loading and throwing exceptions. |
||
63 | * |
||
64 | * @param \ReflectionParameter $param Parameter for parsing |
||
65 | * |
||
66 | * @return string|null Class name typehint or null |
||
67 | */ |
||
68 | protected function getClassName(\ReflectionParameter $param) |
||
75 | |||
76 | 1 | /** |
|
77 | 1 | * Recursively build class constructor dependencies tree. |
|
78 | * TODO: Analyze recurrent dependencies and throw an exception |
||
79 | 1 | * |
|
80 | * @param string $className Current class name for analyzing |
||
81 | 1 | * @param array $dependencies Reference to tree for filling up |
|
82 | * |
||
83 | 1 | * @return array [string] Multidimensional array as dependency tree |
|
84 | * @throws ClassNotFoundException |
||
85 | */ |
||
86 | 1 | protected function buildDependenciesTree($className, array &$dependencies) |
|
125 | 1 | ||
126 | 1 | /** |
|
127 | 1 | * Recursive object creation with dependencies. |
|
128 | * |
||
129 | * @param array $dependencies Collection of current class dependenices |
||
130 | 1 | * @param string $class Current class name |
|
131 | 1 | * |
|
132 | 1 | * @throws ConstructorParameterNotSetException |
|
133 | 1 | */ |
|
134 | 1 | ||
135 | public function generateLogicConditions(array $dependencies, $class) |
||
172 | |||
173 | 1 | /** |
|
174 | 1 | * @param string $functionName |
|
175 | * |
||
176 | * @return string |
||
177 | 1 | * @throws ConstructorParameterNotSetException |
|
178 | */ |
||
179 | 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 | 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 | 1 | * @return self Chaining |
|
275 | */ |
||
276 | public function service($className, $alias = null, array $parameters = array()) |
||
280 | 1 | ||
281 | /** |
||
282 | * Set service dependency by passing object instance. |
||
283 | 1 | * |
|
284 | * @param mixed $instance Instance that needs to be return by this dependency |
||
285 | * @param string $alias Dependency name |
||
286 | 1 | * @param array $parameters Collection of parameters needed for dependency creation |
|
287 | 1 | * |
|
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 | public function set($className, $alias = null, array $parameters = array()) |
||
319 | } |
||
320 |