1 | <?php |
||
27 | class Container implements ContainerInterface, ObjectHydratorAwareInterface |
||
28 | { |
||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $definitions = []; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected static $instances = []; |
||
38 | |||
39 | /** |
||
40 | * @var ObjectHydratorInterface |
||
41 | */ |
||
42 | protected $hydrator; |
||
43 | |||
44 | /** |
||
45 | * @var null|ContainerInterface |
||
46 | */ |
||
47 | protected $parent; |
||
48 | |||
49 | /** |
||
50 | * Creates a dependency container |
||
51 | */ |
||
52 | public function __construct() |
||
60 | 72 | ||
61 | /** |
||
62 | 74 | * Finds an entry of the container by its identifier and returns it. |
|
63 | 74 | * |
|
64 | 74 | * @param string $id Identifier of the entry to look for. |
|
65 | 74 | * |
|
66 | 74 | * @throws NotFoundException No entry was found for this identifier. |
|
67 | * @throws ContainerException Error while retrieving the entry. |
||
68 | * |
||
69 | * @return mixed Entry. |
||
70 | */ |
||
71 | public function get($id) |
||
80 | 52 | ||
81 | 2 | /** |
|
82 | 2 | * Returns true if the container can return an entry for the given |
|
83 | * identifier. Returns false otherwise. |
||
84 | 2 | * |
|
85 | * @param string $id Identifier of the entry to look for. |
||
86 | * |
||
87 | 50 | * @return boolean |
|
88 | 50 | */ |
|
89 | 50 | public function has($id) |
|
97 | |||
98 | /** |
||
99 | * Adds a definition or a value to the container |
||
100 | * |
||
101 | * @param string $name |
||
102 | * @param mixed $definition |
||
103 | * @param Scope|string $scope Resolving scope |
||
104 | * @param array $parameters Used if $value is a callable |
||
105 | * |
||
106 | 56 | * @return Container |
|
107 | */ |
||
108 | 56 | public function register( |
|
123 | |||
124 | /** |
||
125 | * Checks if parent has a provided key |
||
126 | * |
||
127 | * @param string $key |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | protected function parentHas($key) |
||
138 | 72 | ||
139 | 32 | /** |
|
140 | 32 | * Resolves the definition that was saved under the provided name |
|
141 | 32 | * |
|
142 | 32 | * @param string $name |
|
143 | 32 | * |
|
144 | 32 | * @return mixed |
|
145 | 32 | */ |
|
146 | protected function resolve($name) |
||
159 | |||
160 | /** |
||
161 | * Checks the definition scope to register resolution result |
||
162 | * |
||
163 | * If scope is set to prototype the the resolution result is not |
||
164 | * stores in the container instances. |
||
165 | * |
||
166 | * @param string $name |
||
167 | * @param DefinitionInterface $definition |
||
168 | * @return mixed |
||
169 | */ |
||
170 | protected function registerEntry($name, DefinitionInterface $definition) |
||
180 | |||
181 | 2 | /** |
|
182 | * Adds a definition to the definitions list |
||
183 | * |
||
184 | 8 | * This method does not override an existing entry if the same name exists |
|
185 | 4 | * in the definitions or in any definitions of its parents. |
|
186 | 4 | * This way it is possible to change entries defined by other packages |
|
187 | * as those are build after the main application container is build. |
||
188 | 8 | * The main application container should be the first to be created and |
|
189 | * therefore set any entry that will override the latest containers build. |
||
190 | * |
||
191 | * @param string $name |
||
192 | * @param DefinitionInterface $definition |
||
193 | * |
||
194 | * @return Container |
||
195 | */ |
||
196 | protected function add($name, DefinitionInterface $definition) |
||
206 | 46 | ||
207 | /** |
||
208 | * Creates the definition for registered data |
||
209 | * |
||
210 | * If value is a callable then the definition is Factory, otherwise |
||
211 | * it will create a Value definition. |
||
212 | * |
||
213 | * @see Factory, Value |
||
214 | * |
||
215 | * @param callable|mixed $value |
||
216 | 70 | * @param array $parameters |
|
217 | * |
||
218 | 70 | * @return Factory|Value |
|
219 | 70 | */ |
|
220 | protected function createDefinition( |
||
229 | |||
230 | 72 | /** |
|
231 | * Creates a definition for provided name and value pair |
||
232 | 72 | * |
|
233 | * If $value is a string prefixed with '@' it will create an Alias |
||
234 | 72 | * definition. Otherwise a Value definition will be created. |
|
235 | * |
||
236 | 72 | * @param mixed $value |
|
237 | 72 | * |
|
238 | * @return Value|Alias |
||
239 | */ |
||
240 | protected function createValueDefinition($value) |
||
248 | |||
249 | /** |
||
250 | 32 | * Creates an instance of provided class injecting its dependencies |
|
251 | * |
||
252 | * @param string $className |
||
253 | 32 | * @param array ...$arguments |
|
254 | * |
||
255 | 32 | * @return mixed |
|
256 | 32 | */ |
|
257 | public function make($className, ...$arguments) |
||
278 | |||
279 | 46 | /** |
|
280 | * Set the object hydrator |
||
281 | 46 | * |
|
282 | 46 | * @param ObjectHydratorInterface $hydrator |
|
283 | * |
||
284 | 46 | * @return Container|ObjectHydratorAwareInterface |
|
285 | 14 | */ |
|
286 | 14 | public function setHydrator(ObjectHydratorInterface $hydrator) |
|
291 | |||
292 | /** |
||
293 | * Get the object hydrator |
||
294 | * |
||
295 | * @return ObjectHydratorInterface |
||
296 | */ |
||
297 | public function getHydrator() |
||
304 | 2 | ||
305 | 2 | /** |
|
306 | 4 | * Gets the parent container if it exists |
|
307 | 4 | * |
|
308 | 4 | * @return null|ContainerInterface |
|
309 | */ |
||
310 | public function parent() |
||
314 | |||
315 | /** |
||
316 | * Get the top container |
||
317 | 18 | * |
|
318 | * @return container |
||
319 | 18 | */ |
|
320 | 10 | private function container() |
|
324 | } |
||
325 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: