vendor/doctrine/migrations/lib/Doctrine/Migrations/FilesystemMigrationsRepository.php line 90

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations;
  4. use Doctrine\Migrations\Exception\DuplicateMigrationVersion;
  5. use Doctrine\Migrations\Exception\MigrationClassNotFound;
  6. use Doctrine\Migrations\Exception\MigrationException;
  7. use Doctrine\Migrations\Finder\MigrationFinder;
  8. use Doctrine\Migrations\Metadata\AvailableMigration;
  9. use Doctrine\Migrations\Metadata\AvailableMigrationsSet;
  10. use Doctrine\Migrations\Version\MigrationFactory;
  11. use Doctrine\Migrations\Version\Version;
  12. use function class_exists;
  13. /**
  14.  * The FilesystemMigrationsRepository class is responsible for retrieving migrations, determining what the current migration
  15.  * version, etc.
  16.  *
  17.  * @internal
  18.  */
  19. class FilesystemMigrationsRepository implements MigrationsRepository
  20. {
  21.     private bool $migrationsLoaded false;
  22.     /** @var array<string, string> */
  23.     private array $migrationDirectories;
  24.     private MigrationFinder $migrationFinder;
  25.     private MigrationFactory $versionFactory;
  26.     /** @var AvailableMigration[] */
  27.     private array $migrations = [];
  28.     /**
  29.      * @param string[]              $classes
  30.      * @param array<string, string> $migrationDirectories
  31.      */
  32.     public function __construct(
  33.         array $classes,
  34.         array $migrationDirectories,
  35.         MigrationFinder $migrationFinder,
  36.         MigrationFactory $versionFactory
  37.     ) {
  38.         $this->migrationDirectories $migrationDirectories;
  39.         $this->migrationFinder      $migrationFinder;
  40.         $this->versionFactory       $versionFactory;
  41.         $this->registerMigrations($classes);
  42.     }
  43.     private function registerMigrationInstance(Version $versionAbstractMigration $migration): AvailableMigration
  44.     {
  45.         if (isset($this->migrations[(string) $version])) {
  46.             throw DuplicateMigrationVersion::new(
  47.                 (string) $version,
  48.                 (string) $version
  49.             );
  50.         }
  51.         $this->migrations[(string) $version] = new AvailableMigration($version$migration);
  52.         return $this->migrations[(string) $version];
  53.     }
  54.     /** @throws MigrationException */
  55.     public function registerMigration(string $migrationClassName): AvailableMigration
  56.     {
  57.         $this->ensureMigrationClassExists($migrationClassName);
  58.         $version   = new Version($migrationClassName);
  59.         $migration $this->versionFactory->createVersion($migrationClassName);
  60.         return $this->registerMigrationInstance($version$migration);
  61.     }
  62.     /**
  63.      * @param string[] $migrations
  64.      *
  65.      * @return AvailableMigration[]
  66.      */
  67.     private function registerMigrations(array $migrations): array
  68.     {
  69.         $versions = [];
  70.         foreach ($migrations as $class) {
  71.             $versions[] = $this->registerMigration($class);
  72.         }
  73.         return $versions;
  74.     }
  75.     public function hasMigration(string $version): bool
  76.     {
  77.         $this->loadMigrationsFromDirectories();
  78.         return isset($this->migrations[$version]);
  79.     }
  80.     public function getMigration(Version $version): AvailableMigration
  81.     {
  82.         $this->loadMigrationsFromDirectories();
  83.         if (! isset($this->migrations[(string) $version])) {
  84.             throw MigrationClassNotFound::new((string) $version);
  85.         }
  86.         return $this->migrations[(string) $version];
  87.     }
  88.     /**
  89.      * Returns a non-sorted set of migrations.
  90.      */
  91.     public function getMigrations(): AvailableMigrationsSet
  92.     {
  93.         $this->loadMigrationsFromDirectories();
  94.         return new AvailableMigrationsSet($this->migrations);
  95.     }
  96.     /** @throws MigrationException */
  97.     private function ensureMigrationClassExists(string $class): void
  98.     {
  99.         if (! class_exists($class)) {
  100.             throw MigrationClassNotFound::new($class);
  101.         }
  102.     }
  103.     private function loadMigrationsFromDirectories(): void
  104.     {
  105.         $migrationDirectories $this->migrationDirectories;
  106.         if ($this->migrationsLoaded) {
  107.             return;
  108.         }
  109.         $this->migrationsLoaded true;
  110.         foreach ($migrationDirectories as $namespace => $path) {
  111.                 $migrations $this->migrationFinder->findMigrations(
  112.                     $path,
  113.                     $namespace
  114.                 );
  115.                 $this->registerMigrations($migrations);
  116.         }
  117.     }
  118. }