From 382821a24b3f6953bd148a435c42e5c9b4486b56 Mon Sep 17 00:00:00 2001 From: butschster Date: Sun, 14 Apr 2024 18:20:34 +0400 Subject: [PATCH 1/2] Fixes problem with starting app with database driver --- .../Bootloader/PersistenceBootloader.php | 18 +++++++------- .../Application/Persistence/DriverEnum.php | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 app/src/Application/Persistence/DriverEnum.php diff --git a/app/src/Application/Bootloader/PersistenceBootloader.php b/app/src/Application/Bootloader/PersistenceBootloader.php index d7470ed5..1d55f9f3 100644 --- a/app/src/Application/Bootloader/PersistenceBootloader.php +++ b/app/src/Application/Bootloader/PersistenceBootloader.php @@ -6,6 +6,7 @@ use App\Application\Persistence\CacheEventRepository; use App\Application\Persistence\CycleOrmEventRepository; +use App\Application\Persistence\DriverEnum; use App\Application\Persistence\MongoDBEventRepository; use Cycle\ORM\EntityManagerInterface; use Cycle\ORM\ORMInterface; @@ -14,7 +15,6 @@ use Modules\Events\Domain\EventRepositoryInterface; use MongoDB\Database; use Spiral\Boot\Bootloader\Bootloader; -use Spiral\Boot\EnvironmentInterface; use Spiral\Cache\CacheStorageProviderInterface; use Spiral\Console\Bootloader\ConsoleBootloader; use Spiral\Core\FactoryInterface; @@ -26,12 +26,11 @@ public function defineSingletons(): array return [ EventRepositoryInterface::class => static fn( FactoryInterface $factory, - EnvironmentInterface $env, - ): EventRepositoryInterface => match ($env->get('PERSISTENCE_DRIVER', 'cache')) { - 'cycle', 'database', 'db' => $factory->make(CycleOrmEventRepository::class), - 'mongodb', 'mongo' => $factory->make(MongoDBEventRepository::class), - 'cache', 'memory' => $factory->make(CacheEventRepository::class), - default => throw new \InvalidArgumentException('Unknown persistence driver'), + DriverEnum $driver, + ): EventRepositoryInterface => match ($driver) { + DriverEnum::Database => $factory->make(CycleOrmEventRepository::class), + DriverEnum::MongoDb => $factory->make(MongoDBEventRepository::class), + DriverEnum::InMemory => $factory->make(CacheEventRepository::class), }, CycleOrmEventRepository::class => static fn( ORMInterface $orm, @@ -48,9 +47,10 @@ public function defineSingletons(): array ]; } - public function init(ConsoleBootloader $console, EventRepositoryInterface $repository): void + public function init(ConsoleBootloader $console, DriverEnum $driver): void { - if ($repository instanceof CycleOrmEventRepository) { + var_dump($driver); + if ($driver === DriverEnum::Database) { $console->addConfigureSequence( sequence: 'migrate', header: 'Migration', diff --git a/app/src/Application/Persistence/DriverEnum.php b/app/src/Application/Persistence/DriverEnum.php new file mode 100644 index 00000000..bb7ab04b --- /dev/null +++ b/app/src/Application/Persistence/DriverEnum.php @@ -0,0 +1,24 @@ +get('PERSISTENCE_DRIVER', 'memory')) { + 'cache', 'memory', 'in-memory' => self::InMemory, + 'cycle', 'database', 'db' => self::Database, + 'mongodb', 'mongo' => self::MongoDb, + }; + } +} From fc8eadf4f81a633ec7594bace3d3cc98eaf6ceb8 Mon Sep 17 00:00:00 2001 From: butschster Date: Sun, 14 Apr 2024 18:22:49 +0400 Subject: [PATCH 2/2] cleanup --- app/src/Application/Bootloader/PersistenceBootloader.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/Application/Bootloader/PersistenceBootloader.php b/app/src/Application/Bootloader/PersistenceBootloader.php index 1d55f9f3..620afd30 100644 --- a/app/src/Application/Bootloader/PersistenceBootloader.php +++ b/app/src/Application/Bootloader/PersistenceBootloader.php @@ -49,7 +49,6 @@ public function defineSingletons(): array public function init(ConsoleBootloader $console, DriverEnum $driver): void { - var_dump($driver); if ($driver === DriverEnum::Database) { $console->addConfigureSequence( sequence: 'migrate',