@@ -15,11 +15,11 @@ |
||
15 | 15 | * @param \Stringable|string|null $context Original context. |
16 | 16 | */ |
17 | 17 | public function __construct( |
18 | - public \Stringable|string|null $context = null, |
|
19 | - ) {} |
|
18 | + public \Stringable | string | null $context = null, |
|
19 | + ){} |
|
20 | 20 | |
21 | 21 | public function __toString(): string |
22 | 22 | { |
23 | - return (string) $this->context; |
|
23 | + return (string)$this->context; |
|
24 | 24 | } |
25 | 25 | } |
@@ -16,7 +16,8 @@ |
||
16 | 16 | */ |
17 | 17 | public function __construct( |
18 | 18 | public \Stringable|string|null $context = null, |
19 | - ) {} |
|
19 | + ) { |
|
20 | +} |
|
20 | 21 | |
21 | 22 | public function __toString(): string |
22 | 23 | { |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | |
19 | 19 | public function __construct( |
20 | 20 | private readonly ?string $scopeName = null, |
21 | - ) {} |
|
21 | + ){} |
|
22 | 22 | |
23 | 23 | public function getScopeName(): ?string |
24 | 24 | { |
@@ -37,9 +37,9 @@ discard block |
||
37 | 37 | $this->parentFactory = $factory; |
38 | 38 | |
39 | 39 | // Check a scope with the same name is not already registered |
40 | - if ($this->scopeName !== null) { |
|
40 | + if ($this->scopeName !== null){ |
|
41 | 41 | $tmp = $this; |
42 | - while ($tmp->parentScope !== null) { |
|
42 | + while ($tmp->parentScope !== null){ |
|
43 | 43 | $tmp = $tmp->parentScope; |
44 | 44 | $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName); |
45 | 45 | } |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | $result = [$this->scopeName]; |
68 | 68 | |
69 | 69 | $parent = $this; |
70 | - while ($parent->parentScope !== null) { |
|
70 | + while ($parent->parentScope !== null){ |
|
71 | 71 | $parent = $parent->parentScope; |
72 | 72 | $result[] = $parent->scopeName; |
73 | 73 | } |
@@ -18,7 +18,8 @@ discard block |
||
18 | 18 | |
19 | 19 | public function __construct( |
20 | 20 | private readonly ?string $scopeName = null, |
21 | - ) {} |
|
21 | + ) { |
|
22 | +} |
|
22 | 23 | |
23 | 24 | public function getScopeName(): ?string |
24 | 25 | { |
@@ -37,9 +38,11 @@ discard block |
||
37 | 38 | $this->parentFactory = $factory; |
38 | 39 | |
39 | 40 | // Check a scope with the same name is not already registered |
40 | - if ($this->scopeName !== null) { |
|
41 | + if ($this->scopeName !== null) |
|
42 | + { |
|
41 | 43 | $tmp = $this; |
42 | - while ($tmp->parentScope !== null) { |
|
44 | + while ($tmp->parentScope !== null) |
|
45 | + { |
|
43 | 46 | $tmp = $tmp->parentScope; |
44 | 47 | $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName); |
45 | 48 | } |
@@ -67,7 +70,8 @@ discard block |
||
67 | 70 | $result = [$this->scopeName]; |
68 | 71 | |
69 | 72 | $parent = $this; |
70 | - while ($parent->parentScope !== null) { |
|
73 | + while ($parent->parentScope !== null) |
|
74 | + { |
|
71 | 75 | $parent = $parent->parentScope; |
72 | 76 | $result[] = $parent->scopeName; |
73 | 77 | } |
@@ -28,21 +28,21 @@ discard block |
||
28 | 28 | { |
29 | 29 | public function __construct( |
30 | 30 | protected readonly State $state, |
31 | - ) {} |
|
31 | + ){} |
|
32 | 32 | |
33 | 33 | /** |
34 | 34 | * @param TResolver|object $resolver |
35 | 35 | */ |
36 | 36 | public function bind(string $alias, mixed $resolver): void |
37 | 37 | { |
38 | - if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) { |
|
38 | + if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))){ |
|
39 | 39 | $this->state->inflectors[$alias][] = $resolver; |
40 | 40 | return; |
41 | 41 | } |
42 | 42 | |
43 | - try { |
|
43 | + try{ |
|
44 | 44 | $config = $this->makeConfig($resolver, false); |
45 | - } catch (\Throwable $e) { |
|
45 | + }catch (\Throwable $e){ |
|
46 | 46 | throw $this->invalidBindingException($alias, $e); |
47 | 47 | } |
48 | 48 | |
@@ -54,9 +54,9 @@ discard block |
||
54 | 54 | */ |
55 | 55 | public function bindSingleton(string $alias, mixed $resolver): void |
56 | 56 | { |
57 | - try { |
|
57 | + try{ |
|
58 | 58 | $config = $this->makeConfig($resolver, true); |
59 | - } catch (\Throwable $e) { |
|
59 | + }catch (\Throwable $e){ |
|
60 | 60 | throw $this->invalidBindingException($alias, $e); |
61 | 61 | } |
62 | 62 | |
@@ -68,13 +68,13 @@ discard block |
||
68 | 68 | $bindings = &$this->state->bindings; |
69 | 69 | |
70 | 70 | $flags = []; |
71 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
71 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class){ |
|
72 | 72 | //Checking alias tree |
73 | - if ($flags[$binding->alias] ?? false) { |
|
73 | + if ($flags[$binding->alias] ?? false){ |
|
74 | 74 | return $binding->alias === $alias ?: throw new \Exception('Circular alias detected'); |
75 | 75 | } |
76 | 76 | |
77 | - if (\array_key_exists($alias, $this->state->singletons)) { |
|
77 | + if (\array_key_exists($alias, $this->state->singletons)){ |
|
78 | 78 | return true; |
79 | 79 | } |
80 | 80 | |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | public function removeInjector(string $class): void |
99 | 99 | { |
100 | 100 | unset($this->state->injectors[$class]); |
101 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
101 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class){ |
|
102 | 102 | return; |
103 | 103 | } |
104 | 104 | unset($this->state->bindings[$class]); |
@@ -106,20 +106,20 @@ discard block |
||
106 | 106 | |
107 | 107 | public function hasInjector(string $class): bool |
108 | 108 | { |
109 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
109 | + if (\array_key_exists($class, $this->state->injectors)){ |
|
110 | 110 | return true; |
111 | 111 | } |
112 | 112 | |
113 | - try { |
|
113 | + try{ |
|
114 | 114 | $reflection = new \ReflectionClass($class); |
115 | - } catch (\ReflectionException $e) { |
|
115 | + }catch (\ReflectionException $e){ |
|
116 | 116 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
117 | 117 | } |
118 | 118 | |
119 | 119 | if ( |
120 | 120 | $reflection->implementsInterface(InjectableInterface::class) |
121 | 121 | && $reflection->hasConstant('INJECTOR') |
122 | - ) { |
|
122 | + ){ |
|
123 | 123 | $const = $reflection->getConstant('INJECTOR'); |
124 | 124 | $this->bindInjector($class, $const); |
125 | 125 | |
@@ -127,12 +127,12 @@ discard block |
||
127 | 127 | } |
128 | 128 | |
129 | 129 | // check interfaces |
130 | - foreach ($this->state->injectors as $target => $injector) { |
|
130 | + foreach ($this->state->injectors as $target => $injector){ |
|
131 | 131 | if ( |
132 | 132 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
133 | 133 | || |
134 | 134 | (\interface_exists($target, true) && $reflection->implementsInterface($target)) |
135 | - ) { |
|
135 | + ){ |
|
136 | 136 | $this->state->bindings[$class] = new Injectable($injector); |
137 | 137 | |
138 | 138 | return true; |
@@ -159,15 +159,15 @@ discard block |
||
159 | 159 | |
160 | 160 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
161 | 161 | { |
162 | - if (\is_callable($resolver)) { |
|
162 | + if (\is_callable($resolver)){ |
|
163 | 163 | return new Factory($resolver, $singleton); |
164 | 164 | } |
165 | 165 | |
166 | 166 | // Validate lazy invokable array |
167 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
167 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === ''){ |
|
168 | 168 | throw new \InvalidArgumentException('Incompatible array declaration for resolver.'); |
169 | 169 | } |
170 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
170 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === ''){ |
|
171 | 171 | throw new \InvalidArgumentException('Incompatible array declaration for resolver.'); |
172 | 172 | } |
173 | 173 | |
@@ -188,13 +188,13 @@ discard block |
||
188 | 188 | |
189 | 189 | private function setBinding(string $alias, Binding $config): void |
190 | 190 | { |
191 | - if (isset($this->state->singletons[$alias])) { |
|
191 | + if (isset($this->state->singletons[$alias])){ |
|
192 | 192 | throw new SingletonOverloadException($alias); |
193 | 193 | } |
194 | 194 | |
195 | 195 | $this->state->bindings[$alias] = $config; |
196 | 196 | |
197 | - if ($config instanceof Injectable) { |
|
197 | + if ($config instanceof Injectable){ |
|
198 | 198 | $this->state->injectors[$alias] = $config->injector; |
199 | 199 | } |
200 | 200 | } |
@@ -28,21 +28,26 @@ discard block |
||
28 | 28 | { |
29 | 29 | public function __construct( |
30 | 30 | protected readonly State $state, |
31 | - ) {} |
|
31 | + ) { |
|
32 | +} |
|
32 | 33 | |
33 | 34 | /** |
34 | 35 | * @param TResolver|object $resolver |
35 | 36 | */ |
36 | 37 | public function bind(string $alias, mixed $resolver): void |
37 | 38 | { |
38 | - if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) { |
|
39 | + if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) |
|
40 | + { |
|
39 | 41 | $this->state->inflectors[$alias][] = $resolver; |
40 | 42 | return; |
41 | 43 | } |
42 | 44 | |
43 | - try { |
|
45 | + try |
|
46 | + { |
|
44 | 47 | $config = $this->makeConfig($resolver, false); |
45 | - } catch (\Throwable $e) { |
|
48 | + } |
|
49 | + catch (\Throwable $e) |
|
50 | + { |
|
46 | 51 | throw $this->invalidBindingException($alias, $e); |
47 | 52 | } |
48 | 53 | |
@@ -54,9 +59,12 @@ discard block |
||
54 | 59 | */ |
55 | 60 | public function bindSingleton(string $alias, mixed $resolver): void |
56 | 61 | { |
57 | - try { |
|
62 | + try |
|
63 | + { |
|
58 | 64 | $config = $this->makeConfig($resolver, true); |
59 | - } catch (\Throwable $e) { |
|
65 | + } |
|
66 | + catch (\Throwable $e) |
|
67 | + { |
|
60 | 68 | throw $this->invalidBindingException($alias, $e); |
61 | 69 | } |
62 | 70 | |
@@ -68,13 +76,16 @@ discard block |
||
68 | 76 | $bindings = &$this->state->bindings; |
69 | 77 | |
70 | 78 | $flags = []; |
71 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
79 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) |
|
80 | + { |
|
72 | 81 | //Checking alias tree |
73 | - if ($flags[$binding->alias] ?? false) { |
|
82 | + if ($flags[$binding->alias] ?? false) |
|
83 | + { |
|
74 | 84 | return $binding->alias === $alias ?: throw new \Exception('Circular alias detected'); |
75 | 85 | } |
76 | 86 | |
77 | - if (\array_key_exists($alias, $this->state->singletons)) { |
|
87 | + if (\array_key_exists($alias, $this->state->singletons)) |
|
88 | + { |
|
78 | 89 | return true; |
79 | 90 | } |
80 | 91 | |
@@ -98,7 +109,8 @@ discard block |
||
98 | 109 | public function removeInjector(string $class): void |
99 | 110 | { |
100 | 111 | unset($this->state->injectors[$class]); |
101 | - 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 | + { |
|
102 | 114 | return; |
103 | 115 | } |
104 | 116 | unset($this->state->bindings[$class]); |
@@ -106,13 +118,17 @@ discard block |
||
106 | 118 | |
107 | 119 | public function hasInjector(string $class): bool |
108 | 120 | { |
109 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
121 | + if (\array_key_exists($class, $this->state->injectors)) |
|
122 | + { |
|
110 | 123 | return true; |
111 | 124 | } |
112 | 125 | |
113 | - try { |
|
126 | + try |
|
127 | + { |
|
114 | 128 | $reflection = new \ReflectionClass($class); |
115 | - } catch (\ReflectionException $e) { |
|
129 | + } |
|
130 | + catch (\ReflectionException $e) |
|
131 | + { |
|
116 | 132 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
117 | 133 | } |
118 | 134 | |
@@ -127,7 +143,8 @@ discard block |
||
127 | 143 | } |
128 | 144 | |
129 | 145 | // check interfaces |
130 | - foreach ($this->state->injectors as $target => $injector) { |
|
146 | + foreach ($this->state->injectors as $target => $injector) |
|
147 | + { |
|
131 | 148 | if ( |
132 | 149 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
133 | 150 | || |
@@ -159,15 +176,18 @@ discard block |
||
159 | 176 | |
160 | 177 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
161 | 178 | { |
162 | - if (\is_callable($resolver)) { |
|
179 | + if (\is_callable($resolver)) |
|
180 | + { |
|
163 | 181 | return new Factory($resolver, $singleton); |
164 | 182 | } |
165 | 183 | |
166 | 184 | // Validate lazy invokable array |
167 | - 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 | + { |
|
168 | 187 | throw new \InvalidArgumentException('Incompatible array declaration for resolver.'); |
169 | 188 | } |
170 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
189 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') |
|
190 | + { |
|
171 | 191 | throw new \InvalidArgumentException('Incompatible array declaration for resolver.'); |
172 | 192 | } |
173 | 193 | |
@@ -188,13 +208,15 @@ discard block |
||
188 | 208 | |
189 | 209 | private function setBinding(string $alias, Binding $config): void |
190 | 210 | { |
191 | - if (isset($this->state->singletons[$alias])) { |
|
211 | + if (isset($this->state->singletons[$alias])) |
|
212 | + { |
|
192 | 213 | throw new SingletonOverloadException($alias); |
193 | 214 | } |
194 | 215 | |
195 | 216 | $this->state->bindings[$alias] = $config; |
196 | 217 | |
197 | - if ($config instanceof Injectable) { |
|
218 | + if ($config instanceof Injectable) |
|
219 | + { |
|
198 | 220 | $this->state->injectors[$alias] = $config->injector; |
199 | 221 | } |
200 | 222 | } |
@@ -19,7 +19,7 @@ |
||
19 | 19 | private readonly Config $config, |
20 | 20 | private array $objects = [], |
21 | 21 | private readonly Options $options = new Options(), |
22 | - ) {} |
|
22 | + ){} |
|
23 | 23 | |
24 | 24 | public function set(string $name, object $value): void |
25 | 25 | { |
@@ -19,7 +19,8 @@ |
||
19 | 19 | private readonly Config $config, |
20 | 20 | private array $objects = [], |
21 | 21 | private readonly Options $options = new Options(), |
22 | - ) {} |
|
22 | + ) { |
|
23 | +} |
|
23 | 24 | |
24 | 25 | public function set(string $name, object $value): void |
25 | 26 | { |
@@ -29,19 +29,19 @@ discard block |
||
29 | 29 | public function push(bool $nextLevel, mixed ...$details): void |
30 | 30 | { |
31 | 31 | $trace = $details === [] ? null : new Trace($details); |
32 | - if ($nextLevel || $this->traces === []) { |
|
32 | + if ($nextLevel || $this->traces === []){ |
|
33 | 33 | $this->traces[] = $trace === null ? [] : [$trace]; |
34 | - } elseif ($trace !== null) { |
|
34 | + } elseif ($trace !== null){ |
|
35 | 35 | $this->traces[\array_key_last($this->traces)][] = $trace; |
36 | 36 | } |
37 | 37 | } |
38 | 38 | |
39 | 39 | public function pop(bool $previousLevel = false): void |
40 | 40 | { |
41 | - if ($this->traces === []) { |
|
41 | + if ($this->traces === []){ |
|
42 | 42 | return; |
43 | 43 | } |
44 | - if ($previousLevel) { |
|
44 | + if ($previousLevel){ |
|
45 | 45 | \array_pop($this->traces); |
46 | 46 | return; |
47 | 47 | } |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | |
58 | 58 | public function __toString(): string |
59 | 59 | { |
60 | - return $this->traces === [] ? '' : "Container trace list:\n" . $this->renderTraceList($this->traces); |
|
60 | + return $this->traces === [] ? '' : "Container trace list:\n".$this->renderTraceList($this->traces); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | /** |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | { |
68 | 68 | $result = []; |
69 | 69 | $i = 0; |
70 | - foreach ($blocks as $block) { |
|
70 | + foreach ($blocks as $block){ |
|
71 | 71 | \array_push($result, ...$this->blockToStringList($block, $i++)); |
72 | 72 | } |
73 | 73 | return \implode("\n", $result); |
@@ -87,8 +87,8 @@ discard block |
||
87 | 87 | // Separator |
88 | 88 | $s = "\n"; |
89 | 89 | $nexPrefix = "$s$padding "; |
90 | - foreach ($items as $item) { |
|
91 | - $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string) $item); |
|
90 | + foreach ($items as $item){ |
|
91 | + $result[] = $firstPrefix.\str_replace($s, $nexPrefix, (string)$item); |
|
92 | 92 | } |
93 | 93 | return $result; |
94 | 94 | } |
@@ -29,19 +29,24 @@ discard block |
||
29 | 29 | public function push(bool $nextLevel, mixed ...$details): void |
30 | 30 | { |
31 | 31 | $trace = $details === [] ? null : new Trace($details); |
32 | - if ($nextLevel || $this->traces === []) { |
|
32 | + if ($nextLevel || $this->traces === []) |
|
33 | + { |
|
33 | 34 | $this->traces[] = $trace === null ? [] : [$trace]; |
34 | - } elseif ($trace !== null) { |
|
35 | + } |
|
36 | + elseif ($trace !== null) |
|
37 | + { |
|
35 | 38 | $this->traces[\array_key_last($this->traces)][] = $trace; |
36 | 39 | } |
37 | 40 | } |
38 | 41 | |
39 | 42 | public function pop(bool $previousLevel = false): void |
40 | 43 | { |
41 | - if ($this->traces === []) { |
|
44 | + if ($this->traces === []) |
|
45 | + { |
|
42 | 46 | return; |
43 | 47 | } |
44 | - if ($previousLevel) { |
|
48 | + if ($previousLevel) |
|
49 | + { |
|
45 | 50 | \array_pop($this->traces); |
46 | 51 | return; |
47 | 52 | } |
@@ -67,7 +72,8 @@ discard block |
||
67 | 72 | { |
68 | 73 | $result = []; |
69 | 74 | $i = 0; |
70 | - foreach ($blocks as $block) { |
|
75 | + foreach ($blocks as $block) |
|
76 | + { |
|
71 | 77 | \array_push($result, ...$this->blockToStringList($block, $i++)); |
72 | 78 | } |
73 | 79 | return \implode("\n", $result); |
@@ -87,7 +93,8 @@ discard block |
||
87 | 93 | // Separator |
88 | 94 | $s = "\n"; |
89 | 95 | $nexPrefix = "$s$padding "; |
90 | - foreach ($items as $item) { |
|
96 | + foreach ($items as $item) |
|
97 | + { |
|
91 | 98 | $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string) $item); |
92 | 99 | } |
93 | 100 | return $result; |
@@ -48,11 +48,11 @@ discard block |
||
48 | 48 | public const DEFAULT_ROOT_SCOPE_NAME = 'root'; |
49 | 49 | |
50 | 50 | private Internal\State $state; |
51 | - private ResolverInterface|Internal\Resolver $resolver; |
|
52 | - private FactoryInterface|Internal\Factory $factory; |
|
53 | - private ContainerInterface|Internal\Container $container; |
|
54 | - private BinderInterface|Internal\Binder $binder; |
|
55 | - private InvokerInterface|Internal\Invoker $invoker; |
|
51 | + private ResolverInterface | Internal\Resolver $resolver; |
|
52 | + private FactoryInterface | Internal\Factory $factory; |
|
53 | + private ContainerInterface | Internal\Container $container; |
|
54 | + private BinderInterface | Internal\Binder $binder; |
|
55 | + private InvokerInterface | Internal\Invoker $invoker; |
|
56 | 56 | private Internal\Scope $scope; |
57 | 57 | |
58 | 58 | /** |
@@ -60,11 +60,11 @@ discard block |
||
60 | 60 | */ |
61 | 61 | public function __construct( |
62 | 62 | private Config $config = new Config(), |
63 | - string|\BackedEnum|null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
|
63 | + string | \BackedEnum | null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
|
64 | 64 | private Options $options = new Options(), |
65 | - ) { |
|
66 | - if (\is_object($scopeName)) { |
|
67 | - $scopeName = (string) $scopeName->value; |
|
65 | + ){ |
|
66 | + if (\is_object($scopeName)){ |
|
67 | + $scopeName = (string)$scopeName->value; |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | $this->initServices($this, $scopeName); |
@@ -105,11 +105,11 @@ discard block |
||
105 | 105 | * @throws \Throwable |
106 | 106 | * @psalm-suppress TooManyArguments |
107 | 107 | */ |
108 | - public function make(string $alias, array $parameters = [], \Stringable|string|null $context = null): mixed |
|
108 | + public function make(string $alias, array $parameters = [], \Stringable | string | null $context = null): mixed |
|
109 | 109 | { |
110 | 110 | return ContainerScope::getContainer() === $this |
111 | 111 | ? $this->factory->make($alias, $parameters, $context) |
112 | - : ContainerScope::runScope($this, fn(): mixed => $this->factory->make($alias, $parameters, $context)); |
|
112 | + : ContainerScope::runScope($this, fn() : mixed => $this->factory->make($alias, $parameters, $context)); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | /** |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | * @throws \Throwable |
132 | 132 | * @psalm-suppress TooManyArguments |
133 | 133 | */ |
134 | - public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed |
|
134 | + public function get(string | Autowire $id, \Stringable | string | null $context = null): mixed |
|
135 | 135 | { |
136 | 136 | return ContainerScope::getContainer() === $this |
137 | 137 | ? $this->container->get($id, $context) |
@@ -151,9 +151,9 @@ discard block |
||
151 | 151 | * If {@see string}, the default binder for the given scope will be returned. Default bindings won't affect |
152 | 152 | * already created Container instances except the case with the root one. |
153 | 153 | */ |
154 | - public function getBinder(string|\BackedEnum|null $scope = null): BinderInterface |
|
154 | + public function getBinder(string | \BackedEnum | null $scope = null): BinderInterface |
|
155 | 155 | { |
156 | - $scope = \is_object($scope) ? (string) $scope->value : $scope; |
|
156 | + $scope = \is_object($scope) ? (string)$scope->value : $scope; |
|
157 | 157 | |
158 | 158 | return $scope === null |
159 | 159 | ? $this->binder |
@@ -163,25 +163,25 @@ discard block |
||
163 | 163 | /** |
164 | 164 | * @throws \Throwable |
165 | 165 | */ |
166 | - public function runScope(Scope|array $bindings, callable $scope): mixed |
|
166 | + public function runScope(Scope | array $bindings, callable $scope): mixed |
|
167 | 167 | { |
168 | - if (!\is_array($bindings)) { |
|
168 | + if (!\is_array($bindings)){ |
|
169 | 169 | return $this->runIsolatedScope($bindings, $scope); |
170 | 170 | } |
171 | 171 | |
172 | 172 | $binds = &$this->state->bindings; |
173 | 173 | $singletons = &$this->state->singletons; |
174 | 174 | $cleanup = $previous = $prevSin = []; |
175 | - foreach ($bindings as $alias => $resolver) { |
|
175 | + foreach ($bindings as $alias => $resolver){ |
|
176 | 176 | // Store previous bindings |
177 | - if (isset($binds[$alias])) { |
|
177 | + if (isset($binds[$alias])){ |
|
178 | 178 | $previous[$alias] = $binds[$alias]; |
179 | - } else { |
|
179 | + }else{ |
|
180 | 180 | // Store bindings to be removed |
181 | 181 | $cleanup[] = $alias; |
182 | 182 | } |
183 | 183 | // Store previous singletons |
184 | - if (isset($singletons[$alias])) { |
|
184 | + if (isset($singletons[$alias])){ |
|
185 | 185 | $prevSin[$alias] = $singletons[$alias]; |
186 | 186 | unset($singletons[$alias]); |
187 | 187 | } |
@@ -189,21 +189,21 @@ discard block |
||
189 | 189 | $this->binder->bind($alias, $resolver); |
190 | 190 | } |
191 | 191 | |
192 | - try { |
|
192 | + try{ |
|
193 | 193 | return ContainerScope::getContainer() !== $this |
194 | 194 | ? ContainerScope::runScope($this, $scope) |
195 | 195 | : $scope($this); |
196 | - } finally { |
|
196 | + }finally{ |
|
197 | 197 | // Remove new bindings |
198 | - foreach ($cleanup as $alias) { |
|
198 | + foreach ($cleanup as $alias){ |
|
199 | 199 | unset($binds[$alias], $singletons[$alias]); |
200 | 200 | } |
201 | 201 | // Restore previous bindings |
202 | - foreach ($previous as $alias => $resolver) { |
|
202 | + foreach ($previous as $alias => $resolver){ |
|
203 | 203 | $binds[$alias] = $resolver; |
204 | 204 | } |
205 | 205 | // Restore singletons |
206 | - foreach ($prevSin as $alias => $instance) { |
|
206 | + foreach ($prevSin as $alias => $instance){ |
|
207 | 207 | $singletons[$alias] = $instance; |
208 | 208 | } |
209 | 209 | } |
@@ -251,9 +251,9 @@ discard block |
||
251 | 251 | * If the value is null, option {@see Options::$allowSingletonsRebinding} will be used. |
252 | 252 | * @throws SingletonOverloadException |
253 | 253 | */ |
254 | - public function bindSingleton(string $alias, string|array|callable|object $resolver, ?bool $force = null): void |
|
254 | + public function bindSingleton(string $alias, string | array | callable | object $resolver, ?bool $force = null): void |
|
255 | 255 | { |
256 | - if ($force ?? $this->options->allowSingletonsRebinding) { |
|
256 | + if ($force ?? $this->options->allowSingletonsRebinding){ |
|
257 | 257 | $this->binder->removeBinding($alias); |
258 | 258 | } |
259 | 259 | |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | { |
281 | 281 | return ContainerScope::getContainer() === $this |
282 | 282 | ? $this->invoker->invoke($target, $parameters) |
283 | - : ContainerScope::runScope($this, fn(): mixed => $this->invoker->invoke($target, $parameters)); |
|
283 | + : ContainerScope::runScope($this, fn() : mixed => $this->invoker->invoke($target, $parameters)); |
|
284 | 284 | } |
285 | 285 | |
286 | 286 | /** |
@@ -337,8 +337,8 @@ discard block |
||
337 | 337 | ], $this->options); |
338 | 338 | |
339 | 339 | // Create container services |
340 | - foreach ($container->config as $property => $class) { |
|
341 | - if (\property_exists($container, $property)) { |
|
340 | + foreach ($container->config as $property => $class){ |
|
341 | + if (\property_exists($container, $property)){ |
|
342 | 342 | $container->$property = $constructor->get($property, $class); |
343 | 343 | } |
344 | 344 | } |
@@ -352,7 +352,7 @@ discard block |
||
352 | 352 | private function closeScope(): void |
353 | 353 | { |
354 | 354 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
355 | - if (!isset($this->scope)) { |
|
355 | + if (!isset($this->scope)){ |
|
356 | 356 | $this->destruct(); |
357 | 357 | return; |
358 | 358 | } |
@@ -361,10 +361,10 @@ discard block |
||
361 | 361 | |
362 | 362 | // Run finalizers |
363 | 363 | $errors = []; |
364 | - foreach ($this->state->finalizers as $finalizer) { |
|
365 | - try { |
|
364 | + foreach ($this->state->finalizers as $finalizer){ |
|
365 | + try{ |
|
366 | 366 | $this->invoker->invoke($finalizer); |
367 | - } catch (\Throwable $e) { |
|
367 | + }catch (\Throwable $e){ |
|
368 | 368 | $errors[] = $e; |
369 | 369 | } |
370 | 370 | } |
@@ -373,7 +373,7 @@ discard block |
||
373 | 373 | $this->destruct(); |
374 | 374 | |
375 | 375 | // Throw collected errors |
376 | - if ($errors !== []) { |
|
376 | + if ($errors !== []){ |
|
377 | 377 | throw new FinalizersException($scopeName, $errors); |
378 | 378 | } |
379 | 379 | } |
@@ -395,18 +395,18 @@ discard block |
||
395 | 395 | $container->scope->setParent($this, $this->scope, $this->factory); |
396 | 396 | |
397 | 397 | // Add specific bindings |
398 | - foreach ($config->bindings as $alias => $resolver) { |
|
398 | + foreach ($config->bindings as $alias => $resolver){ |
|
399 | 399 | $container->binder->bind($alias, $resolver); |
400 | 400 | } |
401 | 401 | |
402 | 402 | return ContainerScope::runScope( |
403 | 403 | $container, |
404 | 404 | static function (self $container) use ($config, $closure): mixed { |
405 | - try { |
|
405 | + try{ |
|
406 | 406 | return $config->autowire |
407 | 407 | ? $container->invoke($closure) |
408 | 408 | : $closure($container); |
409 | - } finally { |
|
409 | + }finally{ |
|
410 | 410 | $container->closeScope(); |
411 | 411 | } |
412 | 412 | }, |
@@ -63,7 +63,8 @@ discard block |
||
63 | 63 | string|\BackedEnum|null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
64 | 64 | private Options $options = new Options(), |
65 | 65 | ) { |
66 | - if (\is_object($scopeName)) { |
|
66 | + if (\is_object($scopeName)) |
|
67 | + { |
|
67 | 68 | $scopeName = (string) $scopeName->value; |
68 | 69 | } |
69 | 70 | |
@@ -165,23 +166,29 @@ discard block |
||
165 | 166 | */ |
166 | 167 | public function runScope(Scope|array $bindings, callable $scope): mixed |
167 | 168 | { |
168 | - if (!\is_array($bindings)) { |
|
169 | + if (!\is_array($bindings)) |
|
170 | + { |
|
169 | 171 | return $this->runIsolatedScope($bindings, $scope); |
170 | 172 | } |
171 | 173 | |
172 | 174 | $binds = &$this->state->bindings; |
173 | 175 | $singletons = &$this->state->singletons; |
174 | 176 | $cleanup = $previous = $prevSin = []; |
175 | - foreach ($bindings as $alias => $resolver) { |
|
177 | + foreach ($bindings as $alias => $resolver) |
|
178 | + { |
|
176 | 179 | // Store previous bindings |
177 | - if (isset($binds[$alias])) { |
|
180 | + if (isset($binds[$alias])) |
|
181 | + { |
|
178 | 182 | $previous[$alias] = $binds[$alias]; |
179 | - } else { |
|
183 | + } |
|
184 | + else |
|
185 | + { |
|
180 | 186 | // Store bindings to be removed |
181 | 187 | $cleanup[] = $alias; |
182 | 188 | } |
183 | 189 | // Store previous singletons |
184 | - if (isset($singletons[$alias])) { |
|
190 | + if (isset($singletons[$alias])) |
|
191 | + { |
|
185 | 192 | $prevSin[$alias] = $singletons[$alias]; |
186 | 193 | unset($singletons[$alias]); |
187 | 194 | } |
@@ -189,21 +196,27 @@ discard block |
||
189 | 196 | $this->binder->bind($alias, $resolver); |
190 | 197 | } |
191 | 198 | |
192 | - try { |
|
199 | + try |
|
200 | + { |
|
193 | 201 | return ContainerScope::getContainer() !== $this |
194 | 202 | ? ContainerScope::runScope($this, $scope) |
195 | 203 | : $scope($this); |
196 | - } finally { |
|
204 | + } |
|
205 | + finally |
|
206 | + { |
|
197 | 207 | // Remove new bindings |
198 | - foreach ($cleanup as $alias) { |
|
208 | + foreach ($cleanup as $alias) |
|
209 | + { |
|
199 | 210 | unset($binds[$alias], $singletons[$alias]); |
200 | 211 | } |
201 | 212 | // Restore previous bindings |
202 | - foreach ($previous as $alias => $resolver) { |
|
213 | + foreach ($previous as $alias => $resolver) |
|
214 | + { |
|
203 | 215 | $binds[$alias] = $resolver; |
204 | 216 | } |
205 | 217 | // Restore singletons |
206 | - foreach ($prevSin as $alias => $instance) { |
|
218 | + foreach ($prevSin as $alias => $instance) |
|
219 | + { |
|
207 | 220 | $singletons[$alias] = $instance; |
208 | 221 | } |
209 | 222 | } |
@@ -253,7 +266,8 @@ discard block |
||
253 | 266 | */ |
254 | 267 | public function bindSingleton(string $alias, string|array|callable|object $resolver, ?bool $force = null): void |
255 | 268 | { |
256 | - if ($force ?? $this->options->allowSingletonsRebinding) { |
|
269 | + if ($force ?? $this->options->allowSingletonsRebinding) |
|
270 | + { |
|
257 | 271 | $this->binder->removeBinding($alias); |
258 | 272 | } |
259 | 273 | |
@@ -337,8 +351,10 @@ discard block |
||
337 | 351 | ], $this->options); |
338 | 352 | |
339 | 353 | // Create container services |
340 | - foreach ($container->config as $property => $class) { |
|
341 | - if (\property_exists($container, $property)) { |
|
354 | + foreach ($container->config as $property => $class) |
|
355 | + { |
|
356 | + if (\property_exists($container, $property)) |
|
357 | + { |
|
342 | 358 | $container->$property = $constructor->get($property, $class); |
343 | 359 | } |
344 | 360 | } |
@@ -352,7 +368,8 @@ discard block |
||
352 | 368 | private function closeScope(): void |
353 | 369 | { |
354 | 370 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
355 | - if (!isset($this->scope)) { |
|
371 | + if (!isset($this->scope)) |
|
372 | + { |
|
356 | 373 | $this->destruct(); |
357 | 374 | return; |
358 | 375 | } |
@@ -361,10 +378,14 @@ discard block |
||
361 | 378 | |
362 | 379 | // Run finalizers |
363 | 380 | $errors = []; |
364 | - foreach ($this->state->finalizers as $finalizer) { |
|
365 | - try { |
|
381 | + foreach ($this->state->finalizers as $finalizer) |
|
382 | + { |
|
383 | + try |
|
384 | + { |
|
366 | 385 | $this->invoker->invoke($finalizer); |
367 | - } catch (\Throwable $e) { |
|
386 | + } |
|
387 | + catch (\Throwable $e) |
|
388 | + { |
|
368 | 389 | $errors[] = $e; |
369 | 390 | } |
370 | 391 | } |
@@ -373,7 +394,8 @@ discard block |
||
373 | 394 | $this->destruct(); |
374 | 395 | |
375 | 396 | // Throw collected errors |
376 | - if ($errors !== []) { |
|
397 | + if ($errors !== []) |
|
398 | + { |
|
377 | 399 | throw new FinalizersException($scopeName, $errors); |
378 | 400 | } |
379 | 401 | } |
@@ -395,18 +417,22 @@ discard block |
||
395 | 417 | $container->scope->setParent($this, $this->scope, $this->factory); |
396 | 418 | |
397 | 419 | // Add specific bindings |
398 | - foreach ($config->bindings as $alias => $resolver) { |
|
420 | + foreach ($config->bindings as $alias => $resolver) |
|
421 | + { |
|
399 | 422 | $container->binder->bind($alias, $resolver); |
400 | 423 | } |
401 | 424 | |
402 | 425 | return ContainerScope::runScope( |
403 | 426 | $container, |
404 | 427 | static function (self $container) use ($config, $closure): mixed { |
405 | - try { |
|
428 | + try |
|
429 | + { |
|
406 | 430 | return $config->autowire |
407 | 431 | ? $container->invoke($closure) |
408 | 432 | : $closure($container); |
409 | - } finally { |
|
433 | + } |
|
434 | + finally |
|
435 | + { |
|
410 | 436 | $container->closeScope(); |
411 | 437 | } |
412 | 438 | }, |
@@ -10,4 +10,4 @@ |
||
10 | 10 | * Attention, this abstraction is currently under re-thinking process in order to replace it with |
11 | 11 | * bound context-specific factory (non breaking change). |
12 | 12 | */ |
13 | -interface InjectableInterface {} |
|
13 | +interface InjectableInterface{} |
@@ -10,4 +10,6 @@ |
||
10 | 10 | * Attention, this abstraction is currently under re-thinking process in order to replace it with |
11 | 11 | * bound context-specific factory (non breaking change). |
12 | 12 | */ |
13 | -interface InjectableInterface {} |
|
13 | +interface InjectableInterface |
|
14 | +{ |
|
15 | +} |
@@ -10,4 +10,4 @@ |
||
10 | 10 | * Class treated as singleton will only be constructed once in spiral IoC. |
11 | 11 | * @deprecated Use {@see Singleton} attribute instead. Will be removed in v4.0. |
12 | 12 | */ |
13 | -interface SingletonInterface {} |
|
13 | +interface SingletonInterface{} |
@@ -10,4 +10,6 @@ |
||
10 | 10 | * Class treated as singleton will only be constructed once in spiral IoC. |
11 | 11 | * @deprecated Use {@see Singleton} attribute instead. Will be removed in v4.0. |
12 | 12 | */ |
13 | -interface SingletonInterface {} |
|
13 | +interface SingletonInterface |
|
14 | +{ |
|
15 | +} |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | public function __construct( |
27 | 27 | public readonly string $alias, |
28 | 28 | public readonly array $parameters = [], |
29 | - ) {} |
|
29 | + ){} |
|
30 | 30 | |
31 | 31 | /** |
32 | 32 | * Init the autowire based on string or array definition. |
@@ -35,22 +35,22 @@ discard block |
||
35 | 35 | */ |
36 | 36 | public static function wire(mixed $definition): Autowire |
37 | 37 | { |
38 | - if ($definition instanceof self) { |
|
38 | + if ($definition instanceof self){ |
|
39 | 39 | return $definition; |
40 | 40 | } |
41 | 41 | |
42 | - if (\is_string($definition)) { |
|
42 | + if (\is_string($definition)){ |
|
43 | 43 | return new self($definition); |
44 | 44 | } |
45 | 45 | |
46 | - if (\is_array($definition) && isset($definition['class'])) { |
|
46 | + if (\is_array($definition) && isset($definition['class'])){ |
|
47 | 47 | return new self( |
48 | 48 | $definition['class'], |
49 | 49 | $definition['options'] ?? $definition['params'] ?? [], |
50 | 50 | ); |
51 | 51 | } |
52 | 52 | |
53 | - if (\is_object($definition)) { |
|
53 | + if (\is_object($definition)){ |
|
54 | 54 | $autowire = new self($definition::class, []); |
55 | 55 | $autowire->target = $definition; |
56 | 56 | return $autowire; |
@@ -26,7 +26,8 @@ discard block |
||
26 | 26 | public function __construct( |
27 | 27 | public readonly string $alias, |
28 | 28 | public readonly array $parameters = [], |
29 | - ) {} |
|
29 | + ) { |
|
30 | +} |
|
30 | 31 | |
31 | 32 | /** |
32 | 33 | * Init the autowire based on string or array definition. |
@@ -35,22 +36,26 @@ discard block |
||
35 | 36 | */ |
36 | 37 | public static function wire(mixed $definition): Autowire |
37 | 38 | { |
38 | - if ($definition instanceof self) { |
|
39 | + if ($definition instanceof self) |
|
40 | + { |
|
39 | 41 | return $definition; |
40 | 42 | } |
41 | 43 | |
42 | - if (\is_string($definition)) { |
|
44 | + if (\is_string($definition)) |
|
45 | + { |
|
43 | 46 | return new self($definition); |
44 | 47 | } |
45 | 48 | |
46 | - if (\is_array($definition) && isset($definition['class'])) { |
|
49 | + if (\is_array($definition) && isset($definition['class'])) |
|
50 | + { |
|
47 | 51 | return new self( |
48 | 52 | $definition['class'], |
49 | 53 | $definition['options'] ?? $definition['params'] ?? [], |
50 | 54 | ); |
51 | 55 | } |
52 | 56 | |
53 | - if (\is_object($definition)) { |
|
57 | + if (\is_object($definition)) |
|
58 | + { |
|
54 | 59 | $autowire = new self($definition::class, []); |
55 | 60 | $autowire->target = $definition; |
56 | 61 | return $autowire; |