Skip to content

Commit

Permalink
Merge pull request #1207: Apply Spiral Code Style
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Jan 24, 2025
1 parent 57e57e5 commit fdacb1c
Show file tree
Hide file tree
Showing 78 changed files with 518 additions and 588 deletions.
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
],
"require": {
"php": ">=8.1",
"spiral/core": "^3.14.10",
"spiral/files": "^3.14.10",
"spiral/config": "^3.14.10",
"spiral/debug": "^3.14.10",
"spiral/exceptions": "^3.14.10",
"spiral/core": "^3.15",
"spiral/files": "^3.15",
"spiral/config": "^3.15",
"spiral/debug": "^3.15",
"spiral/exceptions": "^3.15",
"spiral/attributes": "^2.8|^3.0",
"spiral/events": "^3.14.10"
"spiral/events": "^3.15"
},
"require-dev": {
"phpunit/phpunit": "^10.1",
Expand Down
63 changes: 30 additions & 33 deletions src/AbstractKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Spiral\Boot;

use Closure;
use Psr\EventDispatcher\EventDispatcherInterface;
use Spiral\Attribute\DispatcherScope;
use Spiral\Boot\Bootloader\BootloaderRegistry;
Expand Down Expand Up @@ -57,16 +56,16 @@ abstract class AbstractKernel implements KernelInterface
*/
protected array $dispatchers = [];

/** @var array<Closure> */
/** @var array<\Closure> */
private array $runningCallbacks = [];

/** @var array<Closure> */
/** @var array<\Closure> */
private array $bootingCallbacks = [];

/** @var array<Closure> */
/** @var array<\Closure> */
private array $bootedCallbacks = [];

/** @var array<Closure> */
/** @var array<\Closure> */
private array $bootstrappedCallbacks = [];

/**
Expand All @@ -76,7 +75,7 @@ protected function __construct(
protected readonly Container $container,
protected readonly ExceptionHandlerInterface $exceptionHandler,
protected readonly BootloadManagerInterface $bootloader,
array $directories
array $directories,
) {
$container->bindSingleton(ExceptionHandlerInterface::class, $exceptionHandler);
$container->bindSingleton(ExceptionRendererInterface::class, $exceptionHandler);
Expand All @@ -89,21 +88,13 @@ protected function __construct(

$container->bindSingleton(
DirectoriesInterface::class,
new Directories($this->mapDirectories($directories))
new Directories($this->mapDirectories($directories)),
);

$this->finalizer = new Finalizer();
$container->bindSingleton(FinalizerInterface::class, $this->finalizer);
}

/**
* Terminate the application.
*/
public function __destruct()
{
$this->finalizer->finalize(true);
}

/**
* Create an application instance.
*
Expand All @@ -116,7 +107,7 @@ final public static function create(
bool $handleErrors = true,
ExceptionHandlerInterface|string|null $exceptionHandler = null,
Container $container = new Container(),
BootloadManagerInterface|Autowire|null $bootloadManager = null
BootloadManagerInterface|Autowire|null $bootloadManager = null,
): static {
$exceptionHandler ??= ExceptionHandler::class;

Expand Down Expand Up @@ -151,21 +142,20 @@ final public static function create(
$container,
$exceptionHandler,
$bootloadManager,
$directories
$directories,
);
}

/**
* Run the application with given Environment
*
* $app = App::create([...]);
* $app->booting(...);
* $app->booted(...);
* $app->bootstrapped(...);
* $app->run(new Environment([
* 'APP_ENV' => 'production'
* ]));
*
* $app = App::create([...]);
* $app->booting(...);
* $app->booted(...);
* $app->bootstrapped(...);
* $app->run(new Environment([
* 'APP_ENV' => 'production'
* ]));
*/
public function run(?EnvironmentInterface $environment = null): ?self
{
Expand All @@ -187,7 +177,7 @@ function (Container $container): void {
$this->bootstrap();

$this->fireCallbacks($this->bootstrappedCallbacks);
}
},
);
} catch (\Throwable $e) {
$this->exceptionHandler->handleGlobalException($e);
Expand All @@ -208,7 +198,7 @@ function (Container $container): void {
* $kernel->getContainer()->...
* });
*/
public function running(Closure ...$callbacks): void
public function running(\Closure ...$callbacks): void
{
foreach ($callbacks as $callback) {
$this->runningCallbacks[] = $callback;
Expand All @@ -223,7 +213,7 @@ public function running(Closure ...$callbacks): void
* $kernel->getContainer()->...
* });
*/
public function booting(Closure ...$callbacks): void
public function booting(\Closure ...$callbacks): void
{
foreach ($callbacks as $callback) {
$this->bootingCallbacks[] = $callback;
Expand All @@ -238,14 +228,13 @@ public function booting(Closure ...$callbacks): void
* $kernel->getContainer()->...
* });
*/
public function booted(Closure ...$callbacks): void
public function booted(\Closure ...$callbacks): void
{
foreach ($callbacks as $callback) {
$this->bootedCallbacks[] = $callback;
}
}


