@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | { |
| 33 | 33 | public function __construct( |
| 34 | 34 | protected readonly State $state, |
| 35 | - ) { |
|
| 35 | + ){ |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | /** |
@@ -40,14 +40,14 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | public function bind(string $alias, mixed $resolver): void |
| 42 | 42 | { |
| 43 | - if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) { |
|
| 43 | + if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))){ |
|
| 44 | 44 | $this->state->inflectors[$alias][] = $resolver; |
| 45 | 45 | return; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - try { |
|
| 48 | + try{ |
|
| 49 | 49 | $config = $this->makeConfig($resolver, false); |
| 50 | - } catch (\Throwable $e) { |
|
| 50 | + }catch (\Throwable $e){ |
|
| 51 | 51 | throw $this->invalidBindingException($alias, $e); |
| 52 | 52 | } |
| 53 | 53 | |
@@ -59,15 +59,15 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function bindSingleton(string $alias, mixed $resolver): void |
| 61 | 61 | { |
| 62 | - if (isset($this->state->singletons[$alias])) { |
|
| 62 | + if (isset($this->state->singletons[$alias])){ |
|
| 63 | 63 | throw new SingletonOverloadException( |
| 64 | 64 | \sprintf('Can\'t overload the singleton `%s` because it\'s already used.', $alias) |
| 65 | 65 | ); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - try { |
|
| 68 | + try{ |
|
| 69 | 69 | $config = $this->makeConfig($resolver, true); |
| 70 | - } catch (\Throwable $e) { |
|
| 70 | + }catch (\Throwable $e){ |
|
| 71 | 71 | throw $this->invalidBindingException($alias, $e); |
| 72 | 72 | } |
| 73 | 73 | |
@@ -79,9 +79,9 @@ discard block |
||
| 79 | 79 | $bindings = &$this->state->bindings; |
| 80 | 80 | |
| 81 | 81 | $flags = []; |
| 82 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
| 82 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class){ |
|
| 83 | 83 | //Checking alias tree |
| 84 | - if ($flags[$binding->alias] ?? false) { |
|
| 84 | + if ($flags[$binding->alias] ?? false){ |
|
| 85 | 85 | return $binding->alias === $alias ?: throw new Exception('Circular alias detected'); |
| 86 | 86 | } |
| 87 | 87 | |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | public function removeInjector(string $class): void |
| 107 | 107 | { |
| 108 | 108 | unset($this->state->injectors[$class]); |
| 109 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
| 109 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class){ |
|
| 110 | 110 | return; |
| 111 | 111 | } |
| 112 | 112 | unset($this->state->bindings[$class]); |
@@ -114,20 +114,20 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | public function hasInjector(string $class): bool |
| 116 | 116 | { |
| 117 | - try { |
|
| 117 | + try{ |
|
| 118 | 118 | $reflection = new \ReflectionClass($class); |
| 119 | - } catch (\ReflectionException $e) { |
|
| 119 | + }catch (\ReflectionException $e){ |
|
| 120 | 120 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
| 123 | + if (\array_key_exists($class, $this->state->injectors)){ |
|
| 124 | 124 | return true; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | if ( |
| 128 | 128 | $reflection->implementsInterface(InjectableInterface::class) |
| 129 | 129 | && $reflection->hasConstant('INJECTOR') |
| 130 | - ) { |
|
| 130 | + ){ |
|
| 131 | 131 | $const = $reflection->getConstant('INJECTOR'); |
| 132 | 132 | $this->bindInjector($class, $const); |
| 133 | 133 | |
@@ -135,12 +135,12 @@ discard block |
||
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | // check interfaces |
| 138 | - foreach ($this->state->injectors as $target => $injector) { |
|
| 138 | + foreach ($this->state->injectors as $target => $injector){ |
|
| 139 | 139 | if ( |
| 140 | 140 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
| 141 | 141 | || |
| 142 | 142 | (\interface_exists($target, true) && $reflection->implementsInterface($target)) |
| 143 | - ) { |
|
| 143 | + ){ |
|
| 144 | 144 | $this->state->bindings[$class] = new Injectable($injector); |
| 145 | 145 | |
| 146 | 146 | return true; |
@@ -167,15 +167,15 @@ discard block |
||
| 167 | 167 | |
| 168 | 168 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
| 169 | 169 | { |
| 170 | - if (\is_callable($resolver)) { |
|
| 170 | + if (\is_callable($resolver)){ |
|
| 171 | 171 | return new Factory($resolver, $singleton); |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | // Validate lazy invokable array |
| 175 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
| 175 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === ''){ |
|
| 176 | 176 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 177 | 177 | } |
| 178 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
| 178 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === ''){ |
|
| 179 | 179 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 180 | 180 | } |
| 181 | 181 | |
@@ -40,14 +40,18 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | public function bind(string $alias, mixed $resolver): void |
| 42 | 42 | { |
| 43 | - if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) { |
|
| 43 | + if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) |
|
| 44 | + { |
|
| 44 | 45 | $this->state->inflectors[$alias][] = $resolver; |
| 45 | 46 | return; |
| 46 | 47 | } |
| 47 | 48 | |
| 48 | - try { |
|
| 49 | + try |
|
| 50 | + { |
|
| 49 | 51 | $config = $this->makeConfig($resolver, false); |
| 50 | - } catch (\Throwable $e) { |
|
| 52 | + } |
|
| 53 | + catch (\Throwable $e) |
|
| 54 | + { |
|
| 51 | 55 | throw $this->invalidBindingException($alias, $e); |
| 52 | 56 | } |
| 53 | 57 | |
@@ -59,15 +63,19 @@ discard block |
||
| 59 | 63 | */ |
| 60 | 64 | public function bindSingleton(string $alias, mixed $resolver): void |
| 61 | 65 | { |
| 62 | - if (isset($this->state->singletons[$alias])) { |
|
| 66 | + if (isset($this->state->singletons[$alias])) |
|
| 67 | + { |
|
| 63 | 68 | throw new SingletonOverloadException( |
| 64 | 69 | \sprintf('Can\'t overload the singleton `%s` because it\'s already used.', $alias) |
| 65 | 70 | ); |
| 66 | 71 | } |
| 67 | 72 | |
| 68 | - try { |
|
| 73 | + try |
|
| 74 | + { |
|
| 69 | 75 | $config = $this->makeConfig($resolver, true); |
| 70 | - } catch (\Throwable $e) { |
|
| 76 | + } |
|
| 77 | + catch (\Throwable $e) |
|
| 78 | + { |
|
| 71 | 79 | throw $this->invalidBindingException($alias, $e); |
| 72 | 80 | } |
| 73 | 81 | |
@@ -79,9 +87,11 @@ discard block |
||
| 79 | 87 | $bindings = &$this->state->bindings; |
| 80 | 88 | |
| 81 | 89 | $flags = []; |
| 82 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
| 90 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) |
|
| 91 | + { |
|
| 83 | 92 | //Checking alias tree |
| 84 | - if ($flags[$binding->alias] ?? false) { |
|
| 93 | + if ($flags[$binding->alias] ?? false) |
|
| 94 | + { |
|
| 85 | 95 | return $binding->alias === $alias ?: throw new Exception('Circular alias detected'); |
| 86 | 96 | } |
| 87 | 97 | |
@@ -106,7 +116,8 @@ discard block |
||
| 106 | 116 | public function removeInjector(string $class): void |
| 107 | 117 | { |
| 108 | 118 | unset($this->state->injectors[$class]); |
| 109 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
| 119 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) |
|
| 120 | + { |
|
| 110 | 121 | return; |
| 111 | 122 | } |
| 112 | 123 | unset($this->state->bindings[$class]); |
@@ -114,13 +125,17 @@ discard block |
||
| 114 | 125 | |
| 115 | 126 | public function hasInjector(string $class): bool |
| 116 | 127 | { |
| 117 | - try { |
|
| 128 | + try |
|
| 129 | + { |
|
| 118 | 130 | $reflection = new \ReflectionClass($class); |
| 119 | - } catch (\ReflectionException $e) { |
|
| 131 | + } |
|
| 132 | + catch (\ReflectionException $e) |
|
| 133 | + { |
|
| 120 | 134 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 121 | 135 | } |
| 122 | 136 | |
| 123 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
| 137 | + if (\array_key_exists($class, $this->state->injectors)) |
|
| 138 | + { |
|
| 124 | 139 | return true; |
| 125 | 140 | } |
| 126 | 141 | |
@@ -135,7 +150,8 @@ discard block |
||
| 135 | 150 | } |
| 136 | 151 | |
| 137 | 152 | // check interfaces |
| 138 | - foreach ($this->state->injectors as $target => $injector) { |
|
| 153 | + foreach ($this->state->injectors as $target => $injector) |
|
| 154 | + { |
|
| 139 | 155 | if ( |
| 140 | 156 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
| 141 | 157 | || |
@@ -167,15 +183,18 @@ discard block |
||
| 167 | 183 | |
| 168 | 184 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
| 169 | 185 | { |
| 170 | - if (\is_callable($resolver)) { |
|
| 186 | + if (\is_callable($resolver)) |
|
| 187 | + { |
|
| 171 | 188 | return new Factory($resolver, $singleton); |
| 172 | 189 | } |
| 173 | 190 | |
| 174 | 191 | // Validate lazy invokable array |
| 175 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
| 192 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') |
|
| 193 | + { |
|
| 176 | 194 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 177 | 195 | } |
| 178 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
| 196 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') |
|
| 197 | + { |
|
| 179 | 198 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 180 | 199 | } |
| 181 | 200 | |