@@ -30,12 +30,14 @@ discard block |
||
30 | 30 | SampleBoot::class, |
31 | 31 | ], |
32 | 32 | [ |
33 | - static function (Container $container, SampleBoot $boot) { |
|
33 | + static function (Container $container, SampleBoot $boot) |
|
34 | + { |
|
34 | 35 | $container->bind('efg', $boot); |
35 | 36 | }, |
36 | 37 | ], |
37 | 38 | [ |
38 | - static function (Container $container, SampleBoot $boot) { |
|
39 | + static function (Container $container, SampleBoot $boot) |
|
40 | + { |
|
39 | 41 | $container->bind('ghi', $boot); |
40 | 42 | }, |
41 | 43 | ], |
@@ -97,7 +99,8 @@ discard block |
||
97 | 99 | $bootloader = $this->getBootloadManager(); |
98 | 100 | |
99 | 101 | $bootloader->bootload([ |
100 | - new class () extends Bootloader { |
|
102 | + new class () extends Bootloader |
|
103 | + { |
|
101 | 104 | public const BINDINGS = ['abc' => self::class]; |
102 | 105 | public const SINGLETONS = ['single' => self::class]; |
103 | 106 |
@@ -75,7 +75,8 @@ |
||
75 | 75 | #[InjectorMethod(alias: SampleInjectableClass::class)] |
76 | 76 | protected function sampleInjector(): InjectorInterface |
77 | 77 | { |
78 | - return new class implements InjectorInterface { |
|
78 | + return new class implements InjectorInterface |
|
79 | + { |
|
79 | 80 | public function createInjection(\ReflectionClass $class, ?string $context = null): object |
80 | 81 | { |
81 | 82 | return new SampleInjectableClass('foo'); |
@@ -8,5 +8,6 @@ |
||
8 | 8 | { |
9 | 9 | public function __construct( |
10 | 10 | public string $name, |
11 | - ) {} |
|
11 | + ) { |
|
12 | +} |
|
12 | 13 | } |
@@ -4,4 +4,6 @@ |
||
4 | 4 | |
5 | 5 | namespace Spiral\Tests\Boot\Fixtures; |
6 | 6 | |
7 | -interface SampleClassInterface {} |
|
7 | +interface SampleClassInterface |
|
8 | +{ |
|
9 | +} |
@@ -4,4 +4,6 @@ |
||
4 | 4 | |
5 | 5 | namespace Spiral\Tests\Boot\Fixtures; |
6 | 6 | |
7 | -class SampleClass2 {} |
|
7 | +class SampleClass2 |
|
8 | +{ |
|
9 | +} |
@@ -4,4 +4,6 @@ |
||
4 | 4 | |
5 | 5 | namespace Spiral\Tests\Boot\Fixtures; |
6 | 6 | |
7 | -class SampleClass3 {} |
|
7 | +class SampleClass3 |
|
8 | +{ |
|
9 | +} |
@@ -53,7 +53,8 @@ |
||
53 | 53 | public function resolve(object $attribute, object $service, \ReflectionMethod $method): void |
54 | 54 | { |
55 | 55 | $attributeClass = $attribute::class; |
56 | - if (!isset($this->resolvers[$attributeClass])) { |
|
56 | + if (!isset($this->resolvers[$attributeClass])) |
|
57 | + { |
|
57 | 58 | throw new \RuntimeException("No resolver for attribute {$attributeClass}"); |
58 | 59 | } |
59 | 60 |
@@ -38,7 +38,8 @@ discard block |
||
38 | 38 | protected readonly BinderInterface $binder, |
39 | 39 | protected readonly ClassesRegistry $bootloaders = new ClassesRegistry(), |
40 | 40 | ?BootloaderCheckerInterface $checker = null, |
41 | - ) {} |
|
41 | + ) { |
|
42 | +} |
|
42 | 43 | |
43 | 44 | /** |
44 | 45 | * Instantiate bootloader objects and resolve dependencies |
@@ -52,21 +53,25 @@ discard block |
||
52 | 53 | { |
53 | 54 | $this->checker ??= $this->initDefaultChecker(); |
54 | 55 | |
55 | - foreach ($classes as $bootloader => $options) { |
|
56 | + foreach ($classes as $bootloader => $options) |
|
57 | + { |
|
56 | 58 | // default bootload syntax as simple array |
57 | - if (\is_string($options) || $options instanceof BootloaderInterface) { |
|
59 | + if (\is_string($options) || $options instanceof BootloaderInterface) |
|
60 | + { |
|
58 | 61 | $bootloader = $options; |
59 | 62 | $options = []; |
60 | 63 | } |
61 | 64 | $options = $useConfig ? $this->getBootloadConfig($bootloader, $options) : []; |
62 | 65 | |
63 | - if (!$this->checker->canInitialize($bootloader, $useConfig ? $options : null)) { |
|
66 | + if (!$this->checker->canInitialize($bootloader, $useConfig ? $options : null)) |
|
67 | + { |
|
64 | 68 | continue; |
65 | 69 | } |
66 | 70 | |
67 | 71 | $this->bootloaders->register($bootloader instanceof BootloaderInterface ? $bootloader::class : $bootloader); |
68 | 72 | |
69 | - if (!$bootloader instanceof BootloaderInterface) { |
|
73 | + if (!$bootloader instanceof BootloaderInterface) |
|
74 | + { |
|
70 | 75 | $bootloader = $this->container->get($bootloader); |
71 | 76 | } |
72 | 77 | |
@@ -104,11 +109,13 @@ discard block |
||
104 | 109 | */ |
105 | 110 | private function initBootloader(BootloaderInterface $bootloader): void |
106 | 111 | { |
107 | - foreach ($bootloader->defineBindings() as $alias => $resolver) { |
|
112 | + foreach ($bootloader->defineBindings() as $alias => $resolver) |
|
113 | + { |
|
108 | 114 | $this->binder->bind($alias, $resolver); |
109 | 115 | } |
110 | 116 | |
111 | - foreach ($bootloader->defineSingletons() as $alias => $resolver) { |
|
117 | + foreach ($bootloader->defineSingletons() as $alias => $resolver) |
|
118 | + { |
|
112 | 119 | $this->binder->bindSingleton($alias, $resolver); |
113 | 120 | } |
114 | 121 | |
@@ -153,7 +160,8 @@ discard block |
||
153 | 160 | string|BootloaderInterface $bootloader, |
154 | 161 | array|callable|BootloadConfig $config, |
155 | 162 | ): BootloadConfig { |
156 | - if ($config instanceof \Closure) { |
|
163 | + if ($config instanceof \Closure) |
|
164 | + { |
|
157 | 165 | $config = $this->container instanceof ResolverInterface |
158 | 166 | ? $config(...$this->container->resolveArguments(new \ReflectionFunction($config))) |
159 | 167 | : $config(); |
@@ -189,12 +197,14 @@ discard block |
||
189 | 197 | private function getBootloadConfigAttribute(string|BootloaderInterface $bootloader): ?BootloadConfig |
190 | 198 | { |
191 | 199 | $attribute = null; |
192 | - if ($bootloader instanceof BootloaderInterface || \class_exists($bootloader)) { |
|
200 | + if ($bootloader instanceof BootloaderInterface || \class_exists($bootloader)) |
|
201 | + { |
|
193 | 202 | $ref = new \ReflectionClass($bootloader); |
194 | 203 | $attribute = $ref->getAttributes(BootloadConfig::class)[0] ?? null; |
195 | 204 | } |
196 | 205 | |
197 | - if ($attribute === null) { |
|
206 | + if ($attribute === null) |
|
207 | + { |
|
198 | 208 | return null; |
199 | 209 | } |
200 | 210 | |
@@ -216,13 +226,16 @@ discard block |
||
216 | 226 | $methods = $initialMethods; |
217 | 227 | |
218 | 228 | $refl = new \ReflectionClass($bootloader); |
219 | - foreach ($refl->getMethods() as $method) { |
|
220 | - if ($method->isStatic()) { |
|
229 | + foreach ($refl->getMethods() as $method) |
|
230 | + { |
|
231 | + if ($method->isStatic()) |
|
232 | + { |
|
221 | 233 | continue; |
222 | 234 | } |
223 | 235 | |
224 | 236 | $attrs = $method->getAttributes($attribute); |
225 | - if (\count($attrs) === 0) { |
|
237 | + if (\count($attrs) === 0) |
|
238 | + { |
|
226 | 239 | continue; |
227 | 240 | } |
228 | 241 | /** @var InitMethod|BootMethod $attr */ |
@@ -242,7 +255,8 @@ discard block |
||
242 | 255 | */ |
243 | 256 | private function resolveAttributeBindings(BootloaderInterface $bootloader): void |
244 | 257 | { |
245 | - if (!$this->container->has(AttributeResolver::class)) { |
|
258 | + if (!$this->container->has(AttributeResolver::class)) |
|
259 | + { |
|
246 | 260 | return; |
247 | 261 | } |
248 | 262 | |
@@ -252,14 +266,18 @@ discard block |
||
252 | 266 | $availableAttributes = $attributeResolver->getResolvers(); |
253 | 267 | |
254 | 268 | $refl = new \ReflectionClass($bootloader); |
255 | - foreach ($refl->getMethods() as $method) { |
|
256 | - if ($method->isStatic()) { |
|
269 | + foreach ($refl->getMethods() as $method) |
|
270 | + { |
|
271 | + if ($method->isStatic()) |
|
272 | + { |
|
257 | 273 | continue; |
258 | 274 | } |
259 | 275 | |
260 | - foreach ($availableAttributes as $attributeClass) { |
|
276 | + foreach ($availableAttributes as $attributeClass) |
|
277 | + { |
|
261 | 278 | $attrs = $method->getAttributes($attributeClass); |
262 | - foreach ($attrs as $attr) { |
|
279 | + foreach ($attrs as $attr) |
|
280 | + { |
|
263 | 281 | $instance = $attr->newInstance(); |
264 | 282 | $attributeResolver->resolve($instance, $bootloader, $method); |
265 | 283 | } |
@@ -276,7 +294,8 @@ discard block |
||
276 | 294 | private function resolveDependencies(BootloaderInterface $bootloader, array $bootloaderMethods): iterable |
277 | 295 | { |
278 | 296 | $deps = $this->findDependenciesInMethods($bootloader, $bootloaderMethods); |
279 | - if ($bootloader instanceof DependedInterface) { |
|
297 | + if ($bootloader instanceof DependedInterface) |
|
298 | + { |
|
280 | 299 | $deps = [...$deps, ...$bootloader->defineDependencies()]; |
281 | 300 | } |
282 | 301 | |
@@ -293,8 +312,10 @@ discard block |
||
293 | 312 | |
294 | 313 | $methodsDeps = []; |
295 | 314 | |
296 | - foreach ($methods as $method) { |
|
297 | - if ($reflectionClass->hasMethod($method)) { |
|
315 | + foreach ($methods as $method) |
|
316 | + { |
|
317 | + if ($reflectionClass->hasMethod($method)) |
|
318 | + { |
|
298 | 319 | $methodsDeps[] = $this->findBootloaderClassesInMethod( |
299 | 320 | $reflectionClass->getMethod($method), |
300 | 321 | ); |
@@ -310,9 +331,11 @@ discard block |
||
310 | 331 | private function findBootloaderClassesInMethod(\ReflectionMethod $method): array |
311 | 332 | { |
312 | 333 | $args = []; |
313 | - foreach ($method->getParameters() as $parameter) { |
|
334 | + foreach ($method->getParameters() as $parameter) |
|
335 | + { |
|
314 | 336 | $type = $parameter->getType(); |
315 | - if ($type instanceof \ReflectionNamedType && $this->shouldBeBooted($type)) { |
|
337 | + if ($type instanceof \ReflectionNamedType && $this->shouldBeBooted($type)) |
|
338 | + { |
|
316 | 339 | $args[] = $type->getName(); |
317 | 340 | } |
318 | 341 | } |
@@ -21,7 +21,8 @@ discard block |
||
21 | 21 | { |
22 | 22 | public function __construct( |
23 | 23 | protected readonly BinderInterface $binder, |
24 | - ) {} |
|
24 | + ) { |
|
25 | +} |
|
25 | 26 | |
26 | 27 | /** |
27 | 28 | * @psalm-param T $attribute |
@@ -32,35 +33,44 @@ discard block |
||
32 | 33 | $alias = $attribute->alias ?? null; |
33 | 34 | |
34 | 35 | $aliases = []; |
35 | - if ($alias !== null) { |
|
36 | + if ($alias !== null) |
|
37 | + { |
|
36 | 38 | $aliases[] = $alias; |
37 | 39 | } |
38 | 40 | |
39 | 41 | $attrs = $method->getAttributes(name: BindAlias::class); |
40 | - foreach ($attrs as $attr) { |
|
42 | + foreach ($attrs as $attr) |
|
43 | + { |
|
41 | 44 | $aliases = [...$aliases, ...$attr->newInstance()->aliases]; |
42 | 45 | } |
43 | 46 | |
44 | 47 | // If no aliases are provided, we will use the return type as the alias. |
45 | - if (\count($aliases) > 0 && !$attribute->aliasesFromReturnType) { |
|
48 | + if (\count($aliases) > 0 && !$attribute->aliasesFromReturnType) |
|
49 | + { |
|
46 | 50 | return \array_unique(\array_filter($aliases)); |
47 | 51 | } |
48 | 52 | |
49 | 53 | $type = $method->getReturnType(); |
50 | 54 | |
51 | - if ($type instanceof \ReflectionUnionType || $type instanceof \ReflectionIntersectionType) { |
|
52 | - foreach ($type->getTypes() as $type) { |
|
53 | - if ($type->isBuiltin()) { |
|
55 | + if ($type instanceof \ReflectionUnionType || $type instanceof \ReflectionIntersectionType) |
|
56 | + { |
|
57 | + foreach ($type->getTypes() as $type) |
|
58 | + { |
|
59 | + if ($type->isBuiltin()) |
|
60 | + { |
|
54 | 61 | continue; |
55 | 62 | } |
56 | 63 | |
57 | 64 | $aliases[] = $type->getName(); |
58 | 65 | } |
59 | - } elseif ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) { |
|
66 | + } |
|
67 | + elseif ($type instanceof \ReflectionNamedType && !$type->isBuiltin()) |
|
68 | + { |
|
60 | 69 | $aliases[] = $type->getName(); |
61 | 70 | } |
62 | 71 | |
63 | - if ($aliases === []) { |
|
72 | + if ($aliases === []) |
|
73 | + { |
|
64 | 74 | throw new \LogicException( |
65 | 75 | "No alias provided for binding {$method->getDeclaringClass()->getName()}::{$method->getName()}", |
66 | 76 | ); |
@@ -73,7 +83,8 @@ discard block |
||
73 | 83 | { |
74 | 84 | $attrs = $method->getAttributes(name: BindScope::class); |
75 | 85 | |
76 | - if ($attrs === []) { |
|
86 | + if ($attrs === []) |
|
87 | + { |
|
77 | 88 | return null; |
78 | 89 | } |
79 | 90 | |
@@ -85,7 +96,8 @@ discard block |
||
85 | 96 | $binder = $this->binder->getBinder($scope); |
86 | 97 | |
87 | 98 | $alias = \array_shift($aliases); |
88 | - foreach ($aliases as $a) { |
|
99 | + foreach ($aliases as $a) |
|
100 | + { |
|
89 | 101 | $binder->bind($alias, $a); |
90 | 102 | $alias = \array_shift($aliases); |
91 | 103 | } |