/**
* Register a new callback, that will be fired after framework bootstrapped.
* (Before serving)
Expand All @@ -254,7 +243,7 @@ public function booted(Closure ...$callbacks): void
* $kernel->getContainer()->...
* });
*/
public function bootstrapped(Closure ...$callbacks): void
public function bootstrapped(\Closure ...$callbacks): void
{
foreach ($callbacks as $callback) {
$this->bootstrappedCallbacks[] = $callback;
Expand Down Expand Up @@ -314,10 +303,18 @@ public function serve(): mixed
static function (DispatcherInterface $dispatcher) use ($eventDispatcher): mixed {
$eventDispatcher?->dispatch(new DispatcherFound($dispatcher));
return $dispatcher->serve();
}
},
);
}

/**
* Terminate the application.
*/
public function __destruct()
{
$this->finalizer->finalize(true);
}

/**
* Bootstrap application. Must be executed before serve method.
*/
Expand Down Expand Up @@ -376,7 +373,7 @@ private function bootload(array $bootloaders = []): void
static function () use ($self): void {
$self->fireCallbacks($self->bootingCallbacks);
},
]
],
);

$this->fireCallbacks($this->bootedCallbacks);
Expand Down
3 changes: 1 addition & 2 deletions src/Attribute/BootloadConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ public function __construct(
public array $allowEnv = [],
public array $denyEnv = [],
public bool $override = true,
) {
}
) {}
}
9 changes: 4 additions & 5 deletions src/BootloadManager/AbstractBootloadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ abstract class AbstractBootloadManager implements BootloadManagerInterface
{
public function __construct(
private readonly ScopeInterface $scope,
protected readonly InitializerInterface $initializer
) {
}
protected readonly InitializerInterface $initializer,
) {}

