1 | <?php |
||
26 | class Container implements ContainerInterface, ObjectHydratorAwareInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $definitions = []; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected static $instances = []; |
||
37 | |||
38 | /** |
||
39 | * @var ObjectHydratorInterface |
||
40 | */ |
||
41 | protected $hydrator; |
||
42 | |||
43 | /** |
||
44 | * @var null|ContainerInterface |
||
45 | */ |
||
46 | protected $parent; |
||
47 | |||
48 | /** |
||
49 | * Creates a dependency container |
||
50 | */ |
||
51 | public function __construct() |
||
59 | 72 | ||
60 | 72 | /** |
|
61 | * Finds an entry of the container by its identifier and returns it. |
||
62 | 74 | * |
|
63 | 74 | * @param string $id Identifier of the entry to look for. |
|
64 | 74 | * |
|
65 | 74 | * @throws NotFoundException No entry was found for this identifier. |
|
66 | 74 | * @throws ContainerException Error while retrieving the entry. |
|
67 | * |
||
68 | * @return mixed Entry. |
||
69 | */ |
||
70 | public function get($id) |
||
79 | |||
80 | 52 | /** |
|
81 | 2 | * Returns true if the container can return an entry for the given |
|
82 | 2 | * identifier. Returns false otherwise. |
|
83 | * |
||
84 | 2 | * @param string $id Identifier of the entry to look for. |
|
85 | * |
||
86 | * @return boolean |
||
87 | 50 | */ |
|
88 | 50 | public function has($id) |
|
96 | |||
97 | /** |
||
98 | * Adds a definition or a value to the container |
||
99 | * |
||
100 | * @param string $name |
||
101 | * @param mixed $definition |
||
102 | * @param Scope|string $scope Resolving scope |
||
103 | * @param array $parameters Used if $value is a callable |
||
104 | * |
||
105 | * @return Container |
||
106 | 56 | */ |
|
107 | public function register( |
||
122 | |||
123 | /** |
||
124 | * Checks if parent has a provided key |
||
125 | * |
||
126 | * @param string $key |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | protected function parentHas($key) |
||
137 | 74 | ||
138 | 72 | /** |
|
139 | 32 | * Resolves the definition that was saved under the provided name |
|
140 | 32 | * |
|
141 | 32 | * @param string $name |
|
142 | 32 | * |
|
143 | 32 | * @return mixed |
|
144 | 32 | */ |
|
145 | 32 | protected function resolve($name) |
|
158 | 72 | ||
159 | /** |
||
160 | * Checks the definition scope to register resolution result |
||
161 | * |
||
162 | * If scope is set to prototype the the resolution result is not |
||
163 | * stores in the container instances. |
||
164 | * |
||
165 | * @param string $name |
||
166 | * @param DefinitionInterface $definition |
||
167 | * @return mixed |
||
168 | */ |
||
169 | protected function registerEntry($name, DefinitionInterface $definition) |
||
177 | |||
178 | 10 | /** |
|
179 | 2 | * Adds a definition to the definitions list |
|
180 | * |
||
181 | 2 | * This method does not override an existing entry if the same name exists |
|
182 | * in the definitions or in any definitions of its parents. |
||
183 | * This way it is possible to change entries defined by other packages |
||
184 | 8 | * as those are build after the main application container is build. |
|
185 | 4 | * The main application container should be the first to be created and |
|
186 | 4 | * therefore set any entry that will override the latest containers build. |
|
187 | * |
||
188 | 8 | * @param string $name |
|
189 | * @param DefinitionInterface $definition |
||
190 | * |
||
191 | * @return Container |
||
192 | */ |
||
193 | protected function add($name, DefinitionInterface $definition) |
||
203 | 46 | ||
204 | 46 | /** |
|
205 | 46 | * Creates the definition for registered data |
|
206 | 46 | * |
|
207 | * If value is a callable then the definition is Factory, otherwise |
||
208 | * it will create a Value definition. |
||
209 | * |
||
210 | * @see Factory, Value |
||
211 | * |
||
212 | * @param callable|mixed $value |
||
213 | * @param array $parameters |
||
214 | * |
||
215 | * @return Factory|Value |
||
216 | 70 | */ |
|
217 | protected function createDefinition( |
||
226 | |||
227 | /** |
||
228 | * Creates a definition for provided name and value pair |
||
229 | * |
||
230 | 72 | * If $value is a string prefixed with '@' it will create an Alias |
|
231 | * definition. Otherwise a Value definition will be created. |
||
232 | 72 | * |
|
233 | * @param mixed $value |
||
234 | 72 | * |
|
235 | * @return Value|Alias |
||
236 | 72 | */ |
|
237 | 72 | protected function createValueDefinition($value) |
|
245 | |||
246 | /** |
||
247 | * Creates an instance of provided class injecting its dependencies |
||
248 | * |
||
249 | * @param string $className |
||
250 | 32 | * @param array ...$arguments |
|
251 | * |
||
252 | * @return mixed |
||
253 | 32 | */ |
|
254 | public function make($className, ...$arguments) |
||
268 | 8 | ||
269 | /** |
||
270 | 46 | * Set the object hydrator |
|
271 | * |
||
272 | * @param ObjectHydratorInterface $hydrator |
||
273 | * |
||
274 | * @return Container|ObjectHydratorAwareInterface |
||
275 | */ |
||
276 | public function setHydrator(ObjectHydratorInterface $hydrator) |
||
281 | 46 | ||
282 | 46 | /** |
|
283 | * Get the object hydrator |
||
284 | 46 | * |
|
285 | 14 | * @return ObjectHydratorInterface |
|
286 | 14 | */ |
|
287 | 46 | public function getHydrator() |
|
294 | |||
295 | /** |
||
296 | * Gets the parent container if it exists |
||
297 | * |
||
298 | * @return null|ContainerInterface |
||
299 | 4 | */ |
|
300 | public function parent() |
||
304 | } |
||
305 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.