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