1 | <?php |
||
21 | class Container implements ContainerInterface |
||
22 | { |
||
23 | /** @var array[string] Collection of loaded services */ |
||
24 | protected $services = array(); |
||
25 | |||
26 | /** @var array[string] Collection of alias => class name for alias resolving*/ |
||
27 | protected $aliases = array(); |
||
28 | |||
29 | /** @var array[string] Collection of class name dependencies trees */ |
||
30 | protected $dependencies = array(); |
||
31 | |||
32 | /** |
||
33 | * Get reflection paramater class name type hint if present without |
||
34 | * autoloading and throwing exceptions. |
||
35 | * |
||
36 | * @param \ReflectionParameter $param Parameter for parsing |
||
37 | * |
||
38 | * @return string|null Class name typehint or null |
||
39 | */ |
||
40 | 1 | protected function getClassName(\ReflectionParameter $param) |
|
47 | |||
48 | /** |
||
49 | * Recursively build class constructor dependencies tree. |
||
50 | * TODO: Analyze recurrent dependencies and throw an error |
||
51 | * |
||
52 | * @param string $className Current class name for analyzing |
||
53 | * @param array $dependencies Reference to tree for filling up |
||
54 | * |
||
55 | * @return array [string] Multidimensional array as dependency tree |
||
56 | * @throws ClassNotFoundException |
||
57 | */ |
||
58 | 1 | protected function buildDependenciesTree($className, array &$dependencies) |
|
92 | |||
93 | /** |
||
94 | * Finds an entry of the container by its identifier and returns it. |
||
95 | * |
||
96 | * @param string $alias Identifier of the entry to look for. |
||
97 | * |
||
98 | * @throws NotFoundException No entry was found for this identifier. |
||
99 | * @throws ContainerException Error while retrieving the entry. |
||
100 | * |
||
101 | * @return mixed Entry. |
||
102 | */ |
||
103 | public function get($alias) |
||
118 | |||
119 | /** |
||
120 | * Returns true if the container can return an entry for the given identifier. |
||
121 | * Returns false otherwise. |
||
122 | * |
||
123 | * @param string $alias Identifier of the entry to look for. |
||
124 | * |
||
125 | * @return boolean |
||
126 | */ |
||
127 | public function has($alias) |
||
132 | |||
133 | /** |
||
134 | * Set dependency alias with callback function. |
||
135 | * |
||
136 | * @param callable $callable Callable to return dependency |
||
137 | * @param string $alias Dependency name |
||
138 | * |
||
139 | * @return self Chaining |
||
140 | */ |
||
141 | public function callback($callable, $alias = null) |
||
145 | |||
146 | /** |
||
147 | * Set service dependency. Upon first creation of this class instance |
||
148 | * it would be used everywhere where this dependency is needed. |
||
149 | * |
||
150 | * @param string $className Fully qualified class name |
||
151 | * @param string $alias Dependency name |
||
152 | * @param array $parameters Collection of parameters needed for dependency creation |
||
153 | * |
||
154 | * @return self Chaining |
||
155 | */ |
||
156 | public function service($className, $alias = null, array $parameters = array()) |
||
160 | |||
161 | /** |
||
162 | * Set service dependency by passing object instance. |
||
163 | * |
||
164 | * @param mixed $instance Instance that needs to be return by this dependency |
||
165 | * @param string $alias Dependency name |
||
166 | * @param array $parameters Collection of parameters needed for dependency creation |
||
167 | * |
||
168 | * @return self Chaining |
||
169 | */ |
||
170 | public function instance(&$instance, $alias = null, array $parameters = array()) |
||
175 | |||
176 | /** |
||
177 | * Set dependency. |
||
178 | * |
||
179 | * @param string $className Fully qualified class name |
||
180 | * @param string $alias Dependency name |
||
181 | * @param array $parameters Collection of parameters needed for dependency creation |
||
182 | * |
||
183 | * @return ContainerInterface Chaining |
||
184 | */ |
||
185 | 1 | public function set($className, $alias = null, array $parameters = array()) |
|
201 | } |
||
202 |