@@ -54,19 +54,19 @@ discard block |
||
54 | 54 | */ |
55 | 55 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
56 | 56 | { |
57 | - if (!isset($this->state->bindings[$alias])) { |
|
57 | + if (!isset($this->state->bindings[$alias])){ |
|
58 | 58 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
59 | - try { |
|
59 | + try{ |
|
60 | 60 | //No direct instructions how to construct class, make is automatically |
61 | 61 | return $this->autowire($alias, $parameters, $context); |
62 | - } finally { |
|
62 | + }finally{ |
|
63 | 63 | $this->tracer->pop(false); |
64 | 64 | } |
65 | 65 | } |
66 | 66 | |
67 | 67 | $avoidCache = $parameters !== []; |
68 | 68 | $binding = $this->state->bindings[$alias]; |
69 | - try { |
|
69 | + try{ |
|
70 | 70 | $this->tracer->push( |
71 | 71 | false, |
72 | 72 | action: 'resolve from binding', |
@@ -76,23 +76,23 @@ discard block |
||
76 | 76 | ); |
77 | 77 | $this->tracer->push(true); |
78 | 78 | |
79 | - if (\is_object($binding)) { |
|
80 | - if ($binding::class === WeakReference::class) { |
|
81 | - if (($avoidCache || $binding->get() === null) && \class_exists($alias)) { |
|
82 | - try { |
|
79 | + if (\is_object($binding)){ |
|
80 | + if ($binding::class === WeakReference::class){ |
|
81 | + if (($avoidCache || $binding->get() === null) && \class_exists($alias)){ |
|
82 | + try{ |
|
83 | 83 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
84 | 84 | $object = $this->createInstance($alias, $parameters, $context); |
85 | - if ($avoidCache) { |
|
85 | + if ($avoidCache){ |
|
86 | 86 | return $object; |
87 | 87 | } |
88 | 88 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
89 | - } catch (\Throwable) { |
|
89 | + }catch (\Throwable){ |
|
90 | 90 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
91 | 91 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
92 | 92 | $this->tracer->getRootAlias(), |
93 | 93 | $alias, |
94 | 94 | ))); |
95 | - } finally { |
|
95 | + }finally{ |
|
96 | 96 | $this->tracer->pop(); |
97 | 97 | } |
98 | 98 | } |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | : $binding; |
105 | 105 | } |
106 | 106 | |
107 | - if (\is_string($binding)) { |
|
107 | + if (\is_string($binding)){ |
|
108 | 108 | return $binding === $alias |
109 | 109 | ? $this->autowire($alias, $parameters, $context) |
110 | 110 | //Binding is pointing to something else |
@@ -112,20 +112,20 @@ discard block |
||
112 | 112 | } |
113 | 113 | |
114 | 114 | unset($this->state->bindings[$alias]); |
115 | - try { |
|
115 | + try{ |
|
116 | 116 | $instance = $binding[0] === $alias |
117 | 117 | ? $this->autowire($alias, $parameters, $context) |
118 | 118 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
119 | - } finally { |
|
119 | + }finally{ |
|
120 | 120 | /** @psalm-var class-string $alias */ |
121 | 121 | $this->state->bindings[$alias] = $binding; |
122 | 122 | } |
123 | - } finally { |
|
123 | + }finally{ |
|
124 | 124 | $this->tracer->pop(true); |
125 | 125 | $this->tracer->pop(false); |
126 | 126 | } |
127 | 127 | |
128 | - if ($binding[1]) { |
|
128 | + if ($binding[1]){ |
|
129 | 129 | // Indicates singleton |
130 | 130 | /** @psalm-var class-string $alias */ |
131 | 131 | $this->state->bindings[$alias] = $instance; |
@@ -150,7 +150,7 @@ discard block |
||
150 | 150 | && |
151 | 151 | (isset($this->state->injectors[$class]) || $this->binder->hasInjector($class)) |
152 | 152 | )) |
153 | - ) { |
|
153 | + ){ |
|
154 | 154 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
155 | 155 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
156 | 156 | $this->tracer->getRootAlias(), |
@@ -179,18 +179,18 @@ discard block |
||
179 | 179 | array $parameters, |
180 | 180 | string $context = null |
181 | 181 | ): mixed { |
182 | - if (\is_string($target)) { |
|
182 | + if (\is_string($target)){ |
|
183 | 183 | // Reference |
184 | 184 | return $this->make($target, $parameters, $context); |
185 | 185 | } |
186 | 186 | |
187 | - if ($target instanceof Autowire) { |
|
187 | + if ($target instanceof Autowire){ |
|
188 | 188 | return $target->resolve($this, $parameters); |
189 | 189 | } |
190 | 190 | |
191 | - try { |
|
191 | + try{ |
|
192 | 192 | return $this->invoker->invoke($target, $parameters); |
193 | - } catch (NotCallableException $e) { |
|
193 | + }catch (NotCallableException $e){ |
|
194 | 194 | throw new ContainerException( |
195 | 195 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
196 | 196 | $e->getCode(), |
@@ -214,20 +214,20 @@ discard block |
||
214 | 214 | */ |
215 | 215 | private function createInstance(string $class, array $parameters, string $context = null): object |
216 | 216 | { |
217 | - try { |
|
217 | + try{ |
|
218 | 218 | $reflection = new \ReflectionClass($class); |
219 | - } catch (\ReflectionException $e) { |
|
219 | + }catch (\ReflectionException $e){ |
|
220 | 220 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
221 | 221 | } |
222 | 222 | |
223 | 223 | //We have to construct class using external injector when we know exact context |
224 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
224 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
225 | 225 | $injector = $this->state->injectors[$reflection->getName()]; |
226 | 226 | |
227 | - try { |
|
227 | + try{ |
|
228 | 228 | $injectorInstance = $this->container->get($injector); |
229 | 229 | |
230 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
230 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
231 | 231 | throw new InjectionException( |
232 | 232 | \sprintf( |
233 | 233 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | * @psalm-suppress RedundantCondition |
243 | 243 | */ |
244 | 244 | $instance = $injectorInstance->createInjection($reflection, $context); |
245 | - if (!$reflection->isInstance($instance)) { |
|
245 | + if (!$reflection->isInstance($instance)){ |
|
246 | 246 | throw new InjectionException( |
247 | 247 | \sprintf( |
248 | 248 | "Invalid injection response for '%s'.", |
@@ -252,12 +252,12 @@ discard block |
||
252 | 252 | } |
253 | 253 | |
254 | 254 | return $instance; |
255 | - } finally { |
|
255 | + }finally{ |
|
256 | 256 | $this->state->injectors[$reflection->getName()] = $injector; |
257 | 257 | } |
258 | 258 | } |
259 | 259 | |
260 | - if (!$reflection->isInstantiable()) { |
|
260 | + if (!$reflection->isInstantiable()){ |
|
261 | 261 | $itIs = match (true) { |
262 | 262 | $reflection->isEnum() => 'Enum', |
263 | 263 | $reflection->isAbstract() => 'Abstract class', |
@@ -270,12 +270,12 @@ discard block |
||
270 | 270 | |
271 | 271 | $constructor = $reflection->getConstructor(); |
272 | 272 | |
273 | - if ($constructor !== null) { |
|
274 | - try { |
|
273 | + if ($constructor !== null){ |
|
274 | + try{ |
|
275 | 275 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
276 | 276 | $this->tracer->push(true); |
277 | 277 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
278 | - } catch (ValidationException $e) { |
|
278 | + }catch (ValidationException $e){ |
|
279 | 279 | throw new ContainerException( |
280 | 280 | $this->tracer->combineTraceMessage( |
281 | 281 | \sprintf( |
@@ -285,22 +285,22 @@ discard block |
||
285 | 285 | ) |
286 | 286 | ), |
287 | 287 | ); |
288 | - } finally { |
|
288 | + }finally{ |
|
289 | 289 | $this->tracer->pop(true); |
290 | 290 | $this->tracer->pop(false); |
291 | 291 | } |
292 | - try { |
|
292 | + try{ |
|
293 | 293 | // Using constructor with resolved arguments |
294 | 294 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
295 | 295 | $this->tracer->push(true); |
296 | 296 | $instance = new $class(...$arguments); |
297 | - } catch (\TypeError $e) { |
|
297 | + }catch (\TypeError $e){ |
|
298 | 298 | throw new WrongTypeException($constructor, $e); |
299 | - } finally { |
|
299 | + }finally{ |
|
300 | 300 | $this->tracer->pop(true); |
301 | 301 | $this->tracer->pop(false); |
302 | 302 | } |
303 | - } else { |
|
303 | + }else{ |
|
304 | 304 | // No constructor specified |
305 | 305 | $instance = $reflection->newInstance(); |
306 | 306 | } |
@@ -321,9 +321,9 @@ discard block |
||
321 | 321 | private function registerInstance(object $instance): object |
322 | 322 | { |
323 | 323 | //Declarative singletons |
324 | - if ($instance instanceof SingletonInterface) { |
|
324 | + if ($instance instanceof SingletonInterface){ |
|
325 | 325 | $alias = $instance::class; |
326 | - if (!isset($this->state->bindings[$alias])) { |
|
326 | + if (!isset($this->state->bindings[$alias])){ |
|
327 | 327 | $this->state->bindings[$alias] = $instance; |
328 | 328 | } |
329 | 329 | } |
@@ -54,19 +54,24 @@ discard block |
||
54 | 54 | */ |
55 | 55 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
56 | 56 | { |
57 | - if (!isset($this->state->bindings[$alias])) { |
|
57 | + if (!isset($this->state->bindings[$alias])) |
|
58 | + { |
|
58 | 59 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
59 | - try { |
|
60 | + try |
|
61 | + { |
|
60 | 62 | //No direct instructions how to construct class, make is automatically |
61 | 63 | return $this->autowire($alias, $parameters, $context); |
62 | - } finally { |
|
64 | + } |
|
65 | + finally |
|
66 | + { |
|
63 | 67 | $this->tracer->pop(false); |
64 | 68 | } |
65 | 69 | } |
66 | 70 | |
67 | 71 | $avoidCache = $parameters !== []; |
68 | 72 | $binding = $this->state->bindings[$alias]; |
69 | - try { |
|
73 | + try |
|
74 | + { |
|
70 | 75 | $this->tracer->push( |
71 | 76 | false, |
72 | 77 | action: 'resolve from binding', |
@@ -76,23 +81,32 @@ discard block |
||
76 | 81 | ); |
77 | 82 | $this->tracer->push(true); |
78 | 83 | |
79 | - if (\is_object($binding)) { |
|
80 | - if ($binding::class === WeakReference::class) { |
|
81 | - if (($avoidCache || $binding->get() === null) && \class_exists($alias)) { |
|
82 | - try { |
|
84 | + if (\is_object($binding)) |
|
85 | + { |
|
86 | + if ($binding::class === WeakReference::class) |
|
87 | + { |
|
88 | + if (($avoidCache || $binding->get() === null) && \class_exists($alias)) |
|
89 | + { |
|
90 | + try |
|
91 | + { |
|
83 | 92 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
84 | 93 | $object = $this->createInstance($alias, $parameters, $context); |
85 | - if ($avoidCache) { |
|
94 | + if ($avoidCache) |
|
95 | + { |
|
86 | 96 | return $object; |
87 | 97 | } |
88 | 98 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
89 | - } catch (\Throwable) { |
|
99 | + } |
|
100 | + catch (\Throwable) |
|
101 | + { |
|
90 | 102 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
91 | 103 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
92 | 104 | $this->tracer->getRootAlias(), |
93 | 105 | $alias, |
94 | 106 | ))); |
95 | - } finally { |
|
107 | + } |
|
108 | + finally |
|
109 | + { |
|
96 | 110 | $this->tracer->pop(); |
97 | 111 | } |
98 | 112 | } |
@@ -104,7 +118,8 @@ discard block |
||
104 | 118 | : $binding; |
105 | 119 | } |
106 | 120 | |
107 | - if (\is_string($binding)) { |
|
121 | + if (\is_string($binding)) |
|
122 | + { |
|
108 | 123 | return $binding === $alias |
109 | 124 | ? $this->autowire($alias, $parameters, $context) |
110 | 125 | //Binding is pointing to something else |
@@ -112,20 +127,26 @@ discard block |
||
112 | 127 | } |
113 | 128 | |
114 | 129 | unset($this->state->bindings[$alias]); |
115 | - try { |
|
130 | + try |
|
131 | + { |
|
116 | 132 | $instance = $binding[0] === $alias |
117 | 133 | ? $this->autowire($alias, $parameters, $context) |
118 | 134 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
119 | - } finally { |
|
135 | + } |
|
136 | + finally |
|
137 | + { |
|
120 | 138 | /** @psalm-var class-string $alias */ |
121 | 139 | $this->state->bindings[$alias] = $binding; |
122 | 140 | } |
123 | - } finally { |
|
141 | + } |
|
142 | + finally |
|
143 | + { |
|
124 | 144 | $this->tracer->pop(true); |
125 | 145 | $this->tracer->pop(false); |
126 | 146 | } |
127 | 147 | |
128 | - if ($binding[1]) { |
|
148 | + if ($binding[1]) |
|
149 | + { |
|
129 | 150 | // Indicates singleton |
130 | 151 | /** @psalm-var class-string $alias */ |
131 | 152 | $this->state->bindings[$alias] = $instance; |
@@ -179,18 +200,23 @@ discard block |
||
179 | 200 | array $parameters, |
180 | 201 | string $context = null |
181 | 202 | ): mixed { |
182 | - if (\is_string($target)) { |
|
203 | + if (\is_string($target)) |
|
204 | + { |
|
183 | 205 | // Reference |
184 | 206 | return $this->make($target, $parameters, $context); |
185 | 207 | } |
186 | 208 | |
187 | - if ($target instanceof Autowire) { |
|
209 | + if ($target instanceof Autowire) |
|
210 | + { |
|
188 | 211 | return $target->resolve($this, $parameters); |
189 | 212 | } |
190 | 213 | |
191 | - try { |
|
214 | + try |
|
215 | + { |
|
192 | 216 | return $this->invoker->invoke($target, $parameters); |
193 | - } catch (NotCallableException $e) { |
|
217 | + } |
|
218 | + catch (NotCallableException $e) |
|
219 | + { |
|
194 | 220 | throw new ContainerException( |
195 | 221 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
196 | 222 | $e->getCode(), |
@@ -214,20 +240,26 @@ discard block |
||
214 | 240 | */ |
215 | 241 | private function createInstance(string $class, array $parameters, string $context = null): object |
216 | 242 | { |
217 | - try { |
|
243 | + try |
|
244 | + { |
|
218 | 245 | $reflection = new \ReflectionClass($class); |
219 | - } catch (\ReflectionException $e) { |
|
246 | + } |
|
247 | + catch (\ReflectionException $e) |
|
248 | + { |
|
220 | 249 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
221 | 250 | } |
222 | 251 | |
223 | 252 | //We have to construct class using external injector when we know exact context |
224 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
253 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
254 | + { |
|
225 | 255 | $injector = $this->state->injectors[$reflection->getName()]; |
226 | 256 | |
227 | - try { |
|
257 | + try |
|
258 | + { |
|
228 | 259 | $injectorInstance = $this->container->get($injector); |
229 | 260 | |
230 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
261 | + if (!$injectorInstance instanceof InjectorInterface) |
|
262 | + { |
|
231 | 263 | throw new InjectionException( |
232 | 264 | \sprintf( |
233 | 265 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -242,7 +274,8 @@ discard block |
||
242 | 274 | * @psalm-suppress RedundantCondition |
243 | 275 | */ |
244 | 276 | $instance = $injectorInstance->createInjection($reflection, $context); |
245 | - if (!$reflection->isInstance($instance)) { |
|
277 | + if (!$reflection->isInstance($instance)) |
|
278 | + { |
|
246 | 279 | throw new InjectionException( |
247 | 280 | \sprintf( |
248 | 281 | "Invalid injection response for '%s'.", |
@@ -252,12 +285,15 @@ discard block |
||
252 | 285 | } |
253 | 286 | |
254 | 287 | return $instance; |
255 | - } finally { |
|
288 | + } |
|
289 | + finally |
|
290 | + { |
|
256 | 291 | $this->state->injectors[$reflection->getName()] = $injector; |
257 | 292 | } |
258 | 293 | } |
259 | 294 | |
260 | - if (!$reflection->isInstantiable()) { |
|
295 | + if (!$reflection->isInstantiable()) |
|
296 | + { |
|
261 | 297 | $itIs = match (true) { |
262 | 298 | $reflection->isEnum() => 'Enum', |
263 | 299 | $reflection->isAbstract() => 'Abstract class', |
@@ -270,12 +306,16 @@ discard block |
||
270 | 306 | |
271 | 307 | $constructor = $reflection->getConstructor(); |
272 | 308 | |
273 | - if ($constructor !== null) { |
|
274 | - try { |
|
309 | + if ($constructor !== null) |
|
310 | + { |
|
311 | + try |
|
312 | + { |
|
275 | 313 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
276 | 314 | $this->tracer->push(true); |
277 | 315 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
278 | - } catch (ValidationException $e) { |
|
316 | + } |
|
317 | + catch (ValidationException $e) |
|
318 | + { |
|
279 | 319 | throw new ContainerException( |
280 | 320 | $this->tracer->combineTraceMessage( |
281 | 321 | \sprintf( |
@@ -285,22 +325,31 @@ discard block |
||
285 | 325 | ) |
286 | 326 | ), |
287 | 327 | ); |
288 | - } finally { |
|
328 | + } |
|
329 | + finally |
|
330 | + { |
|
289 | 331 | $this->tracer->pop(true); |
290 | 332 | $this->tracer->pop(false); |
291 | 333 | } |
292 | - try { |
|
334 | + try |
|
335 | + { |
|
293 | 336 | // Using constructor with resolved arguments |
294 | 337 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
295 | 338 | $this->tracer->push(true); |
296 | 339 | $instance = new $class(...$arguments); |
297 | - } catch (\TypeError $e) { |
|
340 | + } |
|
341 | + catch (\TypeError $e) |
|
342 | + { |
|
298 | 343 | throw new WrongTypeException($constructor, $e); |
299 | - } finally { |
|
344 | + } |
|
345 | + finally |
|
346 | + { |
|
300 | 347 | $this->tracer->pop(true); |
301 | 348 | $this->tracer->pop(false); |
302 | 349 | } |
303 | - } else { |
|
350 | + } |
|
351 | + else |
|
352 | + { |
|
304 | 353 | // No constructor specified |
305 | 354 | $instance = $reflection->newInstance(); |
306 | 355 | } |
@@ -321,9 +370,11 @@ discard block |
||
321 | 370 | private function registerInstance(object $instance): object |
322 | 371 | { |
323 | 372 | //Declarative singletons |
324 | - if ($instance instanceof SingletonInterface) { |
|
373 | + if ($instance instanceof SingletonInterface) |
|
374 | + { |
|
325 | 375 | $alias = $instance::class; |
326 | - if (!isset($this->state->bindings[$alias])) { |
|
376 | + if (!isset($this->state->bindings[$alias])) |
|
377 | + { |
|
327 | 378 | $this->state->bindings[$alias] = $instance; |
328 | 379 | } |
329 | 380 | } |