@@ -11,7 +11,7 @@ |
||
| 11 | 11 | { |
| 12 | 12 | public function __construct( |
| 13 | 13 | public \WeakReference $reference, |
| 14 | - ) { |
|
| 14 | + ){ |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | public function __toString(): string |
@@ -11,11 +11,11 @@ |
||
| 11 | 11 | { |
| 12 | 12 | public function __construct( |
| 13 | 13 | public readonly object $value, |
| 14 | - ) { |
|
| 14 | + ){ |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | public function __toString(): string |
| 18 | 18 | { |
| 19 | - return 'Shared object of class ' . $this->value::class; |
|
| 19 | + return 'Shared object of class '.$this->value::class; |
|
| 20 | 20 | } |
| 21 | 21 | } |
@@ -12,7 +12,7 @@ |
||
| 12 | 12 | public function __construct( |
| 13 | 13 | public readonly string $alias, |
| 14 | 14 | public readonly bool $singleton = false, |
| 15 | - ) { |
|
| 15 | + ){ |
|
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | public function __toString(): string |
@@ -15,7 +15,7 @@ |
||
| 15 | 15 | public function __construct( |
| 16 | 16 | public readonly array $factory, |
| 17 | 17 | public readonly bool $singleton = false, |
| 18 | - ) { |
|
| 18 | + ){ |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | public function __toString(): string |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | public function __construct( |
| 15 | 15 | public readonly AutowireAlias $autowire, |
| 16 | 16 | public readonly bool $singleton = false, |
| 17 | - ) { |
|
| 17 | + ){ |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | public function __toString(): string |
@@ -49,11 +49,11 @@ discard block |
||
| 49 | 49 | public const DEFAULT_ROOT_SCOPE_NAME = 'root'; |
| 50 | 50 | |
| 51 | 51 | private Internal\State $state; |
| 52 | - private ResolverInterface|Internal\Resolver $resolver; |
|
| 53 | - private FactoryInterface|Internal\Factory $factory; |
|
| 54 | - private ContainerInterface|Internal\Container $container; |
|
| 55 | - private BinderInterface|Internal\Binder $binder; |
|
| 56 | - private InvokerInterface|Internal\Invoker $invoker; |
|
| 52 | + private ResolverInterface | Internal\Resolver $resolver; |
|
| 53 | + private FactoryInterface | Internal\Factory $factory; |
|
| 54 | + private ContainerInterface | Internal\Container $container; |
|
| 55 | + private BinderInterface | Internal\Binder $binder; |
|
| 56 | + private InvokerInterface | Internal\Invoker $invoker; |
|
| 57 | 57 | private Internal\Scope $scope; |
| 58 | 58 | |
| 59 | 59 | /** |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | public function __construct( |
| 63 | 63 | private Config $config = new Config(), |
| 64 | 64 | ?string $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
| 65 | - ) { |
|
| 65 | + ){ |
|
| 66 | 66 | $this->initServices($this, $scopeName); |
| 67 | 67 | |
| 68 | 68 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | * |
| 141 | 141 | * @psalm-suppress InvalidArgument, InvalidCast |
| 142 | 142 | */ |
| 143 | - public function get(string|Autowire $id, string $context = null): mixed |
|
| 143 | + public function get(string | Autowire $id, string $context = null): mixed |
|
| 144 | 144 | { |
| 145 | 145 | /** @psalm-suppress TooManyArguments */ |
| 146 | 146 | return $this->container->get($id, $context); |
@@ -168,16 +168,16 @@ discard block |
||
| 168 | 168 | $binds = &$this->state->bindings; |
| 169 | 169 | $singletons = &$this->state->singletons; |
| 170 | 170 | $cleanup = $previous = $prevSin = []; |
| 171 | - foreach ($bindings as $alias => $resolver) { |
|
| 171 | + foreach ($bindings as $alias => $resolver){ |
|
| 172 | 172 | // Store previous bindings |
| 173 | - if (isset($binds[$alias])) { |
|
| 173 | + if (isset($binds[$alias])){ |
|
| 174 | 174 | $previous[$alias] = $binds[$alias]; |
| 175 | - } else { |
|
| 175 | + }else{ |
|
| 176 | 176 | // Store bindings to be removed |
| 177 | 177 | $cleanup[] = $alias; |
| 178 | 178 | } |
| 179 | 179 | // Store previous singletons |
| 180 | - if (isset($singletons[$alias])) { |
|
| 180 | + if (isset($singletons[$alias])){ |
|
| 181 | 181 | $prevSin[$alias] = $singletons[$alias]; |
| 182 | 182 | unset($singletons[$alias]); |
| 183 | 183 | } |
@@ -185,21 +185,21 @@ discard block |
||
| 185 | 185 | $this->binder->bind($alias, $resolver); |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | - try { |
|
| 188 | + try{ |
|
| 189 | 189 | return ContainerScope::getContainer() !== $this |
| 190 | 190 | ? ContainerScope::runScope($this, $scope) |
| 191 | 191 | : $scope($this); |
| 192 | - } finally { |
|
| 192 | + }finally{ |
|
| 193 | 193 | // Remove new bindings |
| 194 | - foreach ($cleanup as $alias) { |
|
| 194 | + foreach ($cleanup as $alias){ |
|
| 195 | 195 | unset($binds[$alias], $singletons[$alias]); |
| 196 | 196 | } |
| 197 | 197 | // Restore previous bindings |
| 198 | - foreach ($previous as $alias => $resolver) { |
|
| 198 | + foreach ($previous as $alias => $resolver){ |
|
| 199 | 199 | $binds[$alias] = $resolver; |
| 200 | 200 | } |
| 201 | 201 | // Restore singletons |
| 202 | - foreach ($prevSin as $alias => $instance) { |
|
| 202 | + foreach ($prevSin as $alias => $instance){ |
|
| 203 | 203 | $singletons[$alias] = $instance; |
| 204 | 204 | } |
| 205 | 205 | } |
@@ -213,32 +213,32 @@ discard block |
||
| 213 | 213 | // Open scope |
| 214 | 214 | $container = new self($this->config, $name); |
| 215 | 215 | |
| 216 | - try { |
|
| 216 | + try{ |
|
| 217 | 217 | // Configure scope |
| 218 | 218 | $container->scope->setParent($this, $this->scope); |
| 219 | 219 | |
| 220 | 220 | // Add specific bindings |
| 221 | - foreach ($bindings as $alias => $resolver) { |
|
| 221 | + foreach ($bindings as $alias => $resolver){ |
|
| 222 | 222 | $container->binder->bind($alias, $resolver); |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | return ContainerScope::runScope( |
| 226 | 226 | $container, |
| 227 | 227 | static function (self $container) use ($autowire, $closure): mixed { |
| 228 | - try { |
|
| 228 | + try{ |
|
| 229 | 229 | return $autowire |
| 230 | 230 | ? $container->invoke($closure) |
| 231 | 231 | : $closure($container); |
| 232 | - } finally { |
|
| 232 | + }finally{ |
|
| 233 | 233 | $container->closeScope(); |
| 234 | 234 | } |
| 235 | 235 | } |
| 236 | 236 | ); |
| 237 | - } finally { |
|
| 237 | + }finally{ |
|
| 238 | 238 | // Check the container has not been leaked |
| 239 | 239 | $link = \WeakReference::create($container); |
| 240 | 240 | unset($container); |
| 241 | - if ($link->get() !== null) { |
|
| 241 | + if ($link->get() !== null){ |
|
| 242 | 242 | throw new ScopeContainerLeakedException($name, $this->scope->getParentScopeNames()); |
| 243 | 243 | } |
| 244 | 244 | } |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | * |
| 271 | 271 | * @psalm-param TResolver $resolver |
| 272 | 272 | */ |
| 273 | - public function bindSingleton(string $alias, string|array|callable|object $resolver): void |
|
| 273 | + public function bindSingleton(string $alias, string | array | callable | object $resolver): void |
|
| 274 | 274 | { |
| 275 | 275 | $this->binder->bindSingleton($alias, $resolver); |
| 276 | 276 | } |
@@ -340,8 +340,8 @@ discard block |
||
| 340 | 340 | ]); |
| 341 | 341 | |
| 342 | 342 | // Create container services |
| 343 | - foreach ($container->config as $property => $class) { |
|
| 344 | - if (\property_exists($container, $property)) { |
|
| 343 | + foreach ($container->config as $property => $class){ |
|
| 344 | + if (\property_exists($container, $property)){ |
|
| 345 | 345 | $container->$property = $constructor->get($property, $class); |
| 346 | 346 | } |
| 347 | 347 | } |
@@ -355,7 +355,7 @@ discard block |
||
| 355 | 355 | private function closeScope(): void |
| 356 | 356 | { |
| 357 | 357 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
| 358 | - if (!isset($this->scope)) { |
|
| 358 | + if (!isset($this->scope)){ |
|
| 359 | 359 | $this->destruct(); |
| 360 | 360 | return; |
| 361 | 361 | } |
@@ -364,10 +364,10 @@ discard block |
||
| 364 | 364 | |
| 365 | 365 | // Run finalizers |
| 366 | 366 | $errors = []; |
| 367 | - foreach ($this->state->finalizers as $finalizer) { |
|
| 368 | - try { |
|
| 367 | + foreach ($this->state->finalizers as $finalizer){ |
|
| 368 | + try{ |
|
| 369 | 369 | $this->invoker->invoke($finalizer); |
| 370 | - } catch (\Throwable $e) { |
|
| 370 | + }catch (\Throwable $e){ |
|
| 371 | 371 | $errors[] = $e; |
| 372 | 372 | } |
| 373 | 373 | } |
@@ -376,7 +376,7 @@ discard block |
||
| 376 | 376 | $this->destruct(); |
| 377 | 377 | |
| 378 | 378 | // Throw collected errors |
| 379 | - if ($errors !== []) { |
|
| 379 | + if ($errors !== []){ |
|
| 380 | 380 | throw new FinalizersException($scopeName, $errors); |
| 381 | 381 | } |
| 382 | 382 | } |
@@ -168,16 +168,21 @@ discard block |
||
| 168 | 168 | $binds = &$this->state->bindings; |
| 169 | 169 | $singletons = &$this->state->singletons; |
| 170 | 170 | $cleanup = $previous = $prevSin = []; |
| 171 | - foreach ($bindings as $alias => $resolver) { |
|
| 171 | + foreach ($bindings as $alias => $resolver) |
|
| 172 | + { |
|
| 172 | 173 | // Store previous bindings |
| 173 | - if (isset($binds[$alias])) { |
|
| 174 | + if (isset($binds[$alias])) |
|
| 175 | + { |
|
| 174 | 176 | $previous[$alias] = $binds[$alias]; |
| 175 | - } else { |
|
| 177 | + } |
|
| 178 | + else |
|
| 179 | + { |
|
| 176 | 180 | // Store bindings to be removed |
| 177 | 181 | $cleanup[] = $alias; |
| 178 | 182 | } |
| 179 | 183 | // Store previous singletons |
| 180 | - if (isset($singletons[$alias])) { |
|
| 184 | + if (isset($singletons[$alias])) |
|
| 185 | + { |
|
| 181 | 186 | $prevSin[$alias] = $singletons[$alias]; |
| 182 | 187 | unset($singletons[$alias]); |
| 183 | 188 | } |
@@ -185,21 +190,27 @@ discard block |
||
| 185 | 190 | $this->binder->bind($alias, $resolver); |
| 186 | 191 | } |
| 187 | 192 | |
| 188 | - try { |
|
| 193 | + try |
|
| 194 | + { |
|
| 189 | 195 | return ContainerScope::getContainer() !== $this |
| 190 | 196 | ? ContainerScope::runScope($this, $scope) |
| 191 | 197 | : $scope($this); |
| 192 | - } finally { |
|
| 198 | + } |
|
| 199 | + finally |
|
| 200 | + { |
|
| 193 | 201 | // Remove new bindings |
| 194 | - foreach ($cleanup as $alias) { |
|
| 202 | + foreach ($cleanup as $alias) |
|
| 203 | + { |
|
| 195 | 204 | unset($binds[$alias], $singletons[$alias]); |
| 196 | 205 | } |
| 197 | 206 | // Restore previous bindings |
| 198 | - foreach ($previous as $alias => $resolver) { |
|
| 207 | + foreach ($previous as $alias => $resolver) |
|
| 208 | + { |
|
| 199 | 209 | $binds[$alias] = $resolver; |
| 200 | 210 | } |
| 201 | 211 | // Restore singletons |
| 202 | - foreach ($prevSin as $alias => $instance) { |
|
| 212 | + foreach ($prevSin as $alias => $instance) |
|
| 213 | + { |
|
| 203 | 214 | $singletons[$alias] = $instance; |
| 204 | 215 | } |
| 205 | 216 | } |
@@ -213,32 +224,40 @@ discard block |
||
| 213 | 224 | // Open scope |
| 214 | 225 | $container = new self($this->config, $name); |
| 215 | 226 | |
| 216 | - try { |
|
| 227 | + try |
|
| 228 | + { |
|
| 217 | 229 | // Configure scope |
| 218 | 230 | $container->scope->setParent($this, $this->scope); |
| 219 | 231 | |
| 220 | 232 | // Add specific bindings |
| 221 | - foreach ($bindings as $alias => $resolver) { |
|
| 233 | + foreach ($bindings as $alias => $resolver) |
|
| 234 | + { |
|
| 222 | 235 | $container->binder->bind($alias, $resolver); |
| 223 | 236 | } |
| 224 | 237 | |
| 225 | 238 | return ContainerScope::runScope( |
| 226 | 239 | $container, |
| 227 | 240 | static function (self $container) use ($autowire, $closure): mixed { |
| 228 | - try { |
|
| 241 | + try |
|
| 242 | + { |
|
| 229 | 243 | return $autowire |
| 230 | 244 | ? $container->invoke($closure) |
| 231 | 245 | : $closure($container); |
| 232 | - } finally { |
|
| 246 | + } |
|
| 247 | + finally |
|
| 248 | + { |
|
| 233 | 249 | $container->closeScope(); |
| 234 | 250 | } |
| 235 | 251 | } |
| 236 | 252 | ); |
| 237 | - } finally { |
|
| 253 | + } |
|
| 254 | + finally |
|
| 255 | + { |
|
| 238 | 256 | // Check the container has not been leaked |
| 239 | 257 | $link = \WeakReference::create($container); |
| 240 | 258 | unset($container); |
| 241 | - if ($link->get() !== null) { |
|
| 259 | + if ($link->get() !== null) |
|
| 260 | + { |
|
| 242 | 261 | throw new ScopeContainerLeakedException($name, $this->scope->getParentScopeNames()); |
| 243 | 262 | } |
| 244 | 263 | } |
@@ -340,8 +359,10 @@ discard block |
||
| 340 | 359 | ]); |
| 341 | 360 | |
| 342 | 361 | // Create container services |
| 343 | - foreach ($container->config as $property => $class) { |
|
| 344 | - if (\property_exists($container, $property)) { |
|
| 362 | + foreach ($container->config as $property => $class) |
|
| 363 | + { |
|
| 364 | + if (\property_exists($container, $property)) |
|
| 365 | + { |
|
| 345 | 366 | $container->$property = $constructor->get($property, $class); |
| 346 | 367 | } |
| 347 | 368 | } |
@@ -355,7 +376,8 @@ discard block |
||
| 355 | 376 | private function closeScope(): void |
| 356 | 377 | { |
| 357 | 378 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
| 358 | - if (!isset($this->scope)) { |
|
| 379 | + if (!isset($this->scope)) |
|
| 380 | + { |
|
| 359 | 381 | $this->destruct(); |
| 360 | 382 | return; |
| 361 | 383 | } |
@@ -364,10 +386,14 @@ discard block |
||
| 364 | 386 | |
| 365 | 387 | // Run finalizers |
| 366 | 388 | $errors = []; |
| 367 | - foreach ($this->state->finalizers as $finalizer) { |
|
| 368 | - try { |
|
| 389 | + foreach ($this->state->finalizers as $finalizer) |
|
| 390 | + { |
|
| 391 | + try |
|
| 392 | + { |
|
| 369 | 393 | $this->invoker->invoke($finalizer); |
| 370 | - } catch (\Throwable $e) { |
|
| 394 | + } |
|
| 395 | + catch (\Throwable $e) |
|
| 396 | + { |
|
| 371 | 397 | $errors[] = $e; |
| 372 | 398 | } |
| 373 | 399 | } |
@@ -376,7 +402,8 @@ discard block |
||
| 376 | 402 | $this->destruct(); |
| 377 | 403 | |
| 378 | 404 | // Throw collected errors |
| 379 | - if ($errors !== []) { |
|
| 405 | + if ($errors !== []) |
|
| 406 | + { |
|
| 380 | 407 | throw new FinalizersException($scopeName, $errors); |
| 381 | 408 | } |
| 382 | 409 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | { |
| 32 | 32 | public function __construct( |
| 33 | 33 | protected readonly State $state, |
| 34 | - ) { |
|
| 34 | + ){ |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -39,14 +39,14 @@ discard block |
||
| 39 | 39 | */ |
| 40 | 40 | public function bind(string $alias, mixed $resolver): void |
| 41 | 41 | { |
| 42 | - if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) { |
|
| 42 | + if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))){ |
|
| 43 | 43 | $this->state->inflectors[$alias][] = $resolver; |
| 44 | 44 | return; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - try { |
|
| 47 | + try{ |
|
| 48 | 48 | $config = $this->makeConfig($resolver, false); |
| 49 | - } catch (\Throwable $e) { |
|
| 49 | + }catch (\Throwable $e){ |
|
| 50 | 50 | throw $this->invalidBindingException($alias, $e); |
| 51 | 51 | } |
| 52 | 52 | |
@@ -58,9 +58,9 @@ discard block |
||
| 58 | 58 | */ |
| 59 | 59 | public function bindSingleton(string $alias, mixed $resolver): void |
| 60 | 60 | { |
| 61 | - try { |
|
| 61 | + try{ |
|
| 62 | 62 | $config = $this->makeConfig($resolver, true); |
| 63 | - } catch (\Throwable $e) { |
|
| 63 | + }catch (\Throwable $e){ |
|
| 64 | 64 | throw $this->invalidBindingException($alias, $e); |
| 65 | 65 | } |
| 66 | 66 | |
@@ -72,9 +72,9 @@ discard block |
||
| 72 | 72 | $bindings = &$this->state->bindings; |
| 73 | 73 | |
| 74 | 74 | $flags = []; |
| 75 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
| 75 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class){ |
|
| 76 | 76 | //Checking alias tree |
| 77 | - if ($flags[$binding->alias] ?? false) { |
|
| 77 | + if ($flags[$binding->alias] ?? false){ |
|
| 78 | 78 | return $binding->alias === $alias ?: throw new Exception('Circular alias detected'); |
| 79 | 79 | } |
| 80 | 80 | |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | public function removeInjector(string $class): void |
| 100 | 100 | { |
| 101 | 101 | unset($this->state->injectors[$class]); |
| 102 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
| 102 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class){ |
|
| 103 | 103 | return; |
| 104 | 104 | } |
| 105 | 105 | unset($this->state->bindings[$class]); |
@@ -107,20 +107,20 @@ discard block |
||
| 107 | 107 | |
| 108 | 108 | public function hasInjector(string $class): bool |
| 109 | 109 | { |
| 110 | - try { |
|
| 110 | + try{ |
|
| 111 | 111 | $reflection = new \ReflectionClass($class); |
| 112 | - } catch (\ReflectionException $e) { |
|
| 112 | + }catch (\ReflectionException $e){ |
|
| 113 | 113 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
| 116 | + if (\array_key_exists($class, $this->state->injectors)){ |
|
| 117 | 117 | return true; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | if ( |
| 121 | 121 | $reflection->implementsInterface(InjectableInterface::class) |
| 122 | 122 | && $reflection->hasConstant('INJECTOR') |
| 123 | - ) { |
|
| 123 | + ){ |
|
| 124 | 124 | $const = $reflection->getConstant('INJECTOR'); |
| 125 | 125 | $this->bindInjector($class, $const); |
| 126 | 126 | |
@@ -128,12 +128,12 @@ discard block |
||
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | // check interfaces |
| 131 | - foreach ($this->state->injectors as $target => $injector) { |
|
| 131 | + foreach ($this->state->injectors as $target => $injector){ |
|
| 132 | 132 | if ( |
| 133 | 133 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
| 134 | 134 | || |
| 135 | 135 | (\interface_exists($target, true) && $reflection->implementsInterface($target)) |
| 136 | - ) { |
|
| 136 | + ){ |
|
| 137 | 137 | $this->state->bindings[$class] = new Injectable($injector); |
| 138 | 138 | |
| 139 | 139 | return true; |
@@ -160,15 +160,15 @@ discard block |
||
| 160 | 160 | |
| 161 | 161 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
| 162 | 162 | { |
| 163 | - if (\is_callable($resolver)) { |
|
| 163 | + if (\is_callable($resolver)){ |
|
| 164 | 164 | return new Factory($resolver, $singleton); |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | // Validate lazy invokable array |
| 168 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
| 168 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === ''){ |
|
| 169 | 169 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 170 | 170 | } |
| 171 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
| 171 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === ''){ |
|
| 172 | 172 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 173 | 173 | } |
| 174 | 174 | |
@@ -39,14 +39,18 @@ discard block |
||
| 39 | 39 | */ |
| 40 | 40 | public function bind(string $alias, mixed $resolver): void |
| 41 | 41 | { |
| 42 | - if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) { |
|
| 42 | + if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) |
|
| 43 | + { |
|
| 43 | 44 | $this->state->inflectors[$alias][] = $resolver; |
| 44 | 45 | return; |
| 45 | 46 | } |
| 46 | 47 | |
| 47 | - try { |
|
| 48 | + try |
|
| 49 | + { |
|
| 48 | 50 | $config = $this->makeConfig($resolver, false); |
| 49 | - } catch (\Throwable $e) { |
|
| 51 | + } |
|
| 52 | + catch (\Throwable $e) |
|
| 53 | + { |
|
| 50 | 54 | throw $this->invalidBindingException($alias, $e); |
| 51 | 55 | } |
| 52 | 56 | |
@@ -58,9 +62,12 @@ discard block |
||
| 58 | 62 | */ |
| 59 | 63 | public function bindSingleton(string $alias, mixed $resolver): void |
| 60 | 64 | { |
| 61 | - try { |
|
| 65 | + try |
|
| 66 | + { |
|
| 62 | 67 | $config = $this->makeConfig($resolver, true); |
| 63 | - } catch (\Throwable $e) { |
|
| 68 | + } |
|
| 69 | + catch (\Throwable $e) |
|
| 70 | + { |
|
| 64 | 71 | throw $this->invalidBindingException($alias, $e); |
| 65 | 72 | } |
| 66 | 73 | |
@@ -72,9 +79,11 @@ discard block |
||
| 72 | 79 | $bindings = &$this->state->bindings; |
| 73 | 80 | |
| 74 | 81 | $flags = []; |
| 75 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
| 82 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) |
|
| 83 | + { |
|
| 76 | 84 | //Checking alias tree |
| 77 | - if ($flags[$binding->alias] ?? false) { |
|
| 85 | + if ($flags[$binding->alias] ?? false) |
|
| 86 | + { |
|
| 78 | 87 | return $binding->alias === $alias ?: throw new Exception('Circular alias detected'); |
| 79 | 88 | } |
| 80 | 89 | |
@@ -99,7 +108,8 @@ discard block |
||
| 99 | 108 | public function removeInjector(string $class): void |
| 100 | 109 | { |
| 101 | 110 | unset($this->state->injectors[$class]); |
| 102 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
| 111 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) |
|
| 112 | + { |
|
| 103 | 113 | return; |
| 104 | 114 | } |
| 105 | 115 | unset($this->state->bindings[$class]); |
@@ -107,13 +117,17 @@ discard block |
||
| 107 | 117 | |
| 108 | 118 | public function hasInjector(string $class): bool |
| 109 | 119 | { |
| 110 | - try { |
|
| 120 | + try |
|
| 121 | + { |
|
| 111 | 122 | $reflection = new \ReflectionClass($class); |
| 112 | - } catch (\ReflectionException $e) { |
|
| 123 | + } |
|
| 124 | + catch (\ReflectionException $e) |
|
| 125 | + { |
|
| 113 | 126 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 114 | 127 | } |
| 115 | 128 | |
| 116 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
| 129 | + if (\array_key_exists($class, $this->state->injectors)) |
|
| 130 | + { |
|
| 117 | 131 | return true; |
| 118 | 132 | } |
| 119 | 133 | |
@@ -128,7 +142,8 @@ discard block |
||
| 128 | 142 | } |
| 129 | 143 | |
| 130 | 144 | // check interfaces |
| 131 | - foreach ($this->state->injectors as $target => $injector) { |
|
| 145 | + foreach ($this->state->injectors as $target => $injector) |
|
| 146 | + { |
|
| 132 | 147 | if ( |
| 133 | 148 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
| 134 | 149 | || |
@@ -160,15 +175,18 @@ discard block |
||
| 160 | 175 | |
| 161 | 176 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
| 162 | 177 | { |
| 163 | - if (\is_callable($resolver)) { |
|
| 178 | + if (\is_callable($resolver)) |
|
| 179 | + { |
|
| 164 | 180 | return new Factory($resolver, $singleton); |
| 165 | 181 | } |
| 166 | 182 | |
| 167 | 183 | // Validate lazy invokable array |
| 168 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
| 184 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') |
|
| 185 | + { |
|
| 169 | 186 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 170 | 187 | } |
| 171 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
| 188 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') |
|
| 189 | + { |
|
| 172 | 190 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 173 | 191 | } |
| 174 | 192 | |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | public function __construct( |
| 19 | 19 | callable $callable, |
| 20 | 20 | public readonly bool $singleton = false, |
| 21 | - ) { |
|
| 21 | + ){ |
|
| 22 | 22 | $this->factory = $callable(...); |
| 23 | 23 | $this->parametersCount = (new \ReflectionFunction($this->factory))->getNumberOfParameters(); |
| 24 | 24 | /** @psalm-suppress TypeDoesNotContainType */ |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | \is_object($callable[0]) ? $callable[0]::class : $callable[0], |
| 30 | 30 | $callable[1], |
| 31 | 31 | ), |
| 32 | - \is_object($callable) && $callable::class !== \Closure::class => 'object ' . $callable::class, |
|
| 32 | + \is_object($callable) && $callable::class !== \Closure::class => 'object '.$callable::class, |
|
| 33 | 33 | default => null, |
| 34 | 34 | }; |
| 35 | 35 | } |