public function getClasses(): array
{
Expand All @@ -29,14 +28,14 @@ public function bootload(
array $classes,
array $bootingCallbacks = [],
array $bootedCallbacks = [],
bool $useConfig = true
bool $useConfig = true,
): void {
$this->scope->runScope(
[self::class => $this],
function () use ($classes, $bootingCallbacks, $bootedCallbacks, $useConfig): void {
/** @psalm-suppress TooManyArguments */
$this->boot($classes, $bootingCallbacks, $bootedCallbacks, $useConfig);
}
},
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/BootloadManager/BootloadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public function __construct(
private readonly InvokerInterface $invoker,
private readonly ResolverInterface $resolver,
InitializerInterface $initializer,
?InvokerStrategyInterface $invokerStrategy = null
?InvokerStrategyInterface $invokerStrategy = null,
) {
parent::__construct($scope, $initializer);

$this->invokerStrategy = $invokerStrategy ?? new DefaultInvokerStrategy(...$this->resolver->resolveArguments(
(new \ReflectionClass(DefaultInvokerStrategy::class))->getConstructor()
(new \ReflectionClass(DefaultInvokerStrategy::class))->getConstructor(),
));
}

Expand Down
3 changes: 1 addition & 2 deletions src/BootloadManager/Checker/BootloaderChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ final class BootloaderChecker implements BootloaderCheckerInterface
{
public function __construct(
private readonly CheckerRegistryInterface $registry = new CheckerRegistry(),
) {
}
) {}

public function canInitialize(BootloaderInterface|string $bootloader, ?BootloadConfig $config = null): bool
{
Expand Down
3 changes: 1 addition & 2 deletions src/BootloadManager/Checker/CanBootedChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ final class CanBootedChecker implements BootloaderCheckerInterface
{
public function __construct(
private readonly ClassesRegistry $bootloaders,
) {
}
) {}

public function canInitialize(BootloaderInterface|string $bootloader, ?BootloadConfig $config = null): bool
{
Expand Down
3 changes: 1 addition & 2 deletions src/BootloadManager/Checker/ConfigChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ final class ConfigChecker implements BootloaderCheckerInterface
{
public function __construct(
private readonly EnvironmentInterface $environment,
) {
}
) {}

public function canInitialize(BootloaderInterface|string $bootloader, ?BootloadConfig $config = null): bool
{
Expand Down
7 changes: 3 additions & 4 deletions src/BootloadManager/DefaultInvokerStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ final class DefaultInvokerStrategy implements InvokerStrategyInterface
public function __construct(
private readonly InitializerInterface $initializer,
private readonly InvokerInterface $invoker,
private readonly ResolverInterface $resolver
) {
}
private readonly ResolverInterface $resolver,
) {}

public function invokeBootloaders(
array $classes,
array $bootingCallbacks,
array $bootedCallbacks,
bool $useConfig = true
bool $useConfig = true,
): void {
/** @psalm-suppress TooManyArguments */
$bootloaders = \iterator_to_array($this->initializer->init($classes, $useConfig));
Expand Down
11 changes: 5 additions & 6 deletions src/BootloadManager/Initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public function __construct(
protected readonly BinderInterface $binder,
protected readonly ClassesRegistry $bootloaders = new ClassesRegistry(),
?BootloaderCheckerInterface $checker = null,
) {
}
) {}

/**
* Instantiate bootloader objects and resolve dependencies
Expand Down Expand Up @@ -89,7 +88,7 @@ protected function initBootloader(BootloaderInterface $bootloader): iterable

$this->initBindings(
$bootloader->defineBindings(),
$bootloader->defineSingletons()
$bootloader->defineSingletons(),
);
}

Expand Down Expand Up @@ -121,7 +120,7 @@ protected function getDependencies(DependedInterface $bootloader): array
foreach (Methods::cases() as $method) {
if ($reflectionClass->hasMethod($method->value)) {
$methodsDeps[] = $this->findBootloaderClassesInMethod(
$reflectionClass->getMethod($method->value)
$reflectionClass->getMethod($method->value),
);
}
}
Expand Down Expand Up @@ -177,7 +176,7 @@ protected function initDefaultChecker(): BootloaderCheckerInterface
*/
private function getBootloadConfig(
string|BootloaderInterface $bootloader,
array|callable|BootloadConfig $config
array|callable|BootloadConfig $config,
): BootloadConfig {
if ($config instanceof \Closure) {
$config = $this->container instanceof ResolverInterface
Expand All @@ -186,7 +185,7 @@ private function getBootloadConfig(
}
$attr = $this->getBootloadConfigAttribute($bootloader);

$getArgument = static fn (string $key, bool $override, mixed $default = []): mixed => match (true) {
$getArgument = static fn(string $key, bool $override, mixed $default = []): mixed => match (true) {
$config instanceof BootloadConfig && $override => $config->{$key},
$config instanceof BootloadConfig && !$override && \is_array($default) =>
$config->{$key} + ($attr->{$key} ?? []),
Expand Down
4 changes: 2 additions & 2 deletions src/BootloadManager/StrategyBasedBootloadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class StrategyBasedBootloadManager extends AbstractBootloadManager
public function __construct(
private readonly InvokerStrategyInterface $invoker,
ScopeInterface $scope,
InitializerInterface $initializer
InitializerInterface $initializer,
) {
parent::__construct($scope, $initializer);
}
Expand All @@ -27,7 +27,7 @@ protected function boot(
array $classes,
array $bootingCallbacks,
array $bootedCallbacks,
bool $useConfig = true
bool $useConfig = true,
): void {
/** @psalm-suppress TooManyArguments */
$this->invoker->invokeBootloaders($classes, $bootingCallbacks, $bootedCallbacks, $useConfig);
Expand Down
5 changes: 2 additions & 3 deletions src/BootloadManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Spiral\Boot;

use Closure;
use Spiral\Boot\Bootloader\BootloaderInterface;

/**
Expand All @@ -29,8 +28,8 @@ public function getClasses(): array;
* ]
*
* @param TClass[]|array<TClass, array<string,mixed>> $classes
* @param array<Closure> $bootingCallbacks
* @param array<Closure> $bootedCallbacks
* @param array<\Closure> $bootingCallbacks
* @param array<\Closure> $bootedCallbacks
*
* @throws \Throwable
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Bootloader/Bootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ abstract class Bootloader implements BootloaderInterface, DependedInterface
* @psalm-var TConstantBinding
*/
protected const BINDINGS = [];

/**
* @var array<string, class-string|callable>
* @psalm-var TConstantBinding
*/
protected const SINGLETONS = [];

/** @var class-string<BootloaderInterface>[]|class-string<DependedInterface>[] */
protected const DEPENDENCIES = [];

Expand Down
Loading

0 comments on commit fdacb1c

Please sign in to comment.