@@ -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,26 +104,26 @@ discard block |
||
104 | 104 | : $binding; |
105 | 105 | } |
106 | 106 | |
107 | - if (\is_string($binding)) { |
|
107 | + if (\is_string($binding)){ |
|
108 | 108 | //Binding is pointing to something else |
109 | 109 | return $this->make($binding, $parameters, $context); |
110 | 110 | } |
111 | 111 | |
112 | 112 | unset($this->state->bindings[$alias]); |
113 | - try { |
|
113 | + try{ |
|
114 | 114 | $instance = $binding[0] === $alias |
115 | 115 | ? $this->autowire($alias, $parameters, $context) |
116 | 116 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
117 | - } finally { |
|
117 | + }finally{ |
|
118 | 118 | /** @psalm-var class-string $alias */ |
119 | 119 | $this->state->bindings[$alias] = $binding; |
120 | 120 | } |
121 | - } finally { |
|
121 | + }finally{ |
|
122 | 122 | $this->tracer->pop(true); |
123 | 123 | $this->tracer->pop(false); |
124 | 124 | } |
125 | 125 | |
126 | - if ($binding[1]) { |
|
126 | + if ($binding[1]){ |
|
127 | 127 | // Indicates singleton |
128 | 128 | /** @psalm-var class-string $alias */ |
129 | 129 | $this->state->bindings[$alias] = $instance; |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | && |
149 | 149 | (isset($this->state->injectors[$class]) || $this->binder->hasInjector($class)) |
150 | 150 | )) |
151 | - ) { |
|
151 | + ){ |
|
152 | 152 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
153 | 153 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
154 | 154 | $this->tracer->getRootAlias(), |
@@ -177,18 +177,18 @@ discard block |
||
177 | 177 | array $parameters, |
178 | 178 | string $context = null |
179 | 179 | ): mixed { |
180 | - if (\is_string($target)) { |
|
180 | + if (\is_string($target)){ |
|
181 | 181 | // Reference |
182 | 182 | return $this->make($target, $parameters, $context); |
183 | 183 | } |
184 | 184 | |
185 | - if ($target instanceof Autowire) { |
|
185 | + if ($target instanceof Autowire){ |
|
186 | 186 | return $target->resolve($this, $parameters); |
187 | 187 | } |
188 | 188 | |
189 | - try { |
|
189 | + try{ |
|
190 | 190 | return $this->invoker->invoke($target, $parameters); |
191 | - } catch (NotCallableException $e) { |
|
191 | + }catch (NotCallableException $e){ |
|
192 | 192 | throw new ContainerException( |
193 | 193 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
194 | 194 | $e->getCode(), |
@@ -212,20 +212,20 @@ discard block |
||
212 | 212 | */ |
213 | 213 | private function createInstance(string $class, array $parameters, string $context = null): object |
214 | 214 | { |
215 | - try { |
|
215 | + try{ |
|
216 | 216 | $reflection = new \ReflectionClass($class); |
217 | - } catch (\ReflectionException $e) { |
|
217 | + }catch (\ReflectionException $e){ |
|
218 | 218 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
219 | 219 | } |
220 | 220 | |
221 | 221 | //We have to construct class using external injector when we know exact context |
222 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
222 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
223 | 223 | $injector = $this->state->injectors[$reflection->getName()]; |
224 | 224 | |
225 | - try { |
|
225 | + try{ |
|
226 | 226 | $injectorInstance = $this->container->get($injector); |
227 | 227 | |
228 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
228 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
229 | 229 | throw new InjectionException( |
230 | 230 | \sprintf( |
231 | 231 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | |
238 | 238 | /** @var InjectorInterface<TObject> $injectorInstance */ |
239 | 239 | $instance = $injectorInstance->createInjection($reflection, $context); |
240 | - if (!$reflection->isInstance($instance)) { |
|
240 | + if (!$reflection->isInstance($instance)){ |
|
241 | 241 | throw new InjectionException( |
242 | 242 | \sprintf( |
243 | 243 | "Invalid injection response for '%s'.", |
@@ -247,12 +247,12 @@ discard block |
||
247 | 247 | } |
248 | 248 | |
249 | 249 | return $instance; |
250 | - } finally { |
|
250 | + }finally{ |
|
251 | 251 | $this->state->injectors[$reflection->getName()] = $injector; |
252 | 252 | } |
253 | 253 | } |
254 | 254 | |
255 | - if (!$reflection->isInstantiable()) { |
|
255 | + if (!$reflection->isInstantiable()){ |
|
256 | 256 | $itIs = match (true) { |
257 | 257 | $reflection->isEnum() => 'Enum', |
258 | 258 | $reflection->isAbstract() => 'Abstract class', |
@@ -265,12 +265,12 @@ discard block |
||
265 | 265 | |
266 | 266 | $constructor = $reflection->getConstructor(); |
267 | 267 | |
268 | - if ($constructor !== null) { |
|
269 | - try { |
|
268 | + if ($constructor !== null){ |
|
269 | + try{ |
|
270 | 270 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
271 | 271 | $this->tracer->push(true); |
272 | 272 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
273 | - } catch (ValidationException $e) { |
|
273 | + }catch (ValidationException $e){ |
|
274 | 274 | throw new ContainerException( |
275 | 275 | $this->tracer->combineTraceMessage( |
276 | 276 | \sprintf( |
@@ -280,22 +280,22 @@ discard block |
||
280 | 280 | ) |
281 | 281 | ), |
282 | 282 | ); |
283 | - } finally { |
|
283 | + }finally{ |
|
284 | 284 | $this->tracer->pop(true); |
285 | 285 | $this->tracer->pop(false); |
286 | 286 | } |
287 | - try { |
|
287 | + try{ |
|
288 | 288 | // Using constructor with resolved arguments |
289 | 289 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
290 | 290 | $this->tracer->push(true); |
291 | 291 | $instance = new $class(...$arguments); |
292 | - } catch (\TypeError $e) { |
|
292 | + }catch (\TypeError $e){ |
|
293 | 293 | throw new WrongTypeException($constructor, $e); |
294 | - } finally { |
|
294 | + }finally{ |
|
295 | 295 | $this->tracer->pop(true); |
296 | 296 | $this->tracer->pop(false); |
297 | 297 | } |
298 | - } else { |
|
298 | + }else{ |
|
299 | 299 | // No constructor specified |
300 | 300 | $instance = $reflection->newInstance(); |
301 | 301 | } |
@@ -316,9 +316,9 @@ discard block |
||
316 | 316 | private function registerInstance(object $instance): object |
317 | 317 | { |
318 | 318 | //Declarative singletons |
319 | - if ($instance instanceof SingletonInterface) { |
|
319 | + if ($instance instanceof SingletonInterface){ |
|
320 | 320 | $alias = $instance::class; |
321 | - if (!isset($this->state->bindings[$alias])) { |
|
321 | + if (!isset($this->state->bindings[$alias])){ |
|
322 | 322 | $this->state->bindings[$alias] = $instance; |
323 | 323 | } |
324 | 324 | } |
@@ -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,26 +118,33 @@ discard block |
||
104 | 118 | : $binding; |
105 | 119 | } |
106 | 120 | |
107 | - if (\is_string($binding)) { |
|
121 | + if (\is_string($binding)) |
|
122 | + { |
|
108 | 123 | //Binding is pointing to something else |
109 | 124 | return $this->make($binding, $parameters, $context); |
110 | 125 | } |
111 | 126 | |
112 | 127 | unset($this->state->bindings[$alias]); |
113 | - try { |
|
128 | + try |
|
129 | + { |
|
114 | 130 | $instance = $binding[0] === $alias |
115 | 131 | ? $this->autowire($alias, $parameters, $context) |
116 | 132 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
117 | - } finally { |
|
133 | + } |
|
134 | + finally |
|
135 | + { |
|
118 | 136 | /** @psalm-var class-string $alias */ |
119 | 137 | $this->state->bindings[$alias] = $binding; |
120 | 138 | } |
121 | - } finally { |
|
139 | + } |
|
140 | + finally |
|
141 | + { |
|
122 | 142 | $this->tracer->pop(true); |
123 | 143 | $this->tracer->pop(false); |
124 | 144 | } |
125 | 145 | |
126 | - if ($binding[1]) { |
|
146 | + if ($binding[1]) |
|
147 | + { |
|
127 | 148 | // Indicates singleton |
128 | 149 | /** @psalm-var class-string $alias */ |
129 | 150 | $this->state->bindings[$alias] = $instance; |
@@ -177,18 +198,23 @@ discard block |
||
177 | 198 | array $parameters, |
178 | 199 | string $context = null |
179 | 200 | ): mixed { |
180 | - if (\is_string($target)) { |
|
201 | + if (\is_string($target)) |
|
202 | + { |
|
181 | 203 | // Reference |
182 | 204 | return $this->make($target, $parameters, $context); |
183 | 205 | } |
184 | 206 | |
185 | - if ($target instanceof Autowire) { |
|
207 | + if ($target instanceof Autowire) |
|
208 | + { |
|
186 | 209 | return $target->resolve($this, $parameters); |
187 | 210 | } |
188 | 211 | |
189 | - try { |
|
212 | + try |
|
213 | + { |
|
190 | 214 | return $this->invoker->invoke($target, $parameters); |
191 | - } catch (NotCallableException $e) { |
|
215 | + } |
|
216 | + catch (NotCallableException $e) |
|
217 | + { |
|
192 | 218 | throw new ContainerException( |
193 | 219 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
194 | 220 | $e->getCode(), |
@@ -212,20 +238,26 @@ discard block |
||
212 | 238 | */ |
213 | 239 | private function createInstance(string $class, array $parameters, string $context = null): object |
214 | 240 | { |
215 | - try { |
|
241 | + try |
|
242 | + { |
|
216 | 243 | $reflection = new \ReflectionClass($class); |
217 | - } catch (\ReflectionException $e) { |
|
244 | + } |
|
245 | + catch (\ReflectionException $e) |
|
246 | + { |
|
218 | 247 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
219 | 248 | } |
220 | 249 | |
221 | 250 | //We have to construct class using external injector when we know exact context |
222 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
251 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
252 | + { |
|
223 | 253 | $injector = $this->state->injectors[$reflection->getName()]; |
224 | 254 | |
225 | - try { |
|
255 | + try |
|
256 | + { |
|
226 | 257 | $injectorInstance = $this->container->get($injector); |
227 | 258 | |
228 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
259 | + if (!$injectorInstance instanceof InjectorInterface) |
|
260 | + { |
|
229 | 261 | throw new InjectionException( |
230 | 262 | \sprintf( |
231 | 263 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -237,7 +269,8 @@ discard block |
||
237 | 269 | |
238 | 270 | /** @var InjectorInterface<TObject> $injectorInstance */ |
239 | 271 | $instance = $injectorInstance->createInjection($reflection, $context); |
240 | - if (!$reflection->isInstance($instance)) { |
|
272 | + if (!$reflection->isInstance($instance)) |
|
273 | + { |
|
241 | 274 | throw new InjectionException( |
242 | 275 | \sprintf( |
243 | 276 | "Invalid injection response for '%s'.", |
@@ -247,12 +280,15 @@ discard block |
||
247 | 280 | } |
248 | 281 | |
249 | 282 | return $instance; |
250 | - } finally { |
|
283 | + } |
|
284 | + finally |
|
285 | + { |
|
251 | 286 | $this->state->injectors[$reflection->getName()] = $injector; |
252 | 287 | } |
253 | 288 | } |
254 | 289 | |
255 | - if (!$reflection->isInstantiable()) { |
|
290 | + if (!$reflection->isInstantiable()) |
|
291 | + { |
|
256 | 292 | $itIs = match (true) { |
257 | 293 | $reflection->isEnum() => 'Enum', |
258 | 294 | $reflection->isAbstract() => 'Abstract class', |
@@ -265,12 +301,16 @@ discard block |
||
265 | 301 | |
266 | 302 | $constructor = $reflection->getConstructor(); |
267 | 303 | |
268 | - if ($constructor !== null) { |
|
269 | - try { |
|
304 | + if ($constructor !== null) |
|
305 | + { |
|
306 | + try |
|
307 | + { |
|
270 | 308 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
271 | 309 | $this->tracer->push(true); |
272 | 310 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
273 | - } catch (ValidationException $e) { |
|
311 | + } |
|
312 | + catch (ValidationException $e) |
|
313 | + { |
|
274 | 314 | throw new ContainerException( |
275 | 315 | $this->tracer->combineTraceMessage( |
276 | 316 | \sprintf( |
@@ -280,22 +320,31 @@ discard block |
||
280 | 320 | ) |
281 | 321 | ), |
282 | 322 | ); |
283 | - } finally { |
|
323 | + } |
|
324 | + finally |
|
325 | + { |
|
284 | 326 | $this->tracer->pop(true); |
285 | 327 | $this->tracer->pop(false); |
286 | 328 | } |
287 | - try { |
|
329 | + try |
|
330 | + { |
|
288 | 331 | // Using constructor with resolved arguments |
289 | 332 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
290 | 333 | $this->tracer->push(true); |
291 | 334 | $instance = new $class(...$arguments); |
292 | - } catch (\TypeError $e) { |
|
335 | + } |
|
336 | + catch (\TypeError $e) |
|
337 | + { |
|
293 | 338 | throw new WrongTypeException($constructor, $e); |
294 | - } finally { |
|
339 | + } |
|
340 | + finally |
|
341 | + { |
|
295 | 342 | $this->tracer->pop(true); |
296 | 343 | $this->tracer->pop(false); |
297 | 344 | } |
298 | - } else { |
|
345 | + } |
|
346 | + else |
|
347 | + { |
|
299 | 348 | // No constructor specified |
300 | 349 | $instance = $reflection->newInstance(); |
301 | 350 | } |
@@ -316,9 +365,11 @@ discard block |
||
316 | 365 | private function registerInstance(object $instance): object |
317 | 366 | { |
318 | 367 | //Declarative singletons |
319 | - if ($instance instanceof SingletonInterface) { |
|
368 | + if ($instance instanceof SingletonInterface) |
|
369 | + { |
|
320 | 370 | $alias = $instance::class; |
321 | - if (!isset($this->state->bindings[$alias])) { |
|
371 | + if (!isset($this->state->bindings[$alias])) |
|
372 | + { |
|
322 | 373 | $this->state->bindings[$alias] = $instance; |
323 | 374 | } |
324 | 375 | } |