@@ -32,21 +32,21 @@ discard block |
||
32 | 32 | { |
33 | 33 | public function __construct( |
34 | 34 | protected readonly State $state, |
35 | - ) {} |
|
35 | + ){} |
|
36 | 36 | |
37 | 37 | /** |
38 | 38 | * @param TResolver|object $resolver |
39 | 39 | */ |
40 | 40 | public function bind(string $alias, mixed $resolver): void |
41 | 41 | { |
42 | - if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) { |
|
42 | + if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))){ |
|
43 | 43 | $this->state->inflectors[$alias][] = $resolver; |
44 | 44 | return; |
45 | 45 | } |
46 | 46 | |
47 | - try { |
|
47 | + try{ |
|
48 | 48 | $config = $this->makeConfig($resolver, false); |
49 | - } catch (\Throwable $e) { |
|
49 | + }catch (\Throwable $e){ |
|
50 | 50 | throw $this->invalidBindingException($alias, $e); |
51 | 51 | } |
52 | 52 | |
@@ -58,9 +58,9 @@ discard block |
||
58 | 58 | */ |
59 | 59 | public function bindSingleton(string $alias, mixed $resolver): void |
60 | 60 | { |
61 | - try { |
|
61 | + try{ |
|
62 | 62 | $config = $this->makeConfig($resolver, true); |
63 | - } catch (\Throwable $e) { |
|
63 | + }catch (\Throwable $e){ |
|
64 | 64 | throw $this->invalidBindingException($alias, $e); |
65 | 65 | } |
66 | 66 | |
@@ -72,13 +72,13 @@ discard block |
||
72 | 72 | $bindings = &$this->state->bindings; |
73 | 73 | |
74 | 74 | $flags = []; |
75 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
75 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class){ |
|
76 | 76 | //Checking alias tree |
77 | - if ($flags[$binding->alias] ?? false) { |
|
77 | + if ($flags[$binding->alias] ?? false){ |
|
78 | 78 | return $binding->alias === $alias ?: throw new Exception('Circular alias detected'); |
79 | 79 | } |
80 | 80 | |
81 | - if (\array_key_exists($alias, $this->state->singletons)) { |
|
81 | + if (\array_key_exists($alias, $this->state->singletons)){ |
|
82 | 82 | return true; |
83 | 83 | } |
84 | 84 | |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | public function removeInjector(string $class): void |
103 | 103 | { |
104 | 104 | unset($this->state->injectors[$class]); |
105 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
105 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class){ |
|
106 | 106 | return; |
107 | 107 | } |
108 | 108 | unset($this->state->bindings[$class]); |
@@ -110,20 +110,20 @@ discard block |
||
110 | 110 | |
111 | 111 | public function hasInjector(string $class): bool |
112 | 112 | { |
113 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
113 | + if (\array_key_exists($class, $this->state->injectors)){ |
|
114 | 114 | return true; |
115 | 115 | } |
116 | 116 | |
117 | - try { |
|
117 | + try{ |
|
118 | 118 | $reflection = new \ReflectionClass($class); |
119 | - } catch (\ReflectionException $e) { |
|
119 | + }catch (\ReflectionException $e){ |
|
120 | 120 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
121 | 121 | } |
122 | 122 | |
123 | 123 | if ( |
124 | 124 | $reflection->implementsInterface(InjectableInterface::class) |
125 | 125 | && $reflection->hasConstant('INJECTOR') |
126 | - ) { |
|
126 | + ){ |
|
127 | 127 | $const = $reflection->getConstant('INJECTOR'); |
128 | 128 | $this->bindInjector($class, $const); |
129 | 129 | |
@@ -131,12 +131,12 @@ discard block |
||
131 | 131 | } |
132 | 132 | |
133 | 133 | // check interfaces |
134 | - foreach ($this->state->injectors as $target => $injector) { |
|
134 | + foreach ($this->state->injectors as $target => $injector){ |
|
135 | 135 | if ( |
136 | 136 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
137 | 137 | || |
138 | 138 | (\interface_exists($target, true) && $reflection->implementsInterface($target)) |
139 | - ) { |
|
139 | + ){ |
|
140 | 140 | $this->state->bindings[$class] = new Injectable($injector); |
141 | 141 | |
142 | 142 | return true; |
@@ -163,15 +163,15 @@ discard block |
||
163 | 163 | |
164 | 164 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
165 | 165 | { |
166 | - if (\is_callable($resolver)) { |
|
166 | + if (\is_callable($resolver)){ |
|
167 | 167 | return new Factory($resolver, $singleton); |
168 | 168 | } |
169 | 169 | |
170 | 170 | // Validate lazy invokable array |
171 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
171 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === ''){ |
|
172 | 172 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
173 | 173 | } |
174 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
174 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === ''){ |
|
175 | 175 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
176 | 176 | } |
177 | 177 | |
@@ -191,13 +191,13 @@ 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 | |
198 | 198 | $this->state->bindings[$alias] = $config; |
199 | 199 | |
200 | - if ($config instanceof Injectable) { |
|
200 | + if ($config instanceof Injectable){ |
|
201 | 201 | $this->state->injectors[$alias] = $config->injector; |
202 | 202 | } |
203 | 203 | } |
@@ -32,21 +32,26 @@ discard block |
||
32 | 32 | { |
33 | 33 | public function __construct( |
34 | 34 | protected readonly State $state, |
35 | - ) {} |
|
35 | + ) { |
|
36 | +} |
|
36 | 37 | |
37 | 38 | /** |
38 | 39 | * @param TResolver|object $resolver |
39 | 40 | */ |
40 | 41 | public function bind(string $alias, mixed $resolver): void |
41 | 42 | { |
42 | - if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) { |
|
43 | + if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) |
|
44 | + { |
|
43 | 45 | $this->state->inflectors[$alias][] = $resolver; |
44 | 46 | return; |
45 | 47 | } |
46 | 48 | |
47 | - try { |
|
49 | + try |
|
50 | + { |
|
48 | 51 | $config = $this->makeConfig($resolver, false); |
49 | - } catch (\Throwable $e) { |
|
52 | + } |
|
53 | + catch (\Throwable $e) |
|
54 | + { |
|
50 | 55 | throw $this->invalidBindingException($alias, $e); |
51 | 56 | } |
52 | 57 | |
@@ -58,9 +63,12 @@ discard block |
||
58 | 63 | */ |
59 | 64 | public function bindSingleton(string $alias, mixed $resolver): void |
60 | 65 | { |
61 | - try { |
|
66 | + try |
|
67 | + { |
|
62 | 68 | $config = $this->makeConfig($resolver, true); |
63 | - } catch (\Throwable $e) { |
|
69 | + } |
|
70 | + catch (\Throwable $e) |
|
71 | + { |
|
64 | 72 | throw $this->invalidBindingException($alias, $e); |
65 | 73 | } |
66 | 74 | |
@@ -72,13 +80,16 @@ discard block |
||
72 | 80 | $bindings = &$this->state->bindings; |
73 | 81 | |
74 | 82 | $flags = []; |
75 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
83 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) |
|
84 | + { |
|
76 | 85 | //Checking alias tree |
77 | - if ($flags[$binding->alias] ?? false) { |
|
86 | + if ($flags[$binding->alias] ?? false) |
|
87 | + { |
|
78 | 88 | return $binding->alias === $alias ?: throw new Exception('Circular alias detected'); |
79 | 89 | } |
80 | 90 | |
81 | - if (\array_key_exists($alias, $this->state->singletons)) { |
|
91 | + if (\array_key_exists($alias, $this->state->singletons)) |
|
92 | + { |
|
82 | 93 | return true; |
83 | 94 | } |
84 | 95 | |
@@ -102,7 +113,8 @@ discard block |
||
102 | 113 | public function removeInjector(string $class): void |
103 | 114 | { |
104 | 115 | unset($this->state->injectors[$class]); |
105 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
116 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) |
|
117 | + { |
|
106 | 118 | return; |
107 | 119 | } |
108 | 120 | unset($this->state->bindings[$class]); |
@@ -110,13 +122,17 @@ discard block |
||
110 | 122 | |
111 | 123 | public function hasInjector(string $class): bool |
112 | 124 | { |
113 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
125 | + if (\array_key_exists($class, $this->state->injectors)) |
|
126 | + { |
|
114 | 127 | return true; |
115 | 128 | } |
116 | 129 | |
117 | - try { |
|
130 | + try |
|
131 | + { |
|
118 | 132 | $reflection = new \ReflectionClass($class); |
119 | - } catch (\ReflectionException $e) { |
|
133 | + } |
|
134 | + catch (\ReflectionException $e) |
|
135 | + { |
|
120 | 136 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
121 | 137 | } |
122 | 138 | |
@@ -131,7 +147,8 @@ discard block |
||
131 | 147 | } |
132 | 148 | |
133 | 149 | // check interfaces |
134 | - foreach ($this->state->injectors as $target => $injector) { |
|
150 | + foreach ($this->state->injectors as $target => $injector) |
|
151 | + { |
|
135 | 152 | if ( |
136 | 153 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
137 | 154 | || |
@@ -163,15 +180,18 @@ discard block |
||
163 | 180 | |
164 | 181 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
165 | 182 | { |
166 | - if (\is_callable($resolver)) { |
|
183 | + if (\is_callable($resolver)) |
|
184 | + { |
|
167 | 185 | return new Factory($resolver, $singleton); |
168 | 186 | } |
169 | 187 | |
170 | 188 | // Validate lazy invokable array |
171 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
189 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') |
|
190 | + { |
|
172 | 191 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
173 | 192 | } |
174 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
193 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') |
|
194 | + { |
|
175 | 195 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
176 | 196 | } |
177 | 197 | |
@@ -191,13 +211,15 @@ discard block |
||
191 | 211 | |
192 | 212 | private function setBinding(string $alias, Binding $config): void |
193 | 213 | { |
194 | - if (isset($this->state->singletons[$alias])) { |
|
214 | + if (isset($this->state->singletons[$alias])) |
|
215 | + { |
|
195 | 216 | throw new SingletonOverloadException($alias); |
196 | 217 | } |
197 | 218 | |
198 | 219 | $this->state->bindings[$alias] = $config; |
199 | 220 | |
200 | - if ($config instanceof Injectable) { |
|
221 | + if ($config instanceof Injectable) |
|
222 | + { |
|
201 | 223 | $this->state->injectors[$alias] = $config->injector; |
202 | 224 | } |
203 | 225 | } |