@@ -77,9 +77,9 @@ |
||
77 | 77 | { |
78 | 78 | $this->expectException(ContainerException::class); |
79 | 79 | |
80 | - try { |
|
80 | + try{ |
|
81 | 81 | $container->get(ClassWithUndefinedDependency::class); |
82 | - } catch (ContainerException $e) { |
|
82 | + }catch (ContainerException $e){ |
|
83 | 83 | $this->assertSame( |
84 | 84 | \preg_replace('/\s+/', '', $message), |
85 | 85 | \preg_replace('/\s+/', '', $e->getMessage()) |
@@ -77,9 +77,12 @@ |
||
77 | 77 | { |
78 | 78 | $this->expectException(ContainerException::class); |
79 | 79 | |
80 | - try { |
|
80 | + try |
|
81 | + { |
|
81 | 82 | $container->get(ClassWithUndefinedDependency::class); |
82 | - } catch (ContainerException $e) { |
|
83 | + } |
|
84 | + catch (ContainerException $e) |
|
85 | + { |
|
83 | 86 | $this->assertSame( |
84 | 87 | \preg_replace('/\s+/', '', $message), |
85 | 88 | \preg_replace('/\s+/', '', $e->getMessage()) |
@@ -54,15 +54,15 @@ discard block |
||
54 | 54 | { |
55 | 55 | $this->trace[] = $alias; |
56 | 56 | |
57 | - if (!isset($this->state->bindings[$alias])) { |
|
57 | + if (!isset($this->state->bindings[$alias])){ |
|
58 | 58 | //No direct instructions how to construct class, make is automatically |
59 | 59 | return $this->autowire($alias, $parameters, $context); |
60 | 60 | } |
61 | 61 | |
62 | 62 | $binding = $this->state->bindings[$alias]; |
63 | - if (\is_object($binding)) { |
|
64 | - if ($binding::class === WeakReference::class) { |
|
65 | - if ($binding->get() === null && \class_exists($alias)) { |
|
63 | + if (\is_object($binding)){ |
|
64 | + if ($binding::class === WeakReference::class){ |
|
65 | + if ($binding->get() === null && \class_exists($alias)){ |
|
66 | 66 | $object = $this->createInstance($alias, $parameters, $context); |
67 | 67 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
68 | 68 | } |
@@ -72,23 +72,23 @@ discard block |
||
72 | 72 | return $binding; |
73 | 73 | } |
74 | 74 | |
75 | - if (\is_string($binding)) { |
|
75 | + if (\is_string($binding)){ |
|
76 | 76 | //Binding is pointing to something else |
77 | 77 | return $this->make($binding, $parameters, $context); |
78 | 78 | } |
79 | 79 | |
80 | 80 | unset($this->state->bindings[$alias]); |
81 | - try { |
|
81 | + try{ |
|
82 | 82 | $instance = $binding[0] === $alias |
83 | 83 | ? $this->autowire($alias, $parameters, $context) |
84 | 84 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
85 | - } finally { |
|
85 | + }finally{ |
|
86 | 86 | /** @psalm-var class-string $alias */ |
87 | 87 | $this->state->bindings[$alias] = $binding; |
88 | 88 | $this->trace = []; |
89 | 89 | } |
90 | 90 | |
91 | - if ($binding[1]) { |
|
91 | + if ($binding[1]){ |
|
92 | 92 | // Indicates singleton |
93 | 93 | /** @psalm-var class-string $alias */ |
94 | 94 | $this->state->bindings[$alias] = $instance; |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | */ |
108 | 108 | private function autowire(string $class, array $parameters, string $context = null): object |
109 | 109 | { |
110 | - if (!\class_exists($class) && !isset($this->state->injectors[$class])) { |
|
110 | + if (!\class_exists($class) && !isset($this->state->injectors[$class])){ |
|
111 | 111 | throw new NotFoundException( |
112 | 112 | message: \sprintf('Undefined class or binding `%s`.', $class), |
113 | 113 | trace: $this->trace |
@@ -133,18 +133,18 @@ discard block |
||
133 | 133 | array $parameters, |
134 | 134 | string $context = null |
135 | 135 | ): mixed { |
136 | - if (\is_string($target)) { |
|
136 | + if (\is_string($target)){ |
|
137 | 137 | // Reference |
138 | 138 | return $this->make($target, $parameters, $context); |
139 | 139 | } |
140 | 140 | |
141 | - if ($target instanceof Autowire) { |
|
141 | + if ($target instanceof Autowire){ |
|
142 | 142 | return $target->resolve($this, $parameters); |
143 | 143 | } |
144 | 144 | |
145 | - try { |
|
145 | + try{ |
|
146 | 146 | return $this->invoker->invoke($target, $parameters); |
147 | - } catch (NotCallableException $e) { |
|
147 | + }catch (NotCallableException $e){ |
|
148 | 148 | throw new ContainerException( |
149 | 149 | \sprintf('Invalid binding for `%s`.', $alias), |
150 | 150 | $e->getCode(), |
@@ -169,20 +169,20 @@ discard block |
||
169 | 169 | */ |
170 | 170 | private function createInstance(string $class, array $parameters, string $context = null): object |
171 | 171 | { |
172 | - try { |
|
172 | + try{ |
|
173 | 173 | $reflection = new \ReflectionClass($class); |
174 | - } catch (\ReflectionException $e) { |
|
174 | + }catch (\ReflectionException $e){ |
|
175 | 175 | throw new ContainerException($e->getMessage(), $e->getCode(), $e, $this->trace); |
176 | 176 | } |
177 | 177 | |
178 | 178 | //We have to construct class using external injector when we know exact context |
179 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
179 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
180 | 180 | $injector = $this->state->injectors[$reflection->getName()]; |
181 | 181 | |
182 | - try { |
|
182 | + try{ |
|
183 | 183 | $injectorInstance = $this->container->get($injector); |
184 | 184 | |
185 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
185 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
186 | 186 | throw new InjectionException( |
187 | 187 | \sprintf( |
188 | 188 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | |
195 | 195 | /** @var InjectorInterface<TObject> $injectorInstance */ |
196 | 196 | $instance = $injectorInstance->createInjection($reflection, $context); |
197 | - if (!$reflection->isInstance($instance)) { |
|
197 | + if (!$reflection->isInstance($instance)){ |
|
198 | 198 | throw new InjectionException( |
199 | 199 | \sprintf( |
200 | 200 | "Invalid injection response for '%s'.", |
@@ -204,12 +204,12 @@ discard block |
||
204 | 204 | } |
205 | 205 | |
206 | 206 | return $instance; |
207 | - } finally { |
|
207 | + }finally{ |
|
208 | 208 | $this->state->injectors[$reflection->getName()] = $injector; |
209 | 209 | } |
210 | 210 | } |
211 | 211 | |
212 | - if (!$reflection->isInstantiable()) { |
|
212 | + if (!$reflection->isInstantiable()){ |
|
213 | 213 | $itIs = match (true) { |
214 | 214 | $reflection->isEnum() => 'Enum', |
215 | 215 | $reflection->isAbstract() => 'Abstract class', |
@@ -223,14 +223,14 @@ discard block |
||
223 | 223 | |
224 | 224 | $constructor = $reflection->getConstructor(); |
225 | 225 | |
226 | - if ($constructor !== null) { |
|
227 | - try { |
|
226 | + if ($constructor !== null){ |
|
227 | + try{ |
|
228 | 228 | // Using constructor with resolved arguments |
229 | 229 | $instance = new $class(...$this->resolver->resolveArguments($constructor, $parameters)); |
230 | - } catch (\TypeError $e) { |
|
230 | + }catch (\TypeError $e){ |
|
231 | 231 | throw new WrongTypeException($constructor, $e); |
232 | 232 | } |
233 | - } else { |
|
233 | + }else{ |
|
234 | 234 | // No constructor specified |
235 | 235 | $instance = $reflection->newInstance(); |
236 | 236 | } |
@@ -248,9 +248,9 @@ discard block |
||
248 | 248 | private function registerInstance(object $instance, array $parameters): object |
249 | 249 | { |
250 | 250 | //Declarative singletons (only when class received via direct get) |
251 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
251 | + if ($parameters === [] && $instance instanceof SingletonInterface){ |
|
252 | 252 | $alias = $instance::class; |
253 | - if (!isset($this->state->bindings[$alias])) { |
|
253 | + if (!isset($this->state->bindings[$alias])){ |
|
254 | 254 | $this->state->bindings[$alias] = $instance; |
255 | 255 | } |
256 | 256 | } |
@@ -54,15 +54,19 @@ discard block |
||
54 | 54 | { |
55 | 55 | $this->trace[] = $alias; |
56 | 56 | |
57 | - if (!isset($this->state->bindings[$alias])) { |
|
57 | + if (!isset($this->state->bindings[$alias])) |
|
58 | + { |
|
58 | 59 | //No direct instructions how to construct class, make is automatically |
59 | 60 | return $this->autowire($alias, $parameters, $context); |
60 | 61 | } |
61 | 62 | |
62 | 63 | $binding = $this->state->bindings[$alias]; |
63 | - if (\is_object($binding)) { |
|
64 | - if ($binding::class === WeakReference::class) { |
|
65 | - if ($binding->get() === null && \class_exists($alias)) { |
|
64 | + if (\is_object($binding)) |
|
65 | + { |
|
66 | + if ($binding::class === WeakReference::class) |
|
67 | + { |
|
68 | + if ($binding->get() === null && \class_exists($alias)) |
|
69 | + { |
|
66 | 70 | $object = $this->createInstance($alias, $parameters, $context); |
67 | 71 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
68 | 72 | } |
@@ -72,23 +76,28 @@ discard block |
||
72 | 76 | return $binding; |
73 | 77 | } |
74 | 78 | |
75 | - if (\is_string($binding)) { |
|
79 | + if (\is_string($binding)) |
|
80 | + { |
|
76 | 81 | //Binding is pointing to something else |
77 | 82 | return $this->make($binding, $parameters, $context); |
78 | 83 | } |
79 | 84 | |
80 | 85 | unset($this->state->bindings[$alias]); |
81 | - try { |
|
86 | + try |
|
87 | + { |
|
82 | 88 | $instance = $binding[0] === $alias |
83 | 89 | ? $this->autowire($alias, $parameters, $context) |
84 | 90 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
85 | - } finally { |
|
91 | + } |
|
92 | + finally |
|
93 | + { |
|
86 | 94 | /** @psalm-var class-string $alias */ |
87 | 95 | $this->state->bindings[$alias] = $binding; |
88 | 96 | $this->trace = []; |
89 | 97 | } |
90 | 98 | |
91 | - if ($binding[1]) { |
|
99 | + if ($binding[1]) |
|
100 | + { |
|
92 | 101 | // Indicates singleton |
93 | 102 | /** @psalm-var class-string $alias */ |
94 | 103 | $this->state->bindings[$alias] = $instance; |
@@ -107,7 +116,8 @@ discard block |
||
107 | 116 | */ |
108 | 117 | private function autowire(string $class, array $parameters, string $context = null): object |
109 | 118 | { |
110 | - if (!\class_exists($class) && !isset($this->state->injectors[$class])) { |
|
119 | + if (!\class_exists($class) && !isset($this->state->injectors[$class])) |
|
120 | + { |
|
111 | 121 | throw new NotFoundException( |
112 | 122 | message: \sprintf('Undefined class or binding `%s`.', $class), |
113 | 123 | trace: $this->trace |
@@ -133,18 +143,23 @@ discard block |
||
133 | 143 | array $parameters, |
134 | 144 | string $context = null |
135 | 145 | ): mixed { |
136 | - if (\is_string($target)) { |
|
146 | + if (\is_string($target)) |
|
147 | + { |
|
137 | 148 | // Reference |
138 | 149 | return $this->make($target, $parameters, $context); |
139 | 150 | } |
140 | 151 | |
141 | - if ($target instanceof Autowire) { |
|
152 | + if ($target instanceof Autowire) |
|
153 | + { |
|
142 | 154 | return $target->resolve($this, $parameters); |
143 | 155 | } |
144 | 156 | |
145 | - try { |
|
157 | + try |
|
158 | + { |
|
146 | 159 | return $this->invoker->invoke($target, $parameters); |
147 | - } catch (NotCallableException $e) { |
|
160 | + } |
|
161 | + catch (NotCallableException $e) |
|
162 | + { |
|
148 | 163 | throw new ContainerException( |
149 | 164 | \sprintf('Invalid binding for `%s`.', $alias), |
150 | 165 | $e->getCode(), |
@@ -169,20 +184,26 @@ discard block |
||
169 | 184 | */ |
170 | 185 | private function createInstance(string $class, array $parameters, string $context = null): object |
171 | 186 | { |
172 | - try { |
|
187 | + try |
|
188 | + { |
|
173 | 189 | $reflection = new \ReflectionClass($class); |
174 | - } catch (\ReflectionException $e) { |
|
190 | + } |
|
191 | + catch (\ReflectionException $e) |
|
192 | + { |
|
175 | 193 | throw new ContainerException($e->getMessage(), $e->getCode(), $e, $this->trace); |
176 | 194 | } |
177 | 195 | |
178 | 196 | //We have to construct class using external injector when we know exact context |
179 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
197 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
198 | + { |
|
180 | 199 | $injector = $this->state->injectors[$reflection->getName()]; |
181 | 200 | |
182 | - try { |
|
201 | + try |
|
202 | + { |
|
183 | 203 | $injectorInstance = $this->container->get($injector); |
184 | 204 | |
185 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
205 | + if (!$injectorInstance instanceof InjectorInterface) |
|
206 | + { |
|
186 | 207 | throw new InjectionException( |
187 | 208 | \sprintf( |
188 | 209 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -194,7 +215,8 @@ discard block |
||
194 | 215 | |
195 | 216 | /** @var InjectorInterface<TObject> $injectorInstance */ |
196 | 217 | $instance = $injectorInstance->createInjection($reflection, $context); |
197 | - if (!$reflection->isInstance($instance)) { |
|
218 | + if (!$reflection->isInstance($instance)) |
|
219 | + { |
|
198 | 220 | throw new InjectionException( |
199 | 221 | \sprintf( |
200 | 222 | "Invalid injection response for '%s'.", |
@@ -204,12 +226,15 @@ discard block |
||
204 | 226 | } |
205 | 227 | |
206 | 228 | return $instance; |
207 | - } finally { |
|
229 | + } |
|
230 | + finally |
|
231 | + { |
|
208 | 232 | $this->state->injectors[$reflection->getName()] = $injector; |
209 | 233 | } |
210 | 234 | } |
211 | 235 | |
212 | - if (!$reflection->isInstantiable()) { |
|
236 | + if (!$reflection->isInstantiable()) |
|
237 | + { |
|
213 | 238 | $itIs = match (true) { |
214 | 239 | $reflection->isEnum() => 'Enum', |
215 | 240 | $reflection->isAbstract() => 'Abstract class', |
@@ -223,14 +248,20 @@ discard block |
||
223 | 248 | |
224 | 249 | $constructor = $reflection->getConstructor(); |
225 | 250 | |
226 | - if ($constructor !== null) { |
|
227 | - try { |
|
251 | + if ($constructor !== null) |
|
252 | + { |
|
253 | + try |
|
254 | + { |
|
228 | 255 | // Using constructor with resolved arguments |
229 | 256 | $instance = new $class(...$this->resolver->resolveArguments($constructor, $parameters)); |
230 | - } catch (\TypeError $e) { |
|
257 | + } |
|
258 | + catch (\TypeError $e) |
|
259 | + { |
|
231 | 260 | throw new WrongTypeException($constructor, $e); |
232 | 261 | } |
233 | - } else { |
|
262 | + } |
|
263 | + else |
|
264 | + { |
|
234 | 265 | // No constructor specified |
235 | 266 | $instance = $reflection->newInstance(); |
236 | 267 | } |
@@ -248,9 +279,11 @@ discard block |
||
248 | 279 | private function registerInstance(object $instance, array $parameters): object |
249 | 280 | { |
250 | 281 | //Declarative singletons (only when class received via direct get) |
251 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
282 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
283 | + { |
|
252 | 284 | $alias = $instance::class; |
253 | - if (!isset($this->state->bindings[$alias])) { |
|
285 | + if (!isset($this->state->bindings[$alias])) |
|
286 | + { |
|
254 | 287 | $this->state->bindings[$alias] = $instance; |
255 | 288 | } |
256 | 289 | } |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | int $code = 0, |
18 | 18 | ?\Throwable $previous = null, |
19 | 19 | protected array &$trace = [] |
20 | - ) { |
|
20 | + ){ |
|
21 | 21 | parent::__construct($this->addTrace($message), $code, $previous); |
22 | 22 | |
23 | 23 | $trace = []; |
@@ -28,10 +28,10 @@ discard block |
||
28 | 28 | $result = []; |
29 | 29 | $result[] = $message; |
30 | 30 | |
31 | - if ($this->trace !== []) { |
|
31 | + if ($this->trace !== []){ |
|
32 | 32 | $result[] = 'Container stack trace:'; |
33 | 33 | |
34 | - foreach ($this->trace as $item) { |
|
34 | + foreach ($this->trace as $item){ |
|
35 | 35 | $result[] = $item; |
36 | 36 | } |
37 | 37 | } |
@@ -28,10 +28,12 @@ |
||
28 | 28 | $result = []; |
29 | 29 | $result[] = $message; |
30 | 30 | |
31 | - if ($this->trace !== []) { |
|
31 | + if ($this->trace !== []) |
|
32 | + { |
|
32 | 33 | $result[] = 'Container stack trace:'; |
33 | 34 | |
34 | - foreach ($this->trace as $item) { |
|
35 | + foreach ($this->trace as $item) |
|
36 | + { |
|
35 | 37 | $result[] = $item; |
36 | 38 | } |
37 | 39 | } |