|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Bundle\MigrationsBundle\MigrationsRepository; |
| 6 | + |
| 7 | +use Doctrine\Migrations\AbstractMigration; |
| 8 | +use Doctrine\Migrations\Exception\DuplicateMigrationVersion; |
| 9 | +use Doctrine\Migrations\Metadata\AvailableMigration; |
| 10 | +use Doctrine\Migrations\Metadata\AvailableMigrationsSet; |
| 11 | +use Doctrine\Migrations\MigrationsRepository; |
| 12 | +use Doctrine\Migrations\Version\Version; |
| 13 | +use Symfony\Contracts\Service\ServiceProviderInterface; |
| 14 | + |
| 15 | +use function assert; |
| 16 | + |
| 17 | +class ServiceMigrationsRepository implements MigrationsRepository |
| 18 | +{ |
| 19 | + /** @var MigrationsRepository */ |
| 20 | + private $migrationRepository; |
| 21 | + |
| 22 | + /** @var ServiceProviderInterface<AbstractMigration> */ |
| 23 | + private $container; |
| 24 | + |
| 25 | + /** @var AvailableMigration[] */ |
| 26 | + private $migrations = []; |
| 27 | + |
| 28 | + /** @param ServiceProviderInterface<AbstractMigration> $container */ |
| 29 | + public function __construct( |
| 30 | + MigrationsRepository $migrationRepository, |
| 31 | + ServiceProviderInterface $container |
| 32 | + ) { |
| 33 | + $this->migrationRepository = $migrationRepository; |
| 34 | + $this->container = $container; |
| 35 | + } |
| 36 | + |
| 37 | + public function hasMigration(string $version): bool |
| 38 | + { |
| 39 | + return $this->container->has($version) || $this->migrationRepository->hasMigration($version); |
| 40 | + } |
| 41 | + |
| 42 | + public function getMigration(Version $version): AvailableMigration |
| 43 | + { |
| 44 | + if (! isset($this->migrations[(string) $version]) && ! $this->loadMigrationFromContainer($version)) { |
| 45 | + return $this->migrationRepository->getMigration($version); |
| 46 | + } |
| 47 | + |
| 48 | + return $this->migrations[(string) $version]; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns a non-sorted set of migrations. |
| 53 | + */ |
| 54 | + public function getMigrations(): AvailableMigrationsSet |
| 55 | + { |
| 56 | + $this->loadMigrationsFromContainer(); |
| 57 | + |
| 58 | + $migrations = $this->migrations; |
| 59 | + foreach ($this->migrationRepository->getMigrations()->getItems() as $availableMigration) { |
| 60 | + $version = $availableMigration->getVersion(); |
| 61 | + |
| 62 | + if (isset($migrations[(string) $version])) { |
| 63 | + throw DuplicateMigrationVersion::new( |
| 64 | + (string) $version, |
| 65 | + (string) $version |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + $migrations[(string) $version] = $availableMigration; |
| 70 | + } |
| 71 | + |
| 72 | + return new AvailableMigrationsSet($migrations); |
| 73 | + } |
| 74 | + |
| 75 | + private function loadMigrationsFromContainer(): void |
| 76 | + { |
| 77 | + foreach ($this->container->getProvidedServices() as $id) { |
| 78 | + if (isset($this->migrations[$id])) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + $this->loadMigrationFromContainer(new Version($id)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private function loadMigrationFromContainer(Version $version): bool |
| 87 | + { |
| 88 | + if (! $this->container->has((string) $version)) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + $migration = $this->container->get((string) $version); |
| 93 | + $this->migrations[(string) $version] = new AvailableMigration($version, $migration); |
| 94 | + |
| 95 | + return true; |
| 96 | + } |
| 97 | +} |
0 commit comments