Skip to content

Commit

Permalink
Merge pull request #136 from buggregator/maintenance/php82-min
Browse files Browse the repository at this point in the history
Maintenance/php82 min
  • Loading branch information
butschster authored Apr 14, 2024
2 parents 908dcc6 + fc8eadf commit 65e8d56
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
17 changes: 8 additions & 9 deletions app/src/Application/Bootloader/PersistenceBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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',
Expand Down
24 changes: 24 additions & 0 deletions app/src/Application/Persistence/DriverEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Application\Persistence;

use Spiral\Boot\EnvironmentInterface;
use Spiral\Boot\Injector\InjectableEnumInterface;
use Spiral\Boot\Injector\ProvideFrom;

#[ProvideFrom('detect')]
enum DriverEnum implements InjectableEnumInterface
{
case InMemory;
case Database;
case MongoDb;

public static function detect(EnvironmentInterface $env): self
{
return match ($env->get('PERSISTENCE_DRIVER', 'memory')) {
'cache', 'memory', 'in-memory' => self::InMemory,
'cycle', 'database', 'db' => self::Database,
'mongodb', 'mongo' => self::MongoDb,
};
}
}

0 comments on commit 65e8d56

Please sign in to comment.