|
| 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 */ |
| 23 | + private $container; |
| 24 | + |
| 25 | + /** @var AvailableMigration[] */ |
| 26 | + private $migrations = []; |
| 27 | + |
| 28 | + public function __construct( |
| 29 | + MigrationsRepository $migrationRepository, |
| 30 | + ServiceProviderInterface $container |
| 31 | + ) { |
| 32 | + $this->migrationRepository = $migrationRepository; |
| 33 | + $this->container = $container; |
| 34 | + } |
| 35 | + |
| 36 | + public function hasMigration(string $version): bool |
| 37 | + { |
| 38 | + return $this->container->has($version) || $this->migrationRepository->hasMigration($version); |
| 39 | + } |
| 40 | + |
| 41 | + public function getMigration(Version $version): AvailableMigration |
| 42 | + { |
| 43 | + if (! isset($this->migrations[(string) $version]) && ! $this->loadMigrationFromContainer($version)) { |
| 44 | + return $this->migrationRepository->getMigration($version); |
| 45 | + } |
| 46 | + |
| 47 | + return $this->migrations[(string) $version]; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns a non-sorted set of migrations. |
| 52 | + */ |
| 53 | + public function getMigrations(): AvailableMigrationsSet |
| 54 | + { |
| 55 | + $this->loadMigrationsFromContainer(); |
| 56 | + |
| 57 | + $migrations = $this->migrations; |
| 58 | + foreach ($this->migrationRepository->getMigrations()->getItems() as $availableMigration) { |
| 59 | + $version = $availableMigration->getVersion(); |
| 60 | + |
| 61 | + if (isset($migrations[(string) $version])) { |
| 62 | + throw DuplicateMigrationVersion::new( |
| 63 | + (string) $version, |
| 64 | + (string) $version |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + $migrations[(string) $version] = $availableMigration; |
| 69 | + } |
| 70 | + |
| 71 | + return new AvailableMigrationsSet($migrations); |
| 72 | + } |
| 73 | + |
| 74 | + private function loadMigrationsFromContainer(): void |
| 75 | + { |
| 76 | + foreach ($this->container->getProvidedServices() as $id) { |
| 77 | + if (isset($this->migrations[$id])) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + $this->loadMigrationFromContainer(new Version($id)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private function loadMigrationFromContainer(Version $version): bool |
| 86 | + { |
| 87 | + if (! $this->container->has((string) $version)) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + $migration = $this->container->get((string) $version); |
| 92 | + assert($migration instanceof AbstractMigration); |
| 93 | + |
| 94 | + $this->migrations[(string) $version] = new AvailableMigration($version, $migration); |
| 95 | + |
| 96 | + return true; |
| 97 | + } |
| 98 | +} |
0 commit comments