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 | 44 | public function __construct(ArgumentResolverContract $resolver = null) |
|
88 | |||
89 | /** |
||
90 | * @inheritDoc |
||
91 | * @param callable|string $callable Callable to call OR class name to instantiate and invoke. |
||
92 | */ |
||
93 | 12 | public function call($callable, array $arguments = []) |
|
97 | |||
98 | /** |
||
99 | * @inheritDoc |
||
100 | */ |
||
101 | 36 | public function get($id, array $arguments = []) |
|
115 | |||
116 | /** |
||
117 | * @inheritDoc |
||
118 | */ |
||
119 | 24 | public function has($id): bool |
|
123 | |||
124 | /** |
||
125 | * @inheritDoc |
||
126 | */ |
||
127 | 1 | public function isCallable($callable): bool |
|
131 | |||
132 | /** |
||
133 | * Create callable factory for the subject service. |
||
134 | * |
||
135 | * @param string $id |
||
136 | * @param array $arguments |
||
137 | * @return mixed |
||
138 | * @throws NotFoundException |
||
139 | */ |
||
140 | 36 | protected function instantiateService(string $id, array $arguments) |
|
168 | |||
169 | /** |
||
170 | * @return InvokerContract |
||
171 | */ |
||
172 | 44 | protected function invoker(): InvokerContract |
|
176 | |||
177 | /** |
||
178 | * Check if container can resolve the service with subject identifier. |
||
179 | * |
||
180 | * @param string $id |
||
181 | * @return bool |
||
182 | */ |
||
183 | 30 | protected function isResolvableService(string $id): bool |
|
187 | |||
188 | /** |
||
189 | * @param string $id |
||
190 | * @return bool |
||
191 | */ |
||
192 | 30 | protected function isShared(string $id): bool |
|
196 | |||
197 | /** |
||
198 | * Normalize key to use across container. |
||
199 | * |
||
200 | * @param string $id |
||
201 | * @return string |
||
202 | */ |
||
203 | 38 | protected function normalize(string $id): string |
|
207 | |||
208 | /** |
||
209 | * @param string $id |
||
210 | * @return void |
||
211 | */ |
||
212 | 36 | protected function resolved(string $id) |
|
216 | |||
217 | /** |
||
218 | * Detects circular references. |
||
219 | * |
||
220 | * @param string $id |
||
221 | * @return void |
||
222 | * @throws CircularReferenceException |
||
223 | */ |
||
224 | 36 | protected function resolving(string $id) |
|
233 | |||
234 | /** |
||
235 | * @param InvokerContract $invoker |
||
236 | * @return void |
||
237 | */ |
||
238 | 44 | protected function setInvoker(InvokerContract $invoker) |
|
242 | |||
243 | /** |
||
244 | * Forbid container cloning. |
||
245 | * |
||
246 | * @codeCoverageIgnore |
||
247 | */ |
||
248 | private function __clone() |
||
251 | |||
252 | /** |
||
253 | * Create callable factory with resolved arguments from callable. |
||
254 | * |
||
255 | * @param Invokable $invokable |
||
256 | * @return Closure |
||
257 | */ |
||
258 | 12 | private function createServiceFactoryFromCallable(Invokable $invokable): Closure |
|
264 | |||
265 | /** |
||
266 | * Create callable factory with resolved arguments from class name. |
||
267 | * |
||
268 | * @param string $class |
||
269 | * @return Closure |
||
270 | * @throws UninstantiableServiceException |
||
271 | */ |
||
272 | 23 | private function createServiceFactoryFromClass(string $class): Closure |
|
292 | |||
293 | /** |
||
294 | * @param string $id |
||
295 | * @param array $arguments |
||
296 | * @return object |
||
297 | */ |
||
298 | 29 | private function invokeFactory(string $id, array $arguments) |
|
302 | |||
303 | /** |
||
304 | * @param string $id |
||
305 | * @param object $object |
||
306 | * @return object |
||
307 | */ |
||
308 | 26 | private function saveShared(string $id, $object) |
|
316 | |||
317 | } |
||
318 |