DefaultAliasResolver.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Version;
  4. use Doctrine\Migrations\Exception\NoMigrationsFoundWithCriteria;
  5. use Doctrine\Migrations\Exception\NoMigrationsToExecute;
  6. use Doctrine\Migrations\Exception\UnknownMigrationVersion;
  7. use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
  8. use function substr;
  9. /**
  10. * The DefaultAliasResolver class is responsible for resolving aliases like first, current, etc. to the actual version number.
  11. *
  12. * @internal
  13. */
  14. final class DefaultAliasResolver implements AliasResolver
  15. {
  16. private const ALIAS_FIRST = 'first';
  17. private const ALIAS_CURRENT = 'current';
  18. private const ALIAS_PREV = 'prev';
  19. private const ALIAS_NEXT = 'next';
  20. private const ALIAS_LATEST = 'latest';
  21. /** @var MigrationPlanCalculator */
  22. private $migrationPlanCalculator;
  23. /** @var MetadataStorage */
  24. private $metadataStorage;
  25. /** @var MigrationStatusCalculator */
  26. private $migrationStatusCalculator;
  27. public function __construct(
  28. MigrationPlanCalculator $migrationPlanCalculator,
  29. MetadataStorage $metadataStorage,
  30. MigrationStatusCalculator $migrationStatusCalculator
  31. ) {
  32. $this->migrationPlanCalculator = $migrationPlanCalculator;
  33. $this->metadataStorage = $metadataStorage;
  34. $this->migrationStatusCalculator = $migrationStatusCalculator;
  35. }
  36. /**
  37. * Returns the version number from an alias.
  38. *
  39. * Supported aliases are:
  40. *
  41. * - first: The very first version before any migrations have been run.
  42. * - current: The current version.
  43. * - prev: The version prior to the current version.
  44. * - next: The version following the current version.
  45. * - latest: The latest available version.
  46. *
  47. * If an existing version number is specified, it is returned verbatimly.
  48. *
  49. * @throws NoMigrationsToExecute
  50. * @throws UnknownMigrationVersion
  51. * @throws NoMigrationsFoundWithCriteria
  52. */
  53. public function resolveVersionAlias(string $alias): Version
  54. {
  55. $availableMigrations = $this->migrationPlanCalculator->getMigrations();
  56. $executedMigrations = $this->metadataStorage->getExecutedMigrations();
  57. switch ($alias) {
  58. case self::ALIAS_FIRST:
  59. case '0':
  60. return new Version('0');
  61. case self::ALIAS_CURRENT:
  62. try {
  63. return $executedMigrations->getLast()->getVersion();
  64. } catch (NoMigrationsFoundWithCriteria $e) {
  65. return new Version('0');
  66. }
  67. // no break because of return
  68. case self::ALIAS_PREV:
  69. try {
  70. return $executedMigrations->getLast(-1)->getVersion();
  71. } catch (NoMigrationsFoundWithCriteria $e) {
  72. return new Version('0');
  73. }
  74. // no break because of return
  75. case self::ALIAS_NEXT:
  76. $newMigrations = $this->migrationStatusCalculator->getNewMigrations();
  77. try {
  78. return $newMigrations->getFirst()->getVersion();
  79. } catch (NoMigrationsFoundWithCriteria $e) {
  80. throw NoMigrationsToExecute::new($e);
  81. }
  82. // no break because of return
  83. case self::ALIAS_LATEST:
  84. try {
  85. return $availableMigrations->getLast()->getVersion();
  86. } catch (NoMigrationsFoundWithCriteria $e) {
  87. return $this->resolveVersionAlias(self::ALIAS_CURRENT);
  88. }
  89. // no break because of return
  90. default:
  91. if ($availableMigrations->hasMigration(new Version($alias))) {
  92. return $availableMigrations->getMigration(new Version($alias))->getVersion();
  93. }
  94. if (substr($alias, 0, 7) === self::ALIAS_CURRENT) {
  95. $val = (int) substr($alias, 7);
  96. $targetMigration = null;
  97. if ($val > 0) {
  98. $newMigrations = $this->migrationStatusCalculator->getNewMigrations();
  99. return $newMigrations->getFirst($val - 1)->getVersion();
  100. }
  101. return $executedMigrations->getLast($val)->getVersion();
  102. }
  103. }
  104. throw UnknownMigrationVersion::new($alias);
  105. }
  106. }