@@ -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,9 +59,9 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | public function bindSingleton(string $alias, mixed $resolver): void |
| 61 | 61 | { |
| 62 | - try { |
|
| 62 | + try{ |
|
| 63 | 63 | $config = $this->makeConfig($resolver, true); |
| 64 | - } catch (\Throwable $e) { |
|
| 64 | + }catch (\Throwable $e){ |
|
| 65 | 65 | throw $this->invalidBindingException($alias, $e); |
| 66 | 66 | } |
| 67 | 67 | |
@@ -73,13 +73,13 @@ discard block |
||
| 73 | 73 | $bindings = &$this->state->bindings; |
| 74 | 74 | |
| 75 | 75 | $flags = []; |
| 76 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
| 76 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class){ |
|
| 77 | 77 | //Checking alias tree |
| 78 | - if ($flags[$binding->alias] ?? false) { |
|
| 78 | + if ($flags[$binding->alias] ?? false){ |
|
| 79 | 79 | return $binding->alias === $alias ?: throw new Exception('Circular alias detected'); |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - if (\array_key_exists($alias, $this->state->singletons)) { |
|
| 82 | + if (\array_key_exists($alias, $this->state->singletons)){ |
|
| 83 | 83 | return true; |
| 84 | 84 | } |
| 85 | 85 | |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | public function removeInjector(string $class): void |
| 105 | 105 | { |
| 106 | 106 | unset($this->state->injectors[$class]); |
| 107 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
| 107 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class){ |
|
| 108 | 108 | return; |
| 109 | 109 | } |
| 110 | 110 | unset($this->state->bindings[$class]); |
@@ -112,20 +112,20 @@ discard block |
||
| 112 | 112 | |
| 113 | 113 | public function hasInjector(string $class): bool |
| 114 | 114 | { |
| 115 | - try { |
|
| 115 | + try{ |
|
| 116 | 116 | $reflection = new \ReflectionClass($class); |
| 117 | - } catch (\ReflectionException $e) { |
|
| 117 | + }catch (\ReflectionException $e){ |
|
| 118 | 118 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
| 121 | + if (\array_key_exists($class, $this->state->injectors)){ |
|
| 122 | 122 | return true; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | if ( |
| 126 | 126 | $reflection->implementsInterface(InjectableInterface::class) |
| 127 | 127 | && $reflection->hasConstant('INJECTOR') |
| 128 | - ) { |
|
| 128 | + ){ |
|
| 129 | 129 | $const = $reflection->getConstant('INJECTOR'); |
| 130 | 130 | $this->bindInjector($class, $const); |
| 131 | 131 | |
@@ -133,12 +133,12 @@ discard block |
||
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | // check interfaces |
| 136 | - foreach ($this->state->injectors as $target => $injector) { |
|
| 136 | + foreach ($this->state->injectors as $target => $injector){ |
|
| 137 | 137 | if ( |
| 138 | 138 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
| 139 | 139 | || |
| 140 | 140 | (\interface_exists($target, true) && $reflection->implementsInterface($target)) |
| 141 | - ) { |
|
| 141 | + ){ |
|
| 142 | 142 | $this->state->bindings[$class] = new Injectable($injector); |
| 143 | 143 | |
| 144 | 144 | return true; |
@@ -165,15 +165,15 @@ discard block |
||
| 165 | 165 | |
| 166 | 166 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
| 167 | 167 | { |
| 168 | - if (\is_callable($resolver)) { |
|
| 168 | + if (\is_callable($resolver)){ |
|
| 169 | 169 | return new Factory($resolver, $singleton); |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | // Validate lazy invokable array |
| 173 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
| 173 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === ''){ |
|
| 174 | 174 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 175 | 175 | } |
| 176 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
| 176 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === ''){ |
|
| 177 | 177 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 178 | 178 | } |
| 179 | 179 | |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | |
| 192 | 192 | private function setBinding(string $alias, Binding $config): void |
| 193 | 193 | { |
| 194 | - if (isset($this->state->singletons[$alias])) { |
|
| 194 | + if (isset($this->state->singletons[$alias])){ |
|
| 195 | 195 | throw new SingletonOverloadException($alias); |
| 196 | 196 | } |
| 197 | 197 | |
@@ -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,9 +63,12 @@ discard block |
||
| 59 | 63 | */ |
| 60 | 64 | public function bindSingleton(string $alias, mixed $resolver): void |
| 61 | 65 | { |
| 62 | - try { |
|
| 66 | + try |
|
| 67 | + { |
|
| 63 | 68 | $config = $this->makeConfig($resolver, true); |
| 64 | - } catch (\Throwable $e) { |
|
| 69 | + } |
|
| 70 | + catch (\Throwable $e) |
|
| 71 | + { |
|
| 65 | 72 | throw $this->invalidBindingException($alias, $e); |
| 66 | 73 | } |
| 67 | 74 | |
@@ -73,13 +80,16 @@ discard block |
||
| 73 | 80 | $bindings = &$this->state->bindings; |
| 74 | 81 | |
| 75 | 82 | $flags = []; |
| 76 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
| 83 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) |
|
| 84 | + { |
|
| 77 | 85 | //Checking alias tree |
| 78 | - if ($flags[$binding->alias] ?? false) { |
|
| 86 | + if ($flags[$binding->alias] ?? false) |
|
| 87 | + { |
|
| 79 | 88 | return $binding->alias === $alias ?: throw new Exception('Circular alias detected'); |
| 80 | 89 | } |
| 81 | 90 | |
| 82 | - if (\array_key_exists($alias, $this->state->singletons)) { |
|
| 91 | + if (\array_key_exists($alias, $this->state->singletons)) |
|
| 92 | + { |
|
| 83 | 93 | return true; |
| 84 | 94 | } |
| 85 | 95 | |
@@ -104,7 +114,8 @@ discard block |
||
| 104 | 114 | public function removeInjector(string $class): void |
| 105 | 115 | { |
| 106 | 116 | unset($this->state->injectors[$class]); |
| 107 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
| 117 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) |
|
| 118 | + { |
|
| 108 | 119 | return; |
| 109 | 120 | } |
| 110 | 121 | unset($this->state->bindings[$class]); |
@@ -112,13 +123,17 @@ discard block |
||
| 112 | 123 | |
| 113 | 124 | public function hasInjector(string $class): bool |
| 114 | 125 | { |
| 115 | - try { |
|
| 126 | + try |
|
| 127 | + { |
|
| 116 | 128 | $reflection = new \ReflectionClass($class); |
| 117 | - } catch (\ReflectionException $e) { |
|
| 129 | + } |
|
| 130 | + catch (\ReflectionException $e) |
|
| 131 | + { |
|
| 118 | 132 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 119 | 133 | } |
| 120 | 134 | |
| 121 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
| 135 | + if (\array_key_exists($class, $this->state->injectors)) |
|
| 136 | + { |
|
| 122 | 137 | return true; |
| 123 | 138 | } |
| 124 | 139 | |
@@ -133,7 +148,8 @@ discard block |
||
| 133 | 148 | } |
| 134 | 149 | |
| 135 | 150 | // check interfaces |
| 136 | - foreach ($this->state->injectors as $target => $injector) { |
|
| 151 | + foreach ($this->state->injectors as $target => $injector) |
|
| 152 | + { |
|
| 137 | 153 | if ( |
| 138 | 154 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
| 139 | 155 | || |
@@ -165,15 +181,18 @@ discard block |
||
| 165 | 181 | |
| 166 | 182 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
| 167 | 183 | { |
| 168 | - if (\is_callable($resolver)) { |
|
| 184 | + if (\is_callable($resolver)) |
|
| 185 | + { |
|
| 169 | 186 | return new Factory($resolver, $singleton); |
| 170 | 187 | } |
| 171 | 188 | |
| 172 | 189 | // Validate lazy invokable array |
| 173 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
| 190 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') |
|
| 191 | + { |
|
| 174 | 192 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 175 | 193 | } |
| 176 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
| 194 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') |
|
| 195 | + { |
|
| 177 | 196 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 178 | 197 | } |
| 179 | 198 | |
@@ -191,7 +210,8 @@ discard block |
||
| 191 | 210 | |
| 192 | 211 | private function setBinding(string $alias, Binding $config): void |
| 193 | 212 | { |
| 194 | - if (isset($this->state->singletons[$alias])) { |
|
| 213 | + if (isset($this->state->singletons[$alias])) |
|
| 214 | + { |
|
| 195 | 215 | throw new SingletonOverloadException($alias); |
| 196 | 216 | } |
| 197 | 217 | |
@@ -46,7 +46,7 @@ |
||
| 46 | 46 | $binder = $this->constructor->get('binder', BinderInterface::class); |
| 47 | 47 | $factory = $this->constructor->get('factory', FactoryInterface::class); |
| 48 | 48 | |
| 49 | - $this->bindSingleton('test', new class implements SingletonInterface {}); |
|
| 49 | + $this->bindSingleton('test', new class implements SingletonInterface{}); |
|
| 50 | 50 | $factory->make('test'); |
| 51 | 51 | |
| 52 | 52 | $this->assertTrue($binder->hasInstance('test')); |
@@ -46,7 +46,9 @@ |
||
| 46 | 46 | $binder = $this->constructor->get('binder', BinderInterface::class); |
| 47 | 47 | $factory = $this->constructor->get('factory', FactoryInterface::class); |
| 48 | 48 | |
| 49 | - $this->bindSingleton('test', new class implements SingletonInterface {}); |
|
| 49 | + $this->bindSingleton('test', new class implements SingletonInterface |
|
| 50 | + { |
|
| 51 | +}); |
|
| 50 | 52 | $factory->make('test'); |
| 51 | 53 | |
| 52 | 54 | $this->assertTrue($binder->hasInstance('test')); |