@@ -54,35 +54,35 @@ 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 | $binding = $this->state->bindings[$alias]; |
68 | - try { |
|
68 | + try{ |
|
69 | 69 | $this->tracer->push(false, action: 'resolve from binding', alias: $alias, context: $context, binding: $binding); |
70 | 70 | $this->tracer->push(true); |
71 | 71 | |
72 | - if (\is_object($binding)) { |
|
73 | - if ($binding::class === WeakReference::class) { |
|
74 | - if ($binding->get() === null && \class_exists($alias)) { |
|
75 | - try { |
|
72 | + if (\is_object($binding)){ |
|
73 | + if ($binding::class === WeakReference::class){ |
|
74 | + if ($binding->get() === null && \class_exists($alias)){ |
|
75 | + try{ |
|
76 | 76 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
77 | 77 | $object = $this->createInstance($alias, $parameters, $context); |
78 | 78 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
79 | - } catch (\Throwable) { |
|
79 | + }catch (\Throwable){ |
|
80 | 80 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
81 | 81 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
82 | 82 | $this->tracer->getRootAlias(), |
83 | 83 | $alias, |
84 | 84 | ))); |
85 | - } finally { |
|
85 | + }finally{ |
|
86 | 86 | $this->tracer->pop(); |
87 | 87 | } |
88 | 88 | } |
@@ -92,26 +92,26 @@ discard block |
||
92 | 92 | return $binding; |
93 | 93 | } |
94 | 94 | |
95 | - if (\is_string($binding)) { |
|
95 | + if (\is_string($binding)){ |
|
96 | 96 | //Binding is pointing to something else |
97 | 97 | return $this->make($binding, $parameters, $context); |
98 | 98 | } |
99 | 99 | |
100 | 100 | unset($this->state->bindings[$alias]); |
101 | - try { |
|
101 | + try{ |
|
102 | 102 | $instance = $binding[0] === $alias |
103 | 103 | ? $this->autowire($alias, $parameters, $context) |
104 | 104 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
105 | - } finally { |
|
105 | + }finally{ |
|
106 | 106 | /** @psalm-var class-string $alias */ |
107 | 107 | $this->state->bindings[$alias] = $binding; |
108 | 108 | } |
109 | - } finally { |
|
109 | + }finally{ |
|
110 | 110 | $this->tracer->pop(true); |
111 | 111 | $this->tracer->pop(false); |
112 | 112 | } |
113 | 113 | |
114 | - if ($binding[1]) { |
|
114 | + if ($binding[1]){ |
|
115 | 115 | // Indicates singleton |
116 | 116 | /** @psalm-var class-string $alias */ |
117 | 117 | $this->state->bindings[$alias] = $instance; |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | && |
136 | 136 | (isset($this->state->injectors[$class]) || $this->binder->hasInjector($class)) |
137 | 137 | )) |
138 | - ) { |
|
138 | + ){ |
|
139 | 139 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
140 | 140 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
141 | 141 | $this->tracer->getRootAlias(), |
@@ -162,18 +162,18 @@ discard block |
||
162 | 162 | array $parameters, |
163 | 163 | string $context = null |
164 | 164 | ): mixed { |
165 | - if (\is_string($target)) { |
|
165 | + if (\is_string($target)){ |
|
166 | 166 | // Reference |
167 | 167 | return $this->make($target, $parameters, $context); |
168 | 168 | } |
169 | 169 | |
170 | - if ($target instanceof Autowire) { |
|
170 | + if ($target instanceof Autowire){ |
|
171 | 171 | return $target->resolve($this, $parameters); |
172 | 172 | } |
173 | 173 | |
174 | - try { |
|
174 | + try{ |
|
175 | 175 | return $this->invoker->invoke($target, $parameters); |
176 | - } catch (NotCallableException $e) { |
|
176 | + }catch (NotCallableException $e){ |
|
177 | 177 | throw new ContainerException( |
178 | 178 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
179 | 179 | $e->getCode(), |
@@ -197,20 +197,20 @@ discard block |
||
197 | 197 | */ |
198 | 198 | private function createInstance(string $class, array $parameters, string $context = null): object |
199 | 199 | { |
200 | - try { |
|
200 | + try{ |
|
201 | 201 | $reflection = new \ReflectionClass($class); |
202 | - } catch (\ReflectionException $e) { |
|
202 | + }catch (\ReflectionException $e){ |
|
203 | 203 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
204 | 204 | } |
205 | 205 | |
206 | 206 | //We have to construct class using external injector when we know exact context |
207 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
207 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
208 | 208 | $injector = $this->state->injectors[$reflection->getName()]; |
209 | 209 | |
210 | - try { |
|
210 | + try{ |
|
211 | 211 | $injectorInstance = $this->container->get($injector); |
212 | 212 | |
213 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
213 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
214 | 214 | throw new InjectionException( |
215 | 215 | \sprintf( |
216 | 216 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | |
223 | 223 | /** @var InjectorInterface<TObject> $injectorInstance */ |
224 | 224 | $instance = $injectorInstance->createInjection($reflection, $context); |
225 | - if (!$reflection->isInstance($instance)) { |
|
225 | + if (!$reflection->isInstance($instance)){ |
|
226 | 226 | throw new InjectionException( |
227 | 227 | \sprintf( |
228 | 228 | "Invalid injection response for '%s'.", |
@@ -232,12 +232,12 @@ discard block |
||
232 | 232 | } |
233 | 233 | |
234 | 234 | return $instance; |
235 | - } finally { |
|
235 | + }finally{ |
|
236 | 236 | $this->state->injectors[$reflection->getName()] = $injector; |
237 | 237 | } |
238 | 238 | } |
239 | 239 | |
240 | - if (!$reflection->isInstantiable()) { |
|
240 | + if (!$reflection->isInstantiable()){ |
|
241 | 241 | $itIs = match (true) { |
242 | 242 | $reflection->isEnum() => 'Enum', |
243 | 243 | $reflection->isAbstract() => 'Abstract class', |
@@ -250,12 +250,12 @@ discard block |
||
250 | 250 | |
251 | 251 | $constructor = $reflection->getConstructor(); |
252 | 252 | |
253 | - if ($constructor !== null) { |
|
254 | - try { |
|
253 | + if ($constructor !== null){ |
|
254 | + try{ |
|
255 | 255 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
256 | 256 | $this->tracer->push(true); |
257 | 257 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
258 | - } catch (ValidationException $e) { |
|
258 | + }catch (ValidationException $e){ |
|
259 | 259 | throw new ContainerException( |
260 | 260 | $this->tracer->combineTraceMessage( |
261 | 261 | \sprintf( |
@@ -265,22 +265,22 @@ discard block |
||
265 | 265 | ) |
266 | 266 | ), |
267 | 267 | ); |
268 | - } finally { |
|
268 | + }finally{ |
|
269 | 269 | $this->tracer->pop(true); |
270 | 270 | $this->tracer->pop(false); |
271 | 271 | } |
272 | - try { |
|
272 | + try{ |
|
273 | 273 | // Using constructor with resolved arguments |
274 | 274 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
275 | 275 | $this->tracer->push(true); |
276 | 276 | $instance = new $class(...$arguments); |
277 | - } catch (\TypeError $e) { |
|
277 | + }catch (\TypeError $e){ |
|
278 | 278 | throw new WrongTypeException($constructor, $e); |
279 | - } finally { |
|
279 | + }finally{ |
|
280 | 280 | $this->tracer->pop(true); |
281 | 281 | $this->tracer->pop(false); |
282 | 282 | } |
283 | - } else { |
|
283 | + }else{ |
|
284 | 284 | // No constructor specified |
285 | 285 | $instance = $reflection->newInstance(); |
286 | 286 | } |
@@ -298,9 +298,9 @@ discard block |
||
298 | 298 | private function registerInstance(object $instance, array $parameters): object |
299 | 299 | { |
300 | 300 | //Declarative singletons (only when class received via direct get) |
301 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
301 | + if ($parameters === [] && $instance instanceof SingletonInterface){ |
|
302 | 302 | $alias = $instance::class; |
303 | - if (!isset($this->state->bindings[$alias])) { |
|
303 | + if (!isset($this->state->bindings[$alias])){ |
|
304 | 304 | $this->state->bindings[$alias] = $instance; |
305 | 305 | } |
306 | 306 | } |
@@ -54,35 +54,48 @@ 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 | $binding = $this->state->bindings[$alias]; |
68 | - try { |
|
72 | + try |
|
73 | + { |
|
69 | 74 | $this->tracer->push(false, action: 'resolve from binding', alias: $alias, context: $context, binding: $binding); |
70 | 75 | $this->tracer->push(true); |
71 | 76 | |
72 | - if (\is_object($binding)) { |
|
73 | - if ($binding::class === WeakReference::class) { |
|
74 | - if ($binding->get() === null && \class_exists($alias)) { |
|
75 | - try { |
|
77 | + if (\is_object($binding)) |
|
78 | + { |
|
79 | + if ($binding::class === WeakReference::class) |
|
80 | + { |
|
81 | + if ($binding->get() === null && \class_exists($alias)) |
|
82 | + { |
|
83 | + try |
|
84 | + { |
|
76 | 85 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
77 | 86 | $object = $this->createInstance($alias, $parameters, $context); |
78 | 87 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
79 | - } catch (\Throwable) { |
|
88 | + } |
|
89 | + catch (\Throwable) |
|
90 | + { |
|
80 | 91 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
81 | 92 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
82 | 93 | $this->tracer->getRootAlias(), |
83 | 94 | $alias, |
84 | 95 | ))); |
85 | - } finally { |
|
96 | + } |
|
97 | + finally |
|
98 | + { |
|
86 | 99 | $this->tracer->pop(); |
87 | 100 | } |
88 | 101 | } |
@@ -92,26 +105,33 @@ discard block |
||
92 | 105 | return $binding; |
93 | 106 | } |
94 | 107 | |
95 | - if (\is_string($binding)) { |
|
108 | + if (\is_string($binding)) |
|
109 | + { |
|
96 | 110 | //Binding is pointing to something else |
97 | 111 | return $this->make($binding, $parameters, $context); |
98 | 112 | } |
99 | 113 | |
100 | 114 | unset($this->state->bindings[$alias]); |
101 | - try { |
|
115 | + try |
|
116 | + { |
|
102 | 117 | $instance = $binding[0] === $alias |
103 | 118 | ? $this->autowire($alias, $parameters, $context) |
104 | 119 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
105 | - } finally { |
|
120 | + } |
|
121 | + finally |
|
122 | + { |
|
106 | 123 | /** @psalm-var class-string $alias */ |
107 | 124 | $this->state->bindings[$alias] = $binding; |
108 | 125 | } |
109 | - } finally { |
|
126 | + } |
|
127 | + finally |
|
128 | + { |
|
110 | 129 | $this->tracer->pop(true); |
111 | 130 | $this->tracer->pop(false); |
112 | 131 | } |
113 | 132 | |
114 | - if ($binding[1]) { |
|
133 | + if ($binding[1]) |
|
134 | + { |
|
115 | 135 | // Indicates singleton |
116 | 136 | /** @psalm-var class-string $alias */ |
117 | 137 | $this->state->bindings[$alias] = $instance; |
@@ -162,18 +182,23 @@ discard block |
||
162 | 182 | array $parameters, |
163 | 183 | string $context = null |
164 | 184 | ): mixed { |
165 | - if (\is_string($target)) { |
|
185 | + if (\is_string($target)) |
|
186 | + { |
|
166 | 187 | // Reference |
167 | 188 | return $this->make($target, $parameters, $context); |
168 | 189 | } |
169 | 190 | |
170 | - if ($target instanceof Autowire) { |
|
191 | + if ($target instanceof Autowire) |
|
192 | + { |
|
171 | 193 | return $target->resolve($this, $parameters); |
172 | 194 | } |
173 | 195 | |
174 | - try { |
|
196 | + try |
|
197 | + { |
|
175 | 198 | return $this->invoker->invoke($target, $parameters); |
176 | - } catch (NotCallableException $e) { |
|
199 | + } |
|
200 | + catch (NotCallableException $e) |
|
201 | + { |
|
177 | 202 | throw new ContainerException( |
178 | 203 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
179 | 204 | $e->getCode(), |
@@ -197,20 +222,26 @@ discard block |
||
197 | 222 | */ |
198 | 223 | private function createInstance(string $class, array $parameters, string $context = null): object |
199 | 224 | { |
200 | - try { |
|
225 | + try |
|
226 | + { |
|
201 | 227 | $reflection = new \ReflectionClass($class); |
202 | - } catch (\ReflectionException $e) { |
|
228 | + } |
|
229 | + catch (\ReflectionException $e) |
|
230 | + { |
|
203 | 231 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
204 | 232 | } |
205 | 233 | |
206 | 234 | //We have to construct class using external injector when we know exact context |
207 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
235 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
236 | + { |
|
208 | 237 | $injector = $this->state->injectors[$reflection->getName()]; |
209 | 238 | |
210 | - try { |
|
239 | + try |
|
240 | + { |
|
211 | 241 | $injectorInstance = $this->container->get($injector); |
212 | 242 | |
213 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
243 | + if (!$injectorInstance instanceof InjectorInterface) |
|
244 | + { |
|
214 | 245 | throw new InjectionException( |
215 | 246 | \sprintf( |
216 | 247 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -222,7 +253,8 @@ discard block |
||
222 | 253 | |
223 | 254 | /** @var InjectorInterface<TObject> $injectorInstance */ |
224 | 255 | $instance = $injectorInstance->createInjection($reflection, $context); |
225 | - if (!$reflection->isInstance($instance)) { |
|
256 | + if (!$reflection->isInstance($instance)) |
|
257 | + { |
|
226 | 258 | throw new InjectionException( |
227 | 259 | \sprintf( |
228 | 260 | "Invalid injection response for '%s'.", |
@@ -232,12 +264,15 @@ discard block |
||
232 | 264 | } |
233 | 265 | |
234 | 266 | return $instance; |
235 | - } finally { |
|
267 | + } |
|
268 | + finally |
|
269 | + { |
|
236 | 270 | $this->state->injectors[$reflection->getName()] = $injector; |
237 | 271 | } |
238 | 272 | } |
239 | 273 | |
240 | - if (!$reflection->isInstantiable()) { |
|
274 | + if (!$reflection->isInstantiable()) |
|
275 | + { |
|
241 | 276 | $itIs = match (true) { |
242 | 277 | $reflection->isEnum() => 'Enum', |
243 | 278 | $reflection->isAbstract() => 'Abstract class', |
@@ -250,12 +285,16 @@ discard block |
||
250 | 285 | |
251 | 286 | $constructor = $reflection->getConstructor(); |
252 | 287 | |
253 | - if ($constructor !== null) { |
|
254 | - try { |
|
288 | + if ($constructor !== null) |
|
289 | + { |
|
290 | + try |
|
291 | + { |
|
255 | 292 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
256 | 293 | $this->tracer->push(true); |
257 | 294 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
258 | - } catch (ValidationException $e) { |
|
295 | + } |
|
296 | + catch (ValidationException $e) |
|
297 | + { |
|
259 | 298 | throw new ContainerException( |
260 | 299 | $this->tracer->combineTraceMessage( |
261 | 300 | \sprintf( |
@@ -265,22 +304,31 @@ discard block |
||
265 | 304 | ) |
266 | 305 | ), |
267 | 306 | ); |
268 | - } finally { |
|
307 | + } |
|
308 | + finally |
|
309 | + { |
|
269 | 310 | $this->tracer->pop(true); |
270 | 311 | $this->tracer->pop(false); |
271 | 312 | } |
272 | - try { |
|
313 | + try |
|
314 | + { |
|
273 | 315 | // Using constructor with resolved arguments |
274 | 316 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
275 | 317 | $this->tracer->push(true); |
276 | 318 | $instance = new $class(...$arguments); |
277 | - } catch (\TypeError $e) { |
|
319 | + } |
|
320 | + catch (\TypeError $e) |
|
321 | + { |
|
278 | 322 | throw new WrongTypeException($constructor, $e); |
279 | - } finally { |
|
323 | + } |
|
324 | + finally |
|
325 | + { |
|
280 | 326 | $this->tracer->pop(true); |
281 | 327 | $this->tracer->pop(false); |
282 | 328 | } |
283 | - } else { |
|
329 | + } |
|
330 | + else |
|
331 | + { |
|
284 | 332 | // No constructor specified |
285 | 333 | $instance = $reflection->newInstance(); |
286 | 334 | } |
@@ -298,9 +346,11 @@ discard block |
||
298 | 346 | private function registerInstance(object $instance, array $parameters): object |
299 | 347 | { |
300 | 348 | //Declarative singletons (only when class received via direct get) |
301 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
349 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
350 | + { |
|
302 | 351 | $alias = $instance::class; |
303 | - if (!isset($this->state->bindings[$alias])) { |
|
352 | + if (!isset($this->state->bindings[$alias])) |
|
353 | + { |
|
304 | 354 | $this->state->bindings[$alias] = $instance; |
305 | 355 | } |
306 | 356 | } |