@@ -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,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')); |