@@ -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 | |