@@ -26,11 +26,13 @@ discard block |
||
| 26 | 26 | SampleBootWithMethodBoot::class, |
| 27 | 27 | SampleBoot::class, |
| 28 | 28 | ], [ |
| 29 | - static function(Container $container, SampleBoot $boot) { |
|
| 29 | + static function(Container $container, SampleBoot $boot) |
|
| 30 | + { |
|
| 30 | 31 | $container->bind('efg', $boot); |
| 31 | 32 | } |
| 32 | 33 | ], [ |
| 33 | - static function(Container $container, SampleBoot $boot) { |
|
| 34 | + static function(Container $container, SampleBoot $boot) |
|
| 35 | + { |
|
| 34 | 36 | $container->bind('ghi', $boot); |
| 35 | 37 | } |
| 36 | 38 | ]); |
@@ -81,7 +83,8 @@ discard block |
||
| 81 | 83 | $bootloader = $this->getBootloadManager(); |
| 82 | 84 | |
| 83 | 85 | $bootloader->bootload([ |
| 84 | - new class () extends Bootloader { |
|
| 86 | + new class () extends Bootloader |
|
| 87 | + { |
|
| 85 | 88 | public const BINDINGS = ['abc' => self::class]; |
| 86 | 89 | public const SINGLETONS = ['single' => self::class]; |
| 87 | 90 | |
@@ -26,13 +26,15 @@ discard block |
||
| 26 | 26 | /** @psalm-suppress TooManyArguments */ |
| 27 | 27 | $bootloaders = \iterator_to_array($this->initializer->init($classes, $useRules)); |
| 28 | 28 | |
| 29 | - foreach ($bootloaders as $data) { |
|
| 29 | + foreach ($bootloaders as $data) |
|
| 30 | + { |
|
| 30 | 31 | $this->invokeBootloader($data['bootloader'], Methods::INIT, $data['options']); |
| 31 | 32 | } |
| 32 | 33 | |
| 33 | 34 | $this->fireCallbacks($bootingCallbacks); |
| 34 | 35 | |
| 35 | - foreach ($bootloaders as $data) { |
|
| 36 | + foreach ($bootloaders as $data) |
|
| 37 | + { |
|
| 36 | 38 | $this->invokeBootloader($data['bootloader'], Methods::BOOT, $data['options']); |
| 37 | 39 | } |
| 38 | 40 | |
@@ -42,14 +44,16 @@ discard block |
||
| 42 | 44 | private function invokeBootloader(BootloaderInterface $bootloader, Methods $method, array $options): void |
| 43 | 45 | { |
| 44 | 46 | $refl = new \ReflectionClass($bootloader); |
| 45 | - if (!$refl->hasMethod($method->value)) { |
|
| 47 | + if (!$refl->hasMethod($method->value)) |
|
| 48 | + { |
|
| 46 | 49 | return; |
| 47 | 50 | } |
| 48 | 51 | |
| 49 | 52 | $method = $refl->getMethod($method->value); |
| 50 | 53 | |
| 51 | 54 | $args = $this->resolver->resolveArguments($method); |
| 52 | - if (!isset($args['boot'])) { |
|
| 55 | + if (!isset($args['boot'])) |
|
| 56 | + { |
|
| 53 | 57 | $args['boot'] = $options; |
| 54 | 58 | } |
| 55 | 59 | |
@@ -61,7 +65,8 @@ discard block |
||
| 61 | 65 | */ |
| 62 | 66 | private function fireCallbacks(array $callbacks): void |
| 63 | 67 | { |
| 64 | - foreach ($callbacks as $callback) { |
|
| 68 | + foreach ($callbacks as $callback) |
|
| 69 | + { |
|
| 65 | 70 | $this->invoker->invoke($callback); |
| 66 | 71 | } |
| 67 | 72 | } |
@@ -17,24 +17,30 @@ |
||
| 17 | 17 | |
| 18 | 18 | public function canInitialize(BootloaderInterface|string $bootloader, ?BootloaderRules $rules = null): bool |
| 19 | 19 | { |
| 20 | - if ($rules === null) { |
|
| 20 | + if ($rules === null) |
|
| 21 | + { |
|
| 21 | 22 | return true; |
| 22 | 23 | } |
| 23 | 24 | |
| 24 | - if (!$rules->enabled) { |
|
| 25 | + if (!$rules->enabled) |
|
| 26 | + { |
|
| 25 | 27 | return false; |
| 26 | 28 | } |
| 27 | 29 | |
| 28 | - foreach ($rules->denyEnv as $env => $denyValues) { |
|
| 30 | + foreach ($rules->denyEnv as $env => $denyValues) |
|
| 31 | + { |
|
| 29 | 32 | $value = $this->environment->get($env); |
| 30 | - if ($value !== null && \in_array($value, (array) $denyValues, true)) { |
|
| 33 | + if ($value !== null && \in_array($value, (array) $denyValues, true)) |
|
| 34 | + { |
|
| 31 | 35 | return false; |
| 32 | 36 | } |
| 33 | 37 | } |
| 34 | 38 | |
| 35 | - foreach ($rules->allowEnv as $env => $allowValues) { |
|
| 39 | + foreach ($rules->allowEnv as $env => $allowValues) |
|
| 40 | + { |
|
| 36 | 41 | $value = $this->environment->get($env); |
| 37 | - if ($value === null || !\in_array($value, (array) $allowValues, true)) { |
|
| 42 | + if ($value === null || !\in_array($value, (array) $allowValues, true)) |
|
| 43 | + { |
|
| 38 | 44 | return false; |
| 39 | 45 | } |
| 40 | 46 | } |
@@ -15,11 +15,13 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | public function canInitialize(BootloaderInterface|string $bootloader, ?BootloaderRules $rules = null): bool |
| 17 | 17 | { |
| 18 | - if (!\is_string($bootloader)) { |
|
| 18 | + if (!\is_string($bootloader)) |
|
| 19 | + { |
|
| 19 | 20 | return true; |
| 20 | 21 | } |
| 21 | 22 | |
| 22 | - if (!\class_exists($bootloader)) { |
|
| 23 | + if (!\class_exists($bootloader)) |
|
| 24 | + { |
|
| 23 | 25 | throw new ClassNotFoundException(\sprintf('Bootloader class `%s` is not exist.', $bootloader)); |
| 24 | 26 | } |
| 25 | 27 | |
@@ -16,8 +16,10 @@ |
||
| 16 | 16 | |
| 17 | 17 | public function canInitialize(BootloaderInterface|string $bootloader, ?BootloaderRules $rules = null): bool |
| 18 | 18 | { |
| 19 | - foreach ($this->registry->getCheckers() as $checker) { |
|
| 20 | - if (!$checker->canInitialize($bootloader, $rules)) { |
|
| 19 | + foreach ($this->registry->getCheckers() as $checker) |
|
| 20 | + { |
|
| 21 | + if (!$checker->canInitialize($bootloader, $rules)) |
|
| 22 | + { |
|
| 21 | 23 | return false; |
| 22 | 24 | } |
| 23 | 25 | } |
@@ -45,21 +45,25 @@ discard block |
||
| 45 | 45 | { |
| 46 | 46 | $this->checker ??= $this->initDefaultChecker(); |
| 47 | 47 | |
| 48 | - foreach ($classes as $bootloader => $options) { |
|
| 48 | + foreach ($classes as $bootloader => $options) |
|
| 49 | + { |
|
| 49 | 50 | // default bootload syntax as simple array |
| 50 | - if (\is_string($options) || $options instanceof BootloaderInterface) { |
|
| 51 | + if (\is_string($options) || $options instanceof BootloaderInterface) |
|
| 52 | + { |
|
| 51 | 53 | $bootloader = $options; |
| 52 | 54 | $options = []; |
| 53 | 55 | } |
| 54 | 56 | $options = $useRules ? $this->getBootloaderRules($bootloader, $options) : []; |
| 55 | 57 | |
| 56 | - if (!$this->checker->canInitialize($bootloader, $useRules ? $options : null)) { |
|
| 58 | + if (!$this->checker->canInitialize($bootloader, $useRules ? $options : null)) |
|
| 59 | + { |
|
| 57 | 60 | continue; |
| 58 | 61 | } |
| 59 | 62 | |
| 60 | 63 | $this->bootloaders->register($bootloader instanceof BootloaderInterface ? $bootloader::class : $bootloader); |
| 61 | 64 | |
| 62 | - if (!$bootloader instanceof BootloaderInterface) { |
|
| 65 | + if (!$bootloader instanceof BootloaderInterface) |
|
| 66 | + { |
|
| 63 | 67 | $bootloader = $this->container->get($bootloader); |
| 64 | 68 | } |
| 65 | 69 | |
@@ -82,7 +86,8 @@ discard block |
||
| 82 | 86 | */ |
| 83 | 87 | protected function initBootloader(BootloaderInterface $bootloader): iterable |
| 84 | 88 | { |
| 85 | - if ($bootloader instanceof DependedInterface) { |
|
| 89 | + if ($bootloader instanceof DependedInterface) |
|
| 90 | + { |
|
| 86 | 91 | yield from $this->init($this->getDependencies($bootloader)); |
| 87 | 92 | } |
| 88 | 93 | |
@@ -100,11 +105,13 @@ discard block |
||
| 100 | 105 | */ |
| 101 | 106 | protected function initBindings(array $bindings, array $singletons): void |
| 102 | 107 | { |
| 103 | - foreach ($bindings as $aliases => $resolver) { |
|
| 108 | + foreach ($bindings as $aliases => $resolver) |
|
| 109 | + { |
|
| 104 | 110 | $this->binder->bind($aliases, $resolver); |
| 105 | 111 | } |
| 106 | 112 | |
| 107 | - foreach ($singletons as $aliases => $resolver) { |
|
| 113 | + foreach ($singletons as $aliases => $resolver) |
|
| 114 | + { |
|
| 108 | 115 | $this->binder->bindSingleton($aliases, $resolver); |
| 109 | 116 | } |
| 110 | 117 | } |
@@ -117,8 +124,10 @@ discard block |
||
| 117 | 124 | |
| 118 | 125 | $methodsDeps = []; |
| 119 | 126 | |
| 120 | - foreach (Methods::cases() as $method) { |
|
| 121 | - if ($reflectionClass->hasMethod($method->value)) { |
|
| 127 | + foreach (Methods::cases() as $method) |
|
| 128 | + { |
|
| 129 | + if ($reflectionClass->hasMethod($method->value)) |
|
| 130 | + { |
|
| 122 | 131 | $methodsDeps[] = $this->findBootloaderClassesInMethod( |
| 123 | 132 | $reflectionClass->getMethod($method->value) |
| 124 | 133 | ); |
@@ -131,9 +140,11 @@ discard block |
||
| 131 | 140 | protected function findBootloaderClassesInMethod(\ReflectionMethod $method): array |
| 132 | 141 | { |
| 133 | 142 | $args = []; |
| 134 | - foreach ($method->getParameters() as $parameter) { |
|
| 143 | + foreach ($method->getParameters() as $parameter) |
|
| 144 | + { |
|
| 135 | 145 | $type = $parameter->getType(); |
| 136 | - if ($type instanceof \ReflectionNamedType && $this->shouldBeBooted($type)) { |
|
| 146 | + if ($type instanceof \ReflectionNamedType && $this->shouldBeBooted($type)) |
|
| 147 | + { |
|
| 137 | 148 | $args[] = $type->getName(); |
| 138 | 149 | } |
| 139 | 150 | } |
@@ -178,7 +189,8 @@ discard block |
||
| 178 | 189 | string|BootloaderInterface $bootloader, |
| 179 | 190 | array|callable|BootloaderRules $rules |
| 180 | 191 | ): BootloaderRules { |
| 181 | - if ($rules instanceof \Closure) { |
|
| 192 | + if ($rules instanceof \Closure) |
|
| 193 | + { |
|
| 182 | 194 | $rules = $this->container instanceof ResolverInterface |
| 183 | 195 | ? $rules(...$this->container->resolveArguments(new \ReflectionFunction($rules))) |
| 184 | 196 | : $rules(); |
@@ -212,12 +224,14 @@ discard block |
||
| 212 | 224 | private function getBootloaderRulesAttribute(string|BootloaderInterface $bootloader): ?BootloaderRules |
| 213 | 225 | { |
| 214 | 226 | $attribute = null; |
| 215 | - if ($bootloader instanceof BootloaderInterface || \class_exists($bootloader)) { |
|
| 227 | + if ($bootloader instanceof BootloaderInterface || \class_exists($bootloader)) |
|
| 228 | + { |
|
| 216 | 229 | $ref = new \ReflectionClass($bootloader); |
| 217 | 230 | $attribute = $ref->getAttributes(BootloaderRules::class)[0] ?? null; |
| 218 | 231 | } |
| 219 | 232 | |
| 220 | - if ($attribute === null) { |
|
| 233 | + if ($attribute === null) |
|
| 234 | + { |
|
| 221 | 235 | return null; |
| 222 | 236 | } |
| 223 | 237 | |