@@ -33,34 +33,42 @@ discard block |
||
| 33 | 33 | */ |
| 34 | 34 | public function init(array $classes): \Generator |
| 35 | 35 | { |
| 36 | - foreach ($classes as $bootloader => $options) { |
|
| 36 | + foreach ($classes as $bootloader => $options) |
|
| 37 | + { |
|
| 37 | 38 | // default bootload syntax as simple array |
| 38 | - if (\is_string($options) || $options instanceof BootloaderInterface) { |
|
| 39 | + if (\is_string($options) || $options instanceof BootloaderInterface) |
|
| 40 | + { |
|
| 39 | 41 | $bootloader = $options; |
| 40 | 42 | $options = []; |
| 41 | 43 | } |
| 42 | 44 | |
| 43 | 45 | // Replace class aliases with source classes |
| 44 | - try { |
|
| 46 | + try |
|
| 47 | + { |
|
| 45 | 48 | $ref = (new \ReflectionClass($bootloader)); |
| 46 | 49 | $className = $ref->getName(); |
| 47 | - } catch (\ReflectionException) { |
|
| 50 | + } |
|
| 51 | + catch (\ReflectionException) |
|
| 52 | + { |
|
| 48 | 53 | throw new ClassNotFoundException( |
| 49 | 54 | \sprintf('Bootloader class `%s` is not exist.', $bootloader) |
| 50 | 55 | ); |
| 51 | 56 | } |
| 52 | 57 | |
| 53 | - if ($this->bootloaders->isBooted($className) || $ref->isAbstract()) { |
|
| 58 | + if ($this->bootloaders->isBooted($className) || $ref->isAbstract()) |
|
| 59 | + { |
|
| 54 | 60 | continue; |
| 55 | 61 | } |
| 56 | 62 | |
| 57 | 63 | $this->bootloaders->register($className); |
| 58 | 64 | |
| 59 | - if (!$bootloader instanceof BootloaderInterface) { |
|
| 65 | + if (!$bootloader instanceof BootloaderInterface) |
|
| 66 | + { |
|
| 60 | 67 | $bootloader = $this->container->get($bootloader); |
| 61 | 68 | } |
| 62 | 69 | |
| 63 | - if (!$this->isBootloader($bootloader)) { |
|
| 70 | + if (!$this->isBootloader($bootloader)) |
|
| 71 | + { |
|
| 64 | 72 | continue; |
| 65 | 73 | } |
| 66 | 74 | |
@@ -80,7 +88,8 @@ discard block |
||
| 80 | 88 | */ |
| 81 | 89 | protected function initBootloader(BootloaderInterface $bootloader): iterable |
| 82 | 90 | { |
| 83 | - if ($bootloader instanceof DependedInterface) { |
|
| 91 | + if ($bootloader instanceof DependedInterface) |
|
| 92 | + { |
|
| 84 | 93 | yield from $this->init($this->getDependencies($bootloader)); |
| 85 | 94 | } |
| 86 | 95 | |
@@ -98,11 +107,13 @@ discard block |
||
| 98 | 107 | */ |
| 99 | 108 | protected function initBindings(array $bindings, array $singletons): void |
| 100 | 109 | { |
| 101 | - foreach ($bindings as $aliases => $resolver) { |
|
| 110 | + foreach ($bindings as $aliases => $resolver) |
|
| 111 | + { |
|
| 102 | 112 | $this->binder->bind($aliases, $resolver); |
| 103 | 113 | } |
| 104 | 114 | |
| 105 | - foreach ($singletons as $aliases => $resolver) { |
|
| 115 | + foreach ($singletons as $aliases => $resolver) |
|
| 116 | + { |
|
| 106 | 117 | $this->binder->bindSingleton($aliases, $resolver); |
| 107 | 118 | } |
| 108 | 119 | } |
@@ -115,8 +126,10 @@ discard block |
||
| 115 | 126 | |
| 116 | 127 | $methodsDeps = []; |
| 117 | 128 | |
| 118 | - foreach (Methods::cases() as $method) { |
|
| 119 | - if ($reflectionClass->hasMethod($method->value)) { |
|
| 129 | + foreach (Methods::cases() as $method) |
|
| 130 | + { |
|
| 131 | + if ($reflectionClass->hasMethod($method->value)) |
|
| 132 | + { |
|
| 120 | 133 | $methodsDeps[] = $this->findBootloaderClassesInMethod( |
| 121 | 134 | $reflectionClass->getMethod($method->value) |
| 122 | 135 | ); |
@@ -129,9 +142,11 @@ discard block |
||
| 129 | 142 | protected function findBootloaderClassesInMethod(\ReflectionMethod $method): array |
| 130 | 143 | { |
| 131 | 144 | $args = []; |
| 132 | - foreach ($method->getParameters() as $parameter) { |
|
| 145 | + foreach ($method->getParameters() as $parameter) |
|
| 146 | + { |
|
| 133 | 147 | $type = $parameter->getType(); |
| 134 | - if ($type instanceof \ReflectionNamedType && $this->shouldBeBooted($type)) { |
|
| 148 | + if ($type instanceof \ReflectionNamedType && $this->shouldBeBooted($type)) |
|
| 149 | + { |
|
| 135 | 150 | $args[] = $type->getName(); |
| 136 | 151 | } |
| 137 | 152 | } |
@@ -28,11 +28,13 @@ discard block |
||
| 28 | 28 | SampleBootWithMethodBoot::class, |
| 29 | 29 | SampleBoot::class, |
| 30 | 30 | ], [ |
| 31 | - static function(Container $container, SampleBoot $boot) { |
|
| 31 | + static function(Container $container, SampleBoot $boot) |
|
| 32 | + { |
|
| 32 | 33 | $container->bind('efg', $boot); |
| 33 | 34 | } |
| 34 | 35 | ], [ |
| 35 | - static function(Container $container, SampleBoot $boot) { |
|
| 36 | + static function(Container $container, SampleBoot $boot) |
|
| 37 | + { |
|
| 36 | 38 | $container->bind('ghi', $boot); |
| 37 | 39 | } |
| 38 | 40 | ]); |
@@ -87,7 +89,8 @@ discard block |
||
| 87 | 89 | $bootloader = $this->getBootloadManager($container); |
| 88 | 90 | |
| 89 | 91 | $bootloader->bootload([ |
| 90 | - new class () extends Bootloader { |
|
| 92 | + new class () extends Bootloader |
|
| 93 | + { |
|
| 91 | 94 | public const BINDINGS = ['abc' => self::class]; |
| 92 | 95 | public const SINGLETONS = ['single' => self::class]; |
| 93 | 96 | |
@@ -108,18 +108,22 @@ discard block |
||
| 108 | 108 | ): static { |
| 109 | 109 | $exceptionHandler ??= ExceptionHandler::class; |
| 110 | 110 | |
| 111 | - if (\is_string($exceptionHandler)) { |
|
| 111 | + if (\is_string($exceptionHandler)) |
|
| 112 | + { |
|
| 112 | 113 | $exceptionHandler = $container->make($exceptionHandler); |
| 113 | 114 | } |
| 114 | 115 | |
| 115 | - if ($handleErrors) { |
|
| 116 | + if ($handleErrors) |
|
| 117 | + { |
|
| 116 | 118 | $exceptionHandler->register(); |
| 117 | 119 | } |
| 118 | 120 | |
| 119 | - if (!$container->has(InitializerInterface::class)) { |
|
| 121 | + if (!$container->has(InitializerInterface::class)) |
|
| 122 | + { |
|
| 120 | 123 | $container->bind(InitializerInterface::class, Initializer::class); |
| 121 | 124 | } |
| 122 | - if (!$container->has(InvokerStrategyInterface::class)) { |
|
| 125 | + if (!$container->has(InvokerStrategyInterface::class)) |
|
| 126 | + { |
|
| 123 | 127 | $container->bind(InvokerStrategyInterface::class, DefaultStrategy::class); |
| 124 | 128 | } |
| 125 | 129 | |
@@ -154,7 +158,8 @@ discard block |
||
| 154 | 158 | |
| 155 | 159 | $this->fireCallbacks($this->runningCallbacks); |
| 156 | 160 | |
| 157 | - try { |
|
| 161 | + try |
|
| 162 | + { |
|
| 158 | 163 | // will protect any against env overwrite action |
| 159 | 164 | $this->container->runScope( |
| 160 | 165 | [EnvironmentInterface::class => $environment], |
@@ -165,7 +170,9 @@ discard block |
||
| 165 | 170 | $this->fireCallbacks($this->bootstrappedCallbacks); |
| 166 | 171 | } |
| 167 | 172 | ); |
| 168 | - } catch (\Throwable $e) { |
|
| 173 | + } |
|
| 174 | + catch (\Throwable $e) |
|
| 175 | + { |
|
| 169 | 176 | $this->exceptionHandler->handleGlobalException($e); |
| 170 | 177 | |
| 171 | 178 | return null; |
@@ -186,7 +193,8 @@ discard block |
||
| 186 | 193 | */ |
| 187 | 194 | public function running(Closure ...$callbacks): void |
| 188 | 195 | { |
| 189 | - foreach ($callbacks as $callback) { |
|
| 196 | + foreach ($callbacks as $callback) |
|
| 197 | + { |
|
| 190 | 198 | $this->runningCallbacks[] = $callback; |
| 191 | 199 | } |
| 192 | 200 | } |
@@ -201,7 +209,8 @@ discard block |
||
| 201 | 209 | */ |
| 202 | 210 | public function booting(Closure ...$callbacks): void |
| 203 | 211 | { |
| 204 | - foreach ($callbacks as $callback) { |
|
| 212 | + foreach ($callbacks as $callback) |
|
| 213 | + { |
|
| 205 | 214 | $this->bootingCallbacks[] = $callback; |
| 206 | 215 | } |
| 207 | 216 | } |
@@ -216,7 +225,8 @@ discard block |
||
| 216 | 225 | */ |
| 217 | 226 | public function booted(Closure ...$callbacks): void |
| 218 | 227 | { |
| 219 | - foreach ($callbacks as $callback) { |
|
| 228 | + foreach ($callbacks as $callback) |
|
| 229 | + { |
|
| 220 | 230 | $this->bootedCallbacks[] = $callback; |
| 221 | 231 | } |
| 222 | 232 | } |
@@ -232,7 +242,8 @@ discard block |
||
| 232 | 242 | */ |
| 233 | 243 | public function bootstrapped(Closure ...$callbacks): void |
| 234 | 244 | { |
| 235 | - foreach ($callbacks as $callback) { |
|
| 245 | + foreach ($callbacks as $callback) |
|
| 246 | + { |
|
| 236 | 247 | $this->bootstrappedCallbacks[] = $callback; |
| 237 | 248 | } |
| 238 | 249 | } |
@@ -260,8 +271,10 @@ discard block |
||
| 260 | 271 | $eventDispatcher = $this->getEventDispatcher(); |
| 261 | 272 | $eventDispatcher?->dispatch(new Serving()); |
| 262 | 273 | |
| 263 | - foreach ($this->dispatchers as $dispatcher) { |
|
| 264 | - if ($dispatcher->canServe()) { |
|
| 274 | + foreach ($this->dispatchers as $dispatcher) |
|
| 275 | + { |
|
| 276 | + if ($dispatcher->canServe()) |
|
| 277 | + { |
|
| 265 | 278 | return $this->container->runScope( |
| 266 | 279 | [DispatcherInterface::class => $dispatcher], |
| 267 | 280 | static function () use ($dispatcher, $eventDispatcher): mixed { |
@@ -302,11 +315,13 @@ discard block |
||
| 302 | 315 | */ |
| 303 | 316 | protected function fireCallbacks(array &$callbacks): void |
| 304 | 317 | { |
| 305 | - if ($callbacks === []) { |
|
| 318 | + if ($callbacks === []) |
|
| 319 | + { |
|
| 306 | 320 | return; |
| 307 | 321 | } |
| 308 | 322 | |
| 309 | - do { |
|
| 323 | + do |
|
| 324 | + { |
|
| 310 | 325 | $this->container->invoke(\current($callbacks)); |
| 311 | 326 | } while (\next($callbacks)); |
| 312 | 327 | |