1 | <?php declare(strict_types = 1); |
||
21 | abstract class AbstractContainer implements ContainerContract |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Array of callable definitions. |
||
26 | * |
||
27 | * @var Invokable[] |
||
28 | */ |
||
29 | protected $callableDefinitions = []; |
||
30 | |||
31 | /** |
||
32 | * Array of class definitions. |
||
33 | * |
||
34 | * @var string[] |
||
35 | */ |
||
36 | protected $classDefinitions = []; |
||
37 | |||
38 | /** |
||
39 | * Array of resolved instances. |
||
40 | * |
||
41 | * @var object[] |
||
42 | */ |
||
43 | protected $instances = []; |
||
44 | |||
45 | /** |
||
46 | * Array of container service identifiers. |
||
47 | * |
||
48 | * @var string[] |
||
49 | */ |
||
50 | protected $keys = []; |
||
51 | |||
52 | /** |
||
53 | * Array of instances identifiers marked as shared. |
||
54 | * Such instances will be instantiated once and returned on consecutive gets. |
||
55 | * |
||
56 | * @var bool[] |
||
57 | */ |
||
58 | protected $shared = []; |
||
59 | |||
60 | /** |
||
61 | * Array of container service callable factories. |
||
62 | * |
||
63 | * @var Closure[] |
||
64 | */ |
||
65 | private $factories = []; |
||
66 | |||
67 | /** |
||
68 | * @var InvokerContract |
||
69 | */ |
||
70 | private $invoker; |
||
71 | |||
72 | /** |
||
73 | * Array of container service identifiers currently being resolved. |
||
74 | * |
||
75 | * @var string[] |
||
76 | */ |
||
77 | private $resolving = []; |
||
78 | |||
79 | /** |
||
80 | * Container constructor. |
||
81 | * |
||
82 | * @param ArgumentResolverContract|null $resolver |
||
83 | */ |
||
84 | 31 | public function __construct(ArgumentResolverContract $resolver = null) |
|
88 | |||
89 | /** |
||
90 | * @inheritDoc |
||
91 | */ |
||
92 | 27 | public function get($id, array $arguments = []) |
|
106 | |||
107 | /** |
||
108 | * @inheritDoc |
||
109 | */ |
||
110 | 16 | public function has($id): bool |
|
114 | |||
115 | /** |
||
116 | * Create callable factory for the subject service. |
||
117 | * |
||
118 | * @param string $id |
||
119 | * @param array $arguments |
||
120 | * @return mixed |
||
121 | * @throws NotFoundException |
||
122 | */ |
||
123 | 27 | protected function instantiateService(string $id, array $arguments) |
|
151 | |||
152 | /** |
||
153 | * @return InvokerContract |
||
154 | */ |
||
155 | 31 | protected function invoker(): InvokerContract |
|
159 | |||
160 | /** |
||
161 | * Check if container can resolve the service with subject identifier. |
||
162 | * |
||
163 | * @param string $id |
||
164 | * @return bool |
||
165 | */ |
||
166 | 22 | protected function isResolvableService(string $id): bool |
|
170 | |||
171 | /** |
||
172 | * @param string $id |
||
173 | * @return bool |
||
174 | */ |
||
175 | 22 | protected function isShared(string $id): bool |
|
179 | |||
180 | /** |
||
181 | * Normalize key to use across container. |
||
182 | * |
||
183 | * @param string $id |
||
184 | * @return string |
||
185 | */ |
||
186 | 31 | protected function normalize(string $id): string |
|
190 | |||
191 | /** |
||
192 | * @param string $id |
||
193 | * @return void |
||
194 | */ |
||
195 | 27 | protected function resolved(string $id) |
|
199 | |||
200 | /** |
||
201 | * Detects circular references. |
||
202 | * |
||
203 | * @param string $id |
||
204 | * @return void |
||
205 | * @throws CircularReferenceException |
||
206 | */ |
||
207 | 27 | protected function resolving(string $id) |
|
216 | |||
217 | /** |
||
218 | * @param InvokerContract $invoker |
||
219 | * @return void |
||
220 | */ |
||
221 | 31 | protected function setInvoker(InvokerContract $invoker) |
|
225 | |||
226 | /** |
||
227 | * Forbid container cloning. |
||
228 | * |
||
229 | * @codeCoverageIgnore |
||
230 | */ |
||
231 | private function __clone() |
||
234 | |||
235 | /** |
||
236 | * Create callable factory with resolved arguments from callable. |
||
237 | * |
||
238 | * @param Invokable $invokable |
||
239 | * @return Closure |
||
240 | */ |
||
241 | 12 | private function createServiceFactoryFromCallable(Invokable $invokable): Closure |
|
247 | |||
248 | /** |
||
249 | * Create callable factory with resolved arguments from class name. |
||
250 | * |
||
251 | * @param string $class |
||
252 | * @return Closure |
||
253 | * @throws UninstantiableServiceException |
||
254 | */ |
||
255 | 17 | private function createServiceFactoryFromClass(string $class): Closure |
|
275 | |||
276 | /** |
||
277 | * @param string $id |
||
278 | * @param array $arguments |
||
279 | * @return object |
||
280 | */ |
||
281 | 23 | private function invokeFactory(string $id, array $arguments) |
|
285 | |||
286 | /** |
||
287 | * @param string $id |
||
288 | * @param object $object |
||
289 | * @return object |
||
290 | */ |
||
291 | 20 | private function saveShared(string $id, $object) |
|
299 | |||
300 | } |
||
301 |