1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Metadata\AvailableMigration; |
8
|
|
|
use Doctrine\Migrations\Metadata\AvailableMigrationsList; |
9
|
|
|
use Doctrine\Migrations\Metadata\ExecutedMigration; |
10
|
|
|
use Doctrine\Migrations\Metadata\ExecutedMigrationsSet; |
11
|
|
|
use Doctrine\Migrations\Version\Version; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use function array_map; |
15
|
|
|
use function array_merge; |
16
|
|
|
use function array_unique; |
17
|
|
|
use function uasort; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The ListCommand class is responsible for outputting a list of all available migrations and their status. |
21
|
|
|
*/ |
22
|
|
|
final class ListCommand extends DoctrineCommand |
23
|
|
|
{ |
24
|
|
|
/** @var string */ |
25
|
|
|
protected static $defaultName = 'migrations:list'; |
26
|
|
|
|
27
|
1 |
|
protected function configure() : void |
28
|
|
|
{ |
29
|
|
|
$this |
30
|
1 |
|
->setAliases(['list-migrations']) |
31
|
1 |
|
->setDescription('Display a list of all available migrations and their status.') |
32
|
1 |
|
->setHelp(<<<EOT |
33
|
1 |
|
The <info>%command.name%</info> command outputs a list of all available migrations and their status: |
34
|
|
|
|
35
|
|
|
<info>%command.full_name%</info> |
36
|
|
|
EOT |
37
|
|
|
); |
38
|
|
|
|
39
|
1 |
|
parent::configure(); |
40
|
1 |
|
} |
41
|
|
|
|
42
|
1 |
|
public function execute(InputInterface $input, OutputInterface $output) : int |
43
|
|
|
{ |
44
|
1 |
|
$versions = $this->getSortedVersions( |
45
|
1 |
|
$this->getDependencyFactory()->getMigrationRepository()->getMigrations(), // available migrations |
46
|
1 |
|
$this->getDependencyFactory()->getMetadataStorage()->getExecutedMigrations() // executed migrations |
47
|
|
|
); |
48
|
|
|
|
49
|
1 |
|
$this->getDependencyFactory()->getMigrationStatusInfosHelper()->listVersions($versions, $output); |
50
|
|
|
|
51
|
1 |
|
return 0; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return Version[] |
56
|
|
|
*/ |
57
|
1 |
|
private function getSortedVersions(AvailableMigrationsList $availableMigrations, ExecutedMigrationsSet $executedMigrations) : array |
58
|
|
|
{ |
59
|
|
|
$availableVersions = array_map(static function (AvailableMigration $availableMigration) : Version { |
60
|
1 |
|
return $availableMigration->getVersion(); |
61
|
1 |
|
}, $availableMigrations->getItems()); |
62
|
|
|
|
63
|
|
|
$executedVersions = array_map(static function (ExecutedMigration $executedMigration) : Version { |
64
|
1 |
|
return $executedMigration->getVersion(); |
65
|
1 |
|
}, $executedMigrations->getItems()); |
66
|
|
|
|
67
|
1 |
|
$versions = array_unique(array_merge($availableVersions, $executedVersions)); |
68
|
|
|
|
69
|
1 |
|
$comparator = $this->getDependencyFactory()->getVersionComparator(); |
70
|
|
|
uasort($versions, static function (Version $a, Version $b) use ($comparator) : int { |
71
|
1 |
|
return $comparator->compare($a, $b); |
72
|
1 |
|
}); |
73
|
|
|
|
74
|
1 |
|
return $versions; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|