@@ -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()) |
@@ -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 | } |
@@ -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 | \sprintf('Undefined class or binding `%s`.', $class), |
| 113 | 113 | 0, |
@@ -135,18 +135,18 @@ discard block |
||
| 135 | 135 | array $parameters, |
| 136 | 136 | string $context = null |
| 137 | 137 | ): mixed { |
| 138 | - if (\is_string($target)) { |
|
| 138 | + if (\is_string($target)){ |
|
| 139 | 139 | // Reference |
| 140 | 140 | return $this->make($target, $parameters, $context); |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - if ($target instanceof Autowire) { |
|
| 143 | + if ($target instanceof Autowire){ |
|
| 144 | 144 | return $target->resolve($this, $parameters); |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - try { |
|
| 147 | + try{ |
|
| 148 | 148 | return $this->invoker->invoke($target, $parameters); |
| 149 | - } catch (NotCallableException $e) { |
|
| 149 | + }catch (NotCallableException $e){ |
|
| 150 | 150 | throw new ContainerException( |
| 151 | 151 | \sprintf('Invalid binding for `%s`.', $alias), |
| 152 | 152 | $e->getCode(), |
@@ -171,20 +171,20 @@ discard block |
||
| 171 | 171 | */ |
| 172 | 172 | private function createInstance(string $class, array $parameters, string $context = null): object |
| 173 | 173 | { |
| 174 | - try { |
|
| 174 | + try{ |
|
| 175 | 175 | $reflection = new \ReflectionClass($class); |
| 176 | - } catch (\ReflectionException $e) { |
|
| 176 | + }catch (\ReflectionException $e){ |
|
| 177 | 177 | throw new ContainerException($e->getMessage(), $e->getCode(), $e, $this->trace); |
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | //We have to construct class using external injector when we know exact context |
| 181 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 181 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
| 182 | 182 | $injector = $this->state->injectors[$reflection->getName()]; |
| 183 | 183 | |
| 184 | - try { |
|
| 184 | + try{ |
|
| 185 | 185 | $injectorInstance = $this->container->get($injector); |
| 186 | 186 | |
| 187 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 187 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
| 188 | 188 | throw new InjectionException( |
| 189 | 189 | \sprintf( |
| 190 | 190 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | |
| 197 | 197 | /** @var InjectorInterface<TObject> $injectorInstance */ |
| 198 | 198 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 199 | - if (!$reflection->isInstance($instance)) { |
|
| 199 | + if (!$reflection->isInstance($instance)){ |
|
| 200 | 200 | throw new InjectionException( |
| 201 | 201 | \sprintf( |
| 202 | 202 | "Invalid injection response for '%s'.", |
@@ -206,12 +206,12 @@ discard block |
||
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | return $instance; |
| 209 | - } finally { |
|
| 209 | + }finally{ |
|
| 210 | 210 | $this->state->injectors[$reflection->getName()] = $injector; |
| 211 | 211 | } |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | - if (!$reflection->isInstantiable()) { |
|
| 214 | + if (!$reflection->isInstantiable()){ |
|
| 215 | 215 | $itIs = match (true) { |
| 216 | 216 | $reflection->isEnum() => 'Enum', |
| 217 | 217 | $reflection->isAbstract() => 'Abstract class', |
@@ -227,14 +227,14 @@ discard block |
||
| 227 | 227 | |
| 228 | 228 | $constructor = $reflection->getConstructor(); |
| 229 | 229 | |
| 230 | - if ($constructor !== null) { |
|
| 231 | - try { |
|
| 230 | + if ($constructor !== null){ |
|
| 231 | + try{ |
|
| 232 | 232 | // Using constructor with resolved arguments |
| 233 | 233 | $instance = new $class(...$this->resolver->resolveArguments($constructor, $parameters)); |
| 234 | - } catch (\TypeError $e) { |
|
| 234 | + }catch (\TypeError $e){ |
|
| 235 | 235 | throw new WrongTypeException($constructor, $e); |
| 236 | 236 | } |
| 237 | - } else { |
|
| 237 | + }else{ |
|
| 238 | 238 | // No constructor specified |
| 239 | 239 | $instance = $reflection->newInstance(); |
| 240 | 240 | } |
@@ -252,9 +252,9 @@ discard block |
||
| 252 | 252 | private function registerInstance(object $instance, array $parameters): object |
| 253 | 253 | { |
| 254 | 254 | //Declarative singletons (only when class received via direct get) |
| 255 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
| 255 | + if ($parameters === [] && $instance instanceof SingletonInterface){ |
|
| 256 | 256 | $alias = $instance::class; |
| 257 | - if (!isset($this->state->bindings[$alias])) { |
|
| 257 | + if (!isset($this->state->bindings[$alias])){ |
|
| 258 | 258 | $this->state->bindings[$alias] = $instance; |
| 259 | 259 | } |
| 260 | 260 | } |
@@ -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 | \sprintf('Undefined class or binding `%s`.', $class), |
| 113 | 123 | 0, |
@@ -135,18 +145,23 @@ discard block |
||
| 135 | 145 | array $parameters, |
| 136 | 146 | string $context = null |
| 137 | 147 | ): mixed { |
| 138 | - if (\is_string($target)) { |
|
| 148 | + if (\is_string($target)) |
|
| 149 | + { |
|
| 139 | 150 | // Reference |
| 140 | 151 | return $this->make($target, $parameters, $context); |
| 141 | 152 | } |
| 142 | 153 | |
| 143 | - if ($target instanceof Autowire) { |
|
| 154 | + if ($target instanceof Autowire) |
|
| 155 | + { |
|
| 144 | 156 | return $target->resolve($this, $parameters); |
| 145 | 157 | } |
| 146 | 158 | |
| 147 | - try { |
|
| 159 | + try |
|
| 160 | + { |
|
| 148 | 161 | return $this->invoker->invoke($target, $parameters); |
| 149 | - } catch (NotCallableException $e) { |
|
| 162 | + } |
|
| 163 | + catch (NotCallableException $e) |
|
| 164 | + { |
|
| 150 | 165 | throw new ContainerException( |
| 151 | 166 | \sprintf('Invalid binding for `%s`.', $alias), |
| 152 | 167 | $e->getCode(), |
@@ -171,20 +186,26 @@ discard block |
||
| 171 | 186 | */ |
| 172 | 187 | private function createInstance(string $class, array $parameters, string $context = null): object |
| 173 | 188 | { |
| 174 | - try { |
|
| 189 | + try |
|
| 190 | + { |
|
| 175 | 191 | $reflection = new \ReflectionClass($class); |
| 176 | - } catch (\ReflectionException $e) { |
|
| 192 | + } |
|
| 193 | + catch (\ReflectionException $e) |
|
| 194 | + { |
|
| 177 | 195 | throw new ContainerException($e->getMessage(), $e->getCode(), $e, $this->trace); |
| 178 | 196 | } |
| 179 | 197 | |
| 180 | 198 | //We have to construct class using external injector when we know exact context |
| 181 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 199 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
| 200 | + { |
|
| 182 | 201 | $injector = $this->state->injectors[$reflection->getName()]; |
| 183 | 202 | |
| 184 | - try { |
|
| 203 | + try |
|
| 204 | + { |
|
| 185 | 205 | $injectorInstance = $this->container->get($injector); |
| 186 | 206 | |
| 187 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 207 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 208 | + { |
|
| 188 | 209 | throw new InjectionException( |
| 189 | 210 | \sprintf( |
| 190 | 211 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -196,7 +217,8 @@ discard block |
||
| 196 | 217 | |
| 197 | 218 | /** @var InjectorInterface<TObject> $injectorInstance */ |
| 198 | 219 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 199 | - if (!$reflection->isInstance($instance)) { |
|
| 220 | + if (!$reflection->isInstance($instance)) |
|
| 221 | + { |
|
| 200 | 222 | throw new InjectionException( |
| 201 | 223 | \sprintf( |
| 202 | 224 | "Invalid injection response for '%s'.", |
@@ -206,12 +228,15 @@ discard block |
||
| 206 | 228 | } |
| 207 | 229 | |
| 208 | 230 | return $instance; |
| 209 | - } finally { |
|
| 231 | + } |
|
| 232 | + finally |
|
| 233 | + { |
|
| 210 | 234 | $this->state->injectors[$reflection->getName()] = $injector; |
| 211 | 235 | } |
| 212 | 236 | } |
| 213 | 237 | |
| 214 | - if (!$reflection->isInstantiable()) { |
|
| 238 | + if (!$reflection->isInstantiable()) |
|
| 239 | + { |
|
| 215 | 240 | $itIs = match (true) { |
| 216 | 241 | $reflection->isEnum() => 'Enum', |
| 217 | 242 | $reflection->isAbstract() => 'Abstract class', |
@@ -227,14 +252,20 @@ discard block |
||
| 227 | 252 | |
| 228 | 253 | $constructor = $reflection->getConstructor(); |
| 229 | 254 | |
| 230 | - if ($constructor !== null) { |
|
| 231 | - try { |
|
| 255 | + if ($constructor !== null) |
|
| 256 | + { |
|
| 257 | + try |
|
| 258 | + { |
|
| 232 | 259 | // Using constructor with resolved arguments |
| 233 | 260 | $instance = new $class(...$this->resolver->resolveArguments($constructor, $parameters)); |
| 234 | - } catch (\TypeError $e) { |
|
| 261 | + } |
|
| 262 | + catch (\TypeError $e) |
|
| 263 | + { |
|
| 235 | 264 | throw new WrongTypeException($constructor, $e); |
| 236 | 265 | } |
| 237 | - } else { |
|
| 266 | + } |
|
| 267 | + else |
|
| 268 | + { |
|
| 238 | 269 | // No constructor specified |
| 239 | 270 | $instance = $reflection->newInstance(); |
| 240 | 271 | } |
@@ -252,9 +283,11 @@ discard block |
||
| 252 | 283 | private function registerInstance(object $instance, array $parameters): object |
| 253 | 284 | { |
| 254 | 285 | //Declarative singletons (only when class received via direct get) |
| 255 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
| 286 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
| 287 | + { |
|
| 256 | 288 | $alias = $instance::class; |
| 257 | - if (!isset($this->state->bindings[$alias])) { |
|
| 289 | + if (!isset($this->state->bindings[$alias])) |
|
| 290 | + { |
|
| 258 | 291 | $this->state->bindings[$alias] = $instance; |
| 259 | 292 | } |
| 260 | 293 | } |