diff --git a/src/Broadcasting/src/Config/BroadcastConfig.php b/src/Broadcasting/src/Config/BroadcastConfig.php index 709391265..23de3c15a 100644 --- a/src/Broadcasting/src/Config/BroadcastConfig.php +++ b/src/Broadcasting/src/Config/BroadcastConfig.php @@ -53,7 +53,7 @@ public function getAliases(): array */ public function getDefaultConnection(): string { - if (!isset($this->config['default']) || empty($this->config['default'])) { + if (empty($this->config['default'])) { throw new InvalidArgumentException('Default broadcast connection is not defined.'); } diff --git a/src/Broadcasting/tests/Config/BroadcastConfigTest.php b/src/Broadcasting/tests/Config/BroadcastConfigTest.php index a48d15160..66bcd221a 100644 --- a/src/Broadcasting/tests/Config/BroadcastConfigTest.php +++ b/src/Broadcasting/tests/Config/BroadcastConfigTest.php @@ -55,16 +55,6 @@ public function testGetsDefaultConnection(): void ); } - public function testNotDefinedDefaultKeyShouldThrowAnException(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectErrorMessage('Default broadcast connection is not defined.'); - - $config = new BroadcastConfig(); - - $config->getDefaultConnection(); - } - public function testGetsConnectionConfigByName(): void { $this->assertSame( diff --git a/src/Cache/src/Config/CacheConfig.php b/src/Cache/src/Config/CacheConfig.php index e05aa44aa..89abdd4fc 100644 --- a/src/Cache/src/Config/CacheConfig.php +++ b/src/Cache/src/Config/CacheConfig.php @@ -23,7 +23,7 @@ final class CacheConfig extends InjectableConfig */ public function getAliases(): array { - return (array)($this->config['aliases'] ?? []); + return $this->config['aliases']; } /** @@ -31,10 +31,6 @@ public function getAliases(): array */ public function getDefaultStorage(): string { - if (!isset($this->config['default']) || empty($this->config['default'])) { - throw new InvalidArgumentException('Default cache storage is not defined.'); - } - if (!\is_string($this->config['default'])) { throw new InvalidArgumentException('Default cache storage config value must be a string'); } diff --git a/src/Cache/tests/Config/CacheConfigTest.php b/src/Cache/tests/Config/CacheConfigTest.php index f18f1cd59..f0100da9f 100644 --- a/src/Cache/tests/Config/CacheConfigTest.php +++ b/src/Cache/tests/Config/CacheConfigTest.php @@ -50,16 +50,6 @@ public function testGetdDefaultDriver(): void ); } - public function testNotDefinedDefaultKeyShouldThrowAnException(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectErrorMessage('Default cache storage is not defined.'); - - $config = new CacheConfig(); - - $config->getDefaultStorage(); - } - public function testGetsStorageConfigByStorageName(): void { $this->assertSame( diff --git a/src/Console/src/StaticLocator.php b/src/Console/src/StaticLocator.php index 8005ff79e..4948db295 100644 --- a/src/Console/src/StaticLocator.php +++ b/src/Console/src/StaticLocator.php @@ -5,9 +5,11 @@ namespace Spiral\Console; use Psr\Container\ContainerInterface; +use Psr\EventDispatcher\EventDispatcherInterface; use Spiral\Console\Config\ConsoleConfig; use Spiral\Console\Traits\LazyTrait; use Spiral\Core\Container; +use Spiral\Events\EventDispatcherAwareInterface; use Symfony\Component\Console\Command\Command as SymfonyCommand; final class StaticLocator implements LocatorInterface @@ -20,8 +22,11 @@ final class StaticLocator implements LocatorInterface public function __construct( private readonly array $commands, private ConsoleConfig $config, - private ContainerInterface $container = new Container() + ContainerInterface $container = new Container(), + ?EventDispatcherInterface $eventDispatcher = null ) { + $this->dispatcher = $eventDispatcher; + $this->container = $container; } /** @@ -36,9 +41,13 @@ public function locateCommands(): array continue; } - $commands[] = $this->supportsLazyLoading($command) + $commands[] = $command = $this->supportsLazyLoading($command) ? $this->createLazyCommand($command) : $this->container->get($command); + + if ($this->dispatcher !== null && $command instanceof EventDispatcherAwareInterface) { + $command->setEventDispatcher($this->dispatcher); + } } return $commands; diff --git a/src/Console/src/Traits/LazyTrait.php b/src/Console/src/Traits/LazyTrait.php index 6af6bbe44..bef28d7c4 100644 --- a/src/Console/src/Traits/LazyTrait.php +++ b/src/Console/src/Traits/LazyTrait.php @@ -5,8 +5,10 @@ namespace Spiral\Console\Traits; use Psr\Container\ContainerInterface; +use Psr\EventDispatcher\EventDispatcherInterface; use Spiral\Console\Command as SpiralCommand; use Spiral\Console\Config\ConsoleConfig; +use Spiral\Events\EventDispatcherAwareInterface; use Symfony\Component\Console\Command\Command as SymfonyCommand; use Symfony\Component\Console\Command\LazyCommand; @@ -14,6 +16,7 @@ trait LazyTrait { private ContainerInterface $container; private ConsoleConfig $config; + private ?EventDispatcherInterface $dispatcher = null; /** * Check if command can be lazy-loaded. @@ -45,6 +48,10 @@ function () use ($class): SymfonyCommand { $command->setContainer($this->container); $command->setInterceptors($this->config->getInterceptors()); + if ($this->dispatcher !== null && $command instanceof EventDispatcherAwareInterface) { + $command->setEventDispatcher($this->dispatcher); + } + return $command; } ); diff --git a/src/Console/tests/BaseTest.php b/src/Console/tests/BaseTest.php index 9eb38b0af..7af49d580 100644 --- a/src/Console/tests/BaseTest.php +++ b/src/Console/tests/BaseTest.php @@ -5,6 +5,7 @@ namespace Spiral\Tests\Console; use PHPUnit\Framework\TestCase; +use Psr\EventDispatcher\EventDispatcherInterface; use Spiral\Console\CommandLocator; use Spiral\Console\Config\ConsoleConfig; use Spiral\Console\Console; @@ -41,14 +42,15 @@ public function setUp(): void ); } - protected function getCore(LocatorInterface $locator = null): Console + protected function getCore(LocatorInterface $locator = null, ?EventDispatcherInterface $eventDispatcher = null): Console { $config = $this->container->get(ConsoleConfig::class); return new Console( - $config, - $locator ?? $this->getStaticLocator([]), - $this->container + config: $config, + locator: $locator ?? $this->getStaticLocator([]), + container: $this->container, + dispatcher: $eventDispatcher ); } diff --git a/src/Console/tests/EventsTest.php b/src/Console/tests/EventsTest.php index 1aa005f62..4ef9953b6 100644 --- a/src/Console/tests/EventsTest.php +++ b/src/Console/tests/EventsTest.php @@ -19,15 +19,16 @@ public function testEventsShouldBeDispatched(): void ->method('dispatch') ->with( $this->callback( - static fn (CommandStarting|CommandFinished $event): bool => $event->command instanceof TestCommand + static fn(CommandStarting|CommandFinished $event): bool => $event->command instanceof TestCommand ), ); - $this->container->bind(EventDispatcherInterface::class, $dispatcher); - - $core = $this->getCore($this->getStaticLocator([ - TestCommand::class - ])); + $core = $this->getCore( + locator: $this->getStaticLocator([ + new TestCommand(), + ]), + eventDispatcher: $dispatcher + ); $core->run('test'); } diff --git a/src/Csrf/src/Config/CsrfConfig.php b/src/Csrf/src/Config/CsrfConfig.php index 4696ebaa6..14823162f 100644 --- a/src/Csrf/src/Config/CsrfConfig.php +++ b/src/Csrf/src/Config/CsrfConfig.php @@ -11,9 +11,10 @@ final class CsrfConfig extends InjectableConfig public const CONFIG = 'csrf'; protected array $config = [ - 'cookie' => 'csrf-token', - 'length' => 16, - 'lifetime' => 86400, + 'cookie' => 'csrf-token', + 'length' => 16, + 'lifetime' => null, + 'sameSite' => null, ]; public function getTokenLength(): int @@ -29,16 +30,16 @@ public function getCookie(): string public function getCookieLifetime(): ?int { - return $this->config['lifetime'] ?? null; + return $this->config['lifetime']; } public function isCookieSecure(): bool { - return !empty($this->config['secure']); + return ! empty($this->config['secure']); } public function getSameSite(): ?string { - return $this->config['sameSite'] ?? null; + return $this->config['sameSite']; } } diff --git a/src/Framework/Console/CommandLocator.php b/src/Framework/Console/CommandLocator.php index cf7b310e3..483ebbad6 100644 --- a/src/Framework/Console/CommandLocator.php +++ b/src/Framework/Console/CommandLocator.php @@ -5,8 +5,10 @@ namespace Spiral\Console; use Psr\Container\ContainerInterface; +use Psr\EventDispatcher\EventDispatcherInterface; use Spiral\Console\Config\ConsoleConfig; use Spiral\Console\Traits\LazyTrait; +use Spiral\Events\EventDispatcherAwareInterface; use Spiral\Tokenizer\ScopedClassesInterface; use Symfony\Component\Console\Command\Command as SymfonyCommand; @@ -17,9 +19,11 @@ final class CommandLocator implements LocatorInterface public function __construct( private readonly ScopedClassesInterface $classes, private ConsoleConfig $config, - ContainerInterface $container + ContainerInterface $container, + ?EventDispatcherInterface $eventDispatcher = null ) { $this->container = $container; + $this->dispatcher = $eventDispatcher; } public function locateCommands(): array @@ -30,9 +34,13 @@ public function locateCommands(): array continue; } - $commands[] = $this->supportsLazyLoading($class->getName()) + $commands[] = $command = $this->supportsLazyLoading($class->getName()) ? $this->createLazyCommand($class->getName()) : $this->container->get($class->getName()); + + if ($this->dispatcher !== null && $command instanceof EventDispatcherAwareInterface) { + $command->setEventDispatcher($this->dispatcher); + } } return $commands; diff --git a/src/Http/src/Config/HttpConfig.php b/src/Http/src/Config/HttpConfig.php index 3179b5984..d538819f0 100644 --- a/src/Http/src/Config/HttpConfig.php +++ b/src/Http/src/Config/HttpConfig.php @@ -41,7 +41,7 @@ public function getBaseHeaders(): array */ public function getMiddleware(): array { - return $this->config['middleware'] ?? $this->config['middlewares']; + return $this->config['middleware']; } public function getInputBags(): array diff --git a/src/Http/tests/ConfigTest.php b/src/Http/tests/ConfigTest.php index 2aefd3ef2..8badf079c 100644 --- a/src/Http/tests/ConfigTest.php +++ b/src/Http/tests/ConfigTest.php @@ -48,13 +48,4 @@ public function testBaseMiddleware(): void $this->assertSame([TestMiddleware::class], $c->getMiddleware()); } - - public function testBaseMiddlewareFallback(): void - { - $c = new HttpConfig([ - 'middlewares' => [TestMiddleware::class] - ]); - - $this->assertSame([TestMiddleware::class], $c->getMiddleware()); - } } diff --git a/src/Queue/src/Config/QueueConfig.php b/src/Queue/src/Config/QueueConfig.php index 1c90255dd..4a0659176 100644 --- a/src/Queue/src/Config/QueueConfig.php +++ b/src/Queue/src/Config/QueueConfig.php @@ -35,7 +35,7 @@ final class QueueConfig extends InjectableConfig */ public function getAliases(): array { - return (array)($this->config['aliases'] ?? []); + return $this->config['aliases']; } /** @@ -64,10 +64,6 @@ public function getPushInterceptors(): array */ public function getDefaultDriver(): string { - if (!isset($this->config['default']) || empty($this->config['default'])) { - throw new InvalidArgumentException('Default queue connection is not defined.'); - } - if (!\is_string($this->config['default'])) { throw new InvalidArgumentException('Default queue connection config value must be a string'); } diff --git a/src/Queue/tests/Config/QueueConfigTest.php b/src/Queue/tests/Config/QueueConfigTest.php index 88dd914b4..4ad250755 100644 --- a/src/Queue/tests/Config/QueueConfigTest.php +++ b/src/Queue/tests/Config/QueueConfigTest.php @@ -58,16 +58,6 @@ public function testGetsDefaultDriver(): void $this->assertSame('foo', $config->getDefaultDriver()); } - public function testGetsEmptyDefaultDriverShouldThrowAnException(): void - { - $this->expectException(InvalidArgumentException::class); - $this->expectErrorMessage('Default queue connection is not defined.'); - - $config = new QueueConfig(); - - $config->getDefaultDriver(); - } - public function testGetsNonStringDefaultDriverShouldThrowAnException(): void { $this->expectException(InvalidArgumentException::class); diff --git a/src/SendIt/tests/App/MailInterceptor.php b/src/SendIt/tests/App/MailInterceptor.php index bef445d80..ea879cf03 100644 --- a/src/SendIt/tests/App/MailInterceptor.php +++ b/src/SendIt/tests/App/MailInterceptor.php @@ -18,14 +18,14 @@ class MailInterceptor implements MailerInterface { - private $last; + private RawMessage $last; public function send(RawMessage $message, Envelope $envelope = null): void { $this->last = $message; } - public function getLast(): Email + public function getLast(): RawMessage { return $this->last; } diff --git a/src/SendIt/tests/RenderTest.php b/src/SendIt/tests/RenderTest.php index 35f253c4d..0f18386a0 100644 --- a/src/SendIt/tests/RenderTest.php +++ b/src/SendIt/tests/RenderTest.php @@ -17,7 +17,7 @@ use Spiral\Mailer\Message; /** - * @requires function \Spiral\Framework\Kernel::init + * @requires function \Spiral\Framework\Kernel::create */ class RenderTest extends TestCase { diff --git a/src/Translator/src/Config/TranslatorConfig.php b/src/Translator/src/Config/TranslatorConfig.php index bcac5f3be..3511e376b 100644 --- a/src/Translator/src/Config/TranslatorConfig.php +++ b/src/Translator/src/Config/TranslatorConfig.php @@ -19,7 +19,7 @@ final class TranslatorConfig extends InjectableConfig /** * @psalm-var array{ * locale: string, - * fallbackLocale: string, + * fallbackLocale?: string, * directory: string, * cacheLocales: bool, * autoRegister: bool, @@ -31,7 +31,6 @@ final class TranslatorConfig extends InjectableConfig */ protected array $config = [ 'locale' => '', - 'fallbackLocale' => '', 'directory' => '', 'cacheLocales' => true, 'autoRegister' => true,