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