@@ -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, bool $force = true): 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,9 +73,9 @@ 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 | |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | public function removeInjector(string $class): void |
101 | 101 | { |
102 | 102 | unset($this->state->injectors[$class]); |
103 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
103 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class){ |
|
104 | 104 | return; |
105 | 105 | } |
106 | 106 | unset($this->state->bindings[$class]); |
@@ -108,20 +108,20 @@ discard block |
||
108 | 108 | |
109 | 109 | public function hasInjector(string $class): bool |
110 | 110 | { |
111 | - try { |
|
111 | + try{ |
|
112 | 112 | $reflection = new \ReflectionClass($class); |
113 | - } catch (\ReflectionException $e) { |
|
113 | + }catch (\ReflectionException $e){ |
|
114 | 114 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
115 | 115 | } |
116 | 116 | |
117 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
117 | + if (\array_key_exists($class, $this->state->injectors)){ |
|
118 | 118 | return true; |
119 | 119 | } |
120 | 120 | |
121 | 121 | if ( |
122 | 122 | $reflection->implementsInterface(InjectableInterface::class) |
123 | 123 | && $reflection->hasConstant('INJECTOR') |
124 | - ) { |
|
124 | + ){ |
|
125 | 125 | $const = $reflection->getConstant('INJECTOR'); |
126 | 126 | $this->bindInjector($class, $const); |
127 | 127 | |
@@ -129,12 +129,12 @@ discard block |
||
129 | 129 | } |
130 | 130 | |
131 | 131 | // check interfaces |
132 | - foreach ($this->state->injectors as $target => $injector) { |
|
132 | + foreach ($this->state->injectors as $target => $injector){ |
|
133 | 133 | if ( |
134 | 134 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
135 | 135 | || |
136 | 136 | (\interface_exists($target, true) && $reflection->implementsInterface($target)) |
137 | - ) { |
|
137 | + ){ |
|
138 | 138 | $this->state->bindings[$class] = new Injectable($injector); |
139 | 139 | |
140 | 140 | return true; |
@@ -161,15 +161,15 @@ discard block |
||
161 | 161 | |
162 | 162 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
163 | 163 | { |
164 | - if (\is_callable($resolver)) { |
|
164 | + if (\is_callable($resolver)){ |
|
165 | 165 | return new Factory($resolver, $singleton); |
166 | 166 | } |
167 | 167 | |
168 | 168 | // Validate lazy invokable array |
169 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
169 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === ''){ |
|
170 | 170 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
171 | 171 | } |
172 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
172 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === ''){ |
|
173 | 173 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
174 | 174 | } |
175 | 175 | |
@@ -187,11 +187,11 @@ discard block |
||
187 | 187 | |
188 | 188 | private function setBinding(string $alias, Binding $config, bool $force): void |
189 | 189 | { |
190 | - if ($force) { |
|
190 | + if ($force){ |
|
191 | 191 | $this->removeBinding($alias); |
192 | 192 | } |
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, bool $force = true): 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,9 +80,11 @@ 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 | |
@@ -100,7 +109,8 @@ discard block |
||
100 | 109 | public function removeInjector(string $class): void |
101 | 110 | { |
102 | 111 | unset($this->state->injectors[$class]); |
103 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
112 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) |
|
113 | + { |
|
104 | 114 | return; |
105 | 115 | } |
106 | 116 | unset($this->state->bindings[$class]); |
@@ -108,13 +118,17 @@ discard block |
||
108 | 118 | |
109 | 119 | public function hasInjector(string $class): bool |
110 | 120 | { |
111 | - try { |
|
121 | + try |
|
122 | + { |
|
112 | 123 | $reflection = new \ReflectionClass($class); |
113 | - } catch (\ReflectionException $e) { |
|
124 | + } |
|
125 | + catch (\ReflectionException $e) |
|
126 | + { |
|
114 | 127 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
115 | 128 | } |
116 | 129 | |
117 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
130 | + if (\array_key_exists($class, $this->state->injectors)) |
|
131 | + { |
|
118 | 132 | return true; |
119 | 133 | } |
120 | 134 | |
@@ -129,7 +143,8 @@ discard block |
||
129 | 143 | } |
130 | 144 | |
131 | 145 | // check interfaces |
132 | - foreach ($this->state->injectors as $target => $injector) { |
|
146 | + foreach ($this->state->injectors as $target => $injector) |
|
147 | + { |
|
133 | 148 | if ( |
134 | 149 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
135 | 150 | || |
@@ -161,15 +176,18 @@ discard block |
||
161 | 176 | |
162 | 177 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
163 | 178 | { |
164 | - if (\is_callable($resolver)) { |
|
179 | + if (\is_callable($resolver)) |
|
180 | + { |
|
165 | 181 | return new Factory($resolver, $singleton); |
166 | 182 | } |
167 | 183 | |
168 | 184 | // Validate lazy invokable array |
169 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
185 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') |
|
186 | + { |
|
170 | 187 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
171 | 188 | } |
172 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
189 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') |
|
190 | + { |
|
173 | 191 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
174 | 192 | } |
175 | 193 | |
@@ -187,11 +205,13 @@ discard block |
||
187 | 205 | |
188 | 206 | private function setBinding(string $alias, Binding $config, bool $force): void |
189 | 207 | { |
190 | - if ($force) { |
|
208 | + if ($force) |
|
209 | + { |
|
191 | 210 | $this->removeBinding($alias); |
192 | 211 | } |
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 |
@@ -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 */ |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | * @throws ContainerException |
139 | 139 | * @throws \Throwable |
140 | 140 | */ |
141 | - public function get(string|Autowire $id, string $context = null): mixed |
|
141 | + public function get(string | Autowire $id, string $context = null): mixed |
|
142 | 142 | { |
143 | 143 | /** @psalm-suppress TooManyArguments */ |
144 | 144 | return $this->container->get($id, $context); |
@@ -166,16 +166,16 @@ discard block |
||
166 | 166 | $binds = &$this->state->bindings; |
167 | 167 | $singletons = &$this->state->singletons; |
168 | 168 | $cleanup = $previous = $prevSin = []; |
169 | - foreach ($bindings as $alias => $resolver) { |
|
169 | + foreach ($bindings as $alias => $resolver){ |
|
170 | 170 | // Store previous bindings |
171 | - if (isset($binds[$alias])) { |
|
171 | + if (isset($binds[$alias])){ |
|
172 | 172 | $previous[$alias] = $binds[$alias]; |
173 | - } else { |
|
173 | + }else{ |
|
174 | 174 | // Store bindings to be removed |
175 | 175 | $cleanup[] = $alias; |
176 | 176 | } |
177 | 177 | // Store previous singletons |
178 | - if (isset($singletons[$alias])) { |
|
178 | + if (isset($singletons[$alias])){ |
|
179 | 179 | $prevSin[$alias] = $singletons[$alias]; |
180 | 180 | unset($singletons[$alias]); |
181 | 181 | } |
@@ -183,21 +183,21 @@ discard block |
||
183 | 183 | $this->binder->bind($alias, $resolver); |
184 | 184 | } |
185 | 185 | |
186 | - try { |
|
186 | + try{ |
|
187 | 187 | return ContainerScope::getContainer() !== $this |
188 | 188 | ? ContainerScope::runScope($this, $scope) |
189 | 189 | : $scope($this); |
190 | - } finally { |
|
190 | + }finally{ |
|
191 | 191 | // Remove new bindings |
192 | - foreach ($cleanup as $alias) { |
|
192 | + foreach ($cleanup as $alias){ |
|
193 | 193 | unset($binds[$alias], $singletons[$alias]); |
194 | 194 | } |
195 | 195 | // Restore previous bindings |
196 | - foreach ($previous as $alias => $resolver) { |
|
196 | + foreach ($previous as $alias => $resolver){ |
|
197 | 197 | $binds[$alias] = $resolver; |
198 | 198 | } |
199 | 199 | // Restore singletons |
200 | - foreach ($prevSin as $alias => $instance) { |
|
200 | + foreach ($prevSin as $alias => $instance){ |
|
201 | 201 | $singletons[$alias] = $instance; |
202 | 202 | } |
203 | 203 | } |
@@ -211,32 +211,32 @@ discard block |
||
211 | 211 | // Open scope |
212 | 212 | $container = new self($this->config, $name); |
213 | 213 | |
214 | - try { |
|
214 | + try{ |
|
215 | 215 | // Configure scope |
216 | 216 | $container->scope->setParent($this, $this->scope); |
217 | 217 | |
218 | 218 | // Add specific bindings |
219 | - foreach ($bindings as $alias => $resolver) { |
|
219 | + foreach ($bindings as $alias => $resolver){ |
|
220 | 220 | $container->binder->bind($alias, $resolver); |
221 | 221 | } |
222 | 222 | |
223 | 223 | return ContainerScope::runScope( |
224 | 224 | $container, |
225 | 225 | static function (self $container) use ($autowire, $closure): mixed { |
226 | - try { |
|
226 | + try{ |
|
227 | 227 | return $autowire |
228 | 228 | ? $container->invoke($closure) |
229 | 229 | : $closure($container); |
230 | - } finally { |
|
230 | + }finally{ |
|
231 | 231 | $container->closeScope(); |
232 | 232 | } |
233 | 233 | } |
234 | 234 | ); |
235 | - } finally { |
|
235 | + }finally{ |
|
236 | 236 | // Check the container has not been leaked |
237 | 237 | $link = \WeakReference::create($container); |
238 | 238 | unset($container); |
239 | - if ($link->get() !== null) { |
|
239 | + if ($link->get() !== null){ |
|
240 | 240 | throw new ScopeContainerLeakedException($name, $this->scope->getParentScopeNames()); |
241 | 241 | } |
242 | 242 | } |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | * @param bool $force If the value is false, an exception will be thrown when attempting |
271 | 271 | * to bind an already constructed singleton. |
272 | 272 | */ |
273 | - public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
|
273 | + public function bindSingleton(string $alias, string | array | callable | object $resolver, bool $force = true): void |
|
274 | 274 | { |
275 | 275 | /** @psalm-suppress TooManyArguments */ |
276 | 276 | $this->binder->bindSingleton($alias, $resolver, $force); |
@@ -338,8 +338,8 @@ discard block |
||
338 | 338 | ]); |
339 | 339 | |
340 | 340 | // Create container services |
341 | - foreach ($container->config as $property => $class) { |
|
342 | - if (\property_exists($container, $property)) { |
|
341 | + foreach ($container->config as $property => $class){ |
|
342 | + if (\property_exists($container, $property)){ |
|
343 | 343 | $container->$property = $constructor->get($property, $class); |
344 | 344 | } |
345 | 345 | } |
@@ -353,7 +353,7 @@ discard block |
||
353 | 353 | private function closeScope(): void |
354 | 354 | { |
355 | 355 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
356 | - if (!isset($this->scope)) { |
|
356 | + if (!isset($this->scope)){ |
|
357 | 357 | $this->destruct(); |
358 | 358 | return; |
359 | 359 | } |
@@ -362,10 +362,10 @@ discard block |
||
362 | 362 | |
363 | 363 | // Run finalizers |
364 | 364 | $errors = []; |
365 | - foreach ($this->state->finalizers as $finalizer) { |
|
366 | - try { |
|
365 | + foreach ($this->state->finalizers as $finalizer){ |
|
366 | + try{ |
|
367 | 367 | $this->invoker->invoke($finalizer); |
368 | - } catch (\Throwable $e) { |
|
368 | + }catch (\Throwable $e){ |
|
369 | 369 | $errors[] = $e; |
370 | 370 | } |
371 | 371 | } |
@@ -374,7 +374,7 @@ discard block |
||
374 | 374 | $this->destruct(); |
375 | 375 | |
376 | 376 | // Throw collected errors |
377 | - if ($errors !== []) { |
|
377 | + if ($errors !== []){ |
|
378 | 378 | throw new FinalizersException($scopeName, $errors); |
379 | 379 | } |
380 | 380 | } |