@@ -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 | } |