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) |
|
92 | 46 | ||
93 | 46 | /** |
|
94 | * Adds a definition or a value to the container |
||
95 | 50 | * |
|
96 | * @param string $name |
||
97 | * @param mixed $definition |
||
98 | * @param Scope|string $scope Resolving scope |
||
99 | * @param array $parameters Used if $value is a callable |
||
100 | * |
||
101 | * @return Container |
||
102 | */ |
||
103 | public function register( |
||
118 | |||
119 | /** |
||
120 | * Resolves the definition that was saved under the provided name |
||
121 | * |
||
122 | * @param string $name |
||
123 | * |
||
124 | * @return mixed |
||
125 | */ |
||
126 | protected function resolve($name) |
||
134 | |||
135 | /** |
||
136 | * Checks the definition scope to register resolution result |
||
137 | 74 | * |
|
138 | 72 | * If scope is set to prototype the the resolution result is not |
|
139 | 32 | * stores in the container instances. |
|
140 | 32 | * |
|
141 | 32 | * @param string $name |
|
142 | 32 | * @param DefinitionInterface $definition |
|
143 | 32 | * @return mixed |
|
144 | 32 | */ |
|
145 | 32 | protected function registerEntry($name, DefinitionInterface $definition) |
|
153 | 72 | ||
154 | /** |
||
155 | 72 | * Adds a definition to the definitions list |
|
156 | * |
||
157 | 72 | * @param string $name |
|
158 | 72 | * @param DefinitionInterface $definition |
|
159 | * |
||
160 | * @return Container |
||
161 | */ |
||
162 | protected function add($name, DefinitionInterface $definition) |
||
168 | |||
169 | /** |
||
170 | * Creates the definition for registered data |
||
171 | * |
||
172 | * If value is a callable then the definition is Factory, otherwise |
||
173 | * it will create a Value definition. |
||
174 | * |
||
175 | 10 | * @see Factory, Value |
|
176 | * |
||
177 | * @param callable|mixed $value |
||
178 | 10 | * @param array $parameters |
|
179 | 2 | * |
|
180 | * @return Factory|Value |
||
181 | 2 | */ |
|
182 | protected function createDefinition( |
||
191 | |||
192 | /** |
||
193 | * Creates a definition for provided name and value pair |
||
194 | * |
||
195 | * If $value is a string prefixed with '@' it will create an Alias |
||
196 | * definition. Otherwise a Value definition will be created. |
||
197 | * |
||
198 | 46 | * @param mixed $value |
|
199 | * |
||
200 | 46 | * @return Value|Alias |
|
201 | 46 | */ |
|
202 | 46 | protected function createValueDefinition($value) |
|
210 | |||
211 | /** |
||
212 | * Creates an instance of provided class injecting its dependencies |
||
213 | * |
||
214 | * @param string $className |
||
215 | * @param array ...$arguments |
||
216 | 70 | * |
|
217 | * @return mixed |
||
218 | 70 | */ |
|
219 | 70 | public function make($className, ...$arguments) |
|
233 | |||
234 | 72 | /** |
|
235 | * Set the object hydrator |
||
236 | 72 | * |
|
237 | 72 | * @param ObjectHydratorInterface $hydrator |
|
238 | * |
||
239 | * @return Container|ObjectHydratorAwareInterface |
||
240 | */ |
||
241 | public function setHydrator(ObjectHydratorInterface $hydrator) |
||
246 | |||
247 | /** |
||
248 | * Get the object hydrator |
||
249 | * |
||
250 | 32 | * @return ObjectHydratorInterface |
|
251 | */ |
||
252 | public function getHydrator() |
||
259 | |||
260 | /** |
||
261 | * Gets the parent container if it exists |
||
262 | * |
||
263 | * @return null|ContainerInterface |
||
264 | */ |
||
265 | 50 | public function parent() |
|
269 | } |
||
270 |
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.