diff --git a/app/src/Application/Bootloader/PersistenceBootloader.php b/app/src/Application/Bootloader/PersistenceBootloader.php index d7470ed5..620afd30 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,9 @@ public function defineSingletons(): array ]; } - public function init(ConsoleBootloader $console, EventRepositoryInterface $repository): void + public function init(ConsoleBootloader $console, DriverEnum $driver): void { - if ($repository instanceof CycleOrmEventRepository) { + 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, + }; + } +}