|
1 | 1 | <?php |
2 | 2 |
|
3 | | -/* |
4 | | - * This file is part of the Doctrine MigrationsBundle |
5 | | - * |
6 | | - * The code was originally distributed inside the Symfony framework. |
7 | | - * |
8 | | - * (c) Michael Simonson <[email protected]> |
9 | | - * |
10 | | - * For the full copyright and license information, please view the LICENSE |
11 | | - * file that was distributed with this source code. |
12 | | - */ |
| 3 | +declare(strict_types=1); |
13 | 4 |
|
14 | 5 | namespace Doctrine\Bundle\MigrationsBundle\Collector; |
15 | 6 |
|
16 | | - |
17 | | -use Doctrine\DBAL\Connection; |
18 | | -use Doctrine\DBAL\Migrations\Configuration\Configuration; |
19 | | -use Doctrine\DBAL\Migrations\Tools\Console\Helper\MigrationStatusInfosHelper; |
| 7 | +use Doctrine\Migrations\DependencyFactory; |
| 8 | +use Doctrine\Migrations\Metadata\AvailableMigration; |
| 9 | +use Doctrine\Migrations\Metadata\AvailableMigrationsList; |
| 10 | +use Doctrine\Migrations\Metadata\ExecutedMigration; |
| 11 | +use Doctrine\Migrations\Metadata\ExecutedMigrationsList; |
| 12 | +use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration; |
20 | 13 | use Symfony\Component\HttpFoundation\Request; |
21 | 14 | use Symfony\Component\HttpFoundation\Response; |
22 | | -use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface; |
| 15 | +use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
23 | 16 |
|
24 | | -class MigrationsCollector implements DataCollectorInterface |
| 17 | +class MigrationsCollector extends DataCollector |
25 | 18 | { |
26 | | - /** @var string */ |
27 | | - private $migrationTablename; |
28 | | - |
29 | | - /** @var string */ |
30 | | - private $migrationName; |
31 | | - |
32 | | - /** @var Connection */ |
33 | | - private $connection; |
34 | | - |
35 | | - /** @var string */ |
36 | | - private $migrationNamespace; |
37 | | - |
38 | | - /** @var string */ |
39 | | - private $migrationDirectory; |
| 19 | + /** @var DependencyFactory */ |
| 20 | + private $dependencyFactory; |
40 | 21 |
|
41 | | - public function __construct(Connection $connection, $migrationNamespace, $migrationDirectory, $migrationTablename, $migrationName) |
| 22 | + public function __construct(DependencyFactory $dependencyFactory) |
42 | 23 | { |
43 | | - $this->connection = $connection; |
44 | | - $this->migrationNamespace = $migrationNamespace; |
45 | | - $this->migrationDirectory = $migrationDirectory; |
46 | | - $this->migrationTablename = $migrationTablename; |
47 | | - $this->migrationName = $migrationName; |
| 24 | + $this->dependencyFactory = $dependencyFactory; |
48 | 25 | } |
49 | 26 |
|
50 | | - /** |
51 | | - * Collects data for the given Request and Response. |
52 | | - * |
53 | | - * @param Request $request A Request instance |
54 | | - * @param Response $response A Response instance |
55 | | - * @param \Exception $exception An Exception instance |
56 | | - */ |
57 | | - public function collect(Request $request, Response $response, \Exception $exception = null) |
| 27 | + public function collect(Request $request, Response $response, \Throwable $exception = null) |
58 | 28 | { |
59 | | - $configuration = new Configuration($this->connection); |
60 | | - $configuration->setMigrationsNamespace($this->migrationNamespace); |
61 | | - $configuration->setMigrationsDirectory($this->migrationDirectory); |
62 | | - $configuration->setMigrationsTableName($this->migrationTablename); |
63 | | - $configuration->setName($this->migrationName); |
64 | | - $configuration->registerMigrationsFromDirectory($configuration->getMigrationsDirectory()); |
65 | | - $migrationsStatusInfos = new MigrationStatusInfosHelper($configuration); |
66 | | - $this->data = $migrationsStatusInfos->getMigrationsInfos(); |
67 | | - $newMigrationsList = $configuration->getMigrationsToExecute('up', $configuration->getLatestVersion()); |
68 | | - $this->data['newMigrationsList'] = array_map(function($migration) { |
69 | | - return $migration->getVersion(); |
70 | | - }, $newMigrationsList); |
71 | | - $this->data['executedUnavailableMigrationsList'] = array_map(function($migration) { |
72 | | - return $migration->getVersion(); |
73 | | - }, $migrationsStatusInfos->getExecutedUnavailableMigrations()); |
| 29 | + $metadataStorage = $this->dependencyFactory->getMetadataStorage(); |
| 30 | + $planCalculator = $this->dependencyFactory->getMigrationPlanCalculator(); |
| 31 | + $statusCalculator = $this->dependencyFactory->getMigrationStatusCalculator(); |
74 | 32 |
|
75 | | - $this->connection = null; |
76 | | - $this->migrationDirectory = null; |
77 | | - $this->migrationNamespace = null; |
78 | | - $this->migrationName = null; |
79 | | - $this->migrationTablename = null; |
80 | | - } |
81 | | - |
82 | | - /** |
83 | | - * Returns the name of the collector. |
84 | | - * |
85 | | - * @return string The collector name |
86 | | - */ |
87 | | - public function getName() |
88 | | - { |
89 | | - return 'doctrine.migrations_collector'; |
90 | | - } |
91 | | - |
92 | | - public function getPreviousMigration() |
93 | | - { |
94 | | - return $this->data['Previous Version']; |
95 | | - } |
96 | | - |
97 | | - public function getCurrentMigration() |
98 | | - { |
99 | | - return $this->data['Current Version']; |
100 | | - } |
101 | | - |
102 | | - public function getNextMigration() |
103 | | - { |
104 | | - return $this->data['Next Version']; |
105 | | - } |
| 33 | + $availableMigrations = $planCalculator->getMigrations(); |
| 34 | + $this->data['available_migrations'] = $this->flattenAvailableMigrations($availableMigrations); |
| 35 | + $this->data['executed_migrations'] = $this->flattenExecutedMigrations( |
| 36 | + $metadataStorage->getExecutedMigrations(), |
| 37 | + $availableMigrations |
| 38 | + ); |
| 39 | + $this->data['new_migrations'] = $this->flattenAvailableMigrations($statusCalculator->getNewMigrations()); |
| 40 | + $this->data['executed_unavailable_migrations'] = $this->flattenExecutedMigrations( |
| 41 | + $statusCalculator->getExecutedUnavailableMigrations(), |
| 42 | + new AvailableMigrationsList([]) |
| 43 | + ); |
106 | 44 |
|
107 | | - public function getLatestMigration() |
108 | | - { |
109 | | - return $this->data['Latest Version']; |
110 | | - } |
| 45 | + $this->data['storage'] = get_class($metadataStorage); |
| 46 | + $configuration = $this->dependencyFactory->getConfiguration(); |
| 47 | + $storage = $configuration->getMetadataStorageConfiguration(); |
| 48 | + if ($storage instanceof TableMetadataStorageConfiguration) { |
| 49 | + $this->data['table'] = $storage->getTableName(); |
| 50 | + $this->data['column'] = $storage->getVersionColumnName(); |
| 51 | + } |
111 | 52 |
|
112 | | - public function getExecutedMigrations() |
113 | | - { |
114 | | - return $this->data['Executed Migrations']; |
115 | | - } |
| 53 | + $connection = $this->dependencyFactory->getConnection(); |
| 54 | + $this->data['driver'] = get_class($connection->getDriver()); |
| 55 | + $this->data['name'] = $connection->getDatabase(); |
116 | 56 |
|
117 | | - public function getExecutedUnavailableMigrations() |
118 | | - { |
119 | | - return $this->data['Executed Unavailable Migrations']; |
| 57 | + $this->data['namespaces'] = $configuration->getMigrationDirectories(); |
120 | 58 | } |
121 | 59 |
|
122 | | - public function getAvailableMigrations() |
| 60 | + public function getName() |
123 | 61 | { |
124 | | - return $this->data['Available Migrations']; |
| 62 | + return 'migrations'; |
125 | 63 | } |
126 | 64 |
|
127 | | - public function getNewMigrations() |
| 65 | + public function getData() |
128 | 66 | { |
129 | | - return $this->data['New Migrations']; |
| 67 | + return $this->data; |
130 | 68 | } |
131 | 69 |
|
132 | | - public function getNewMigrationsList() |
| 70 | + public function reset() |
133 | 71 | { |
134 | | - return $this->data['newMigrationsList']; |
| 72 | + $this->data = []; |
135 | 73 | } |
136 | 74 |
|
137 | | - public function getExecutedUnavailableMigrationsList() |
138 | | - { |
139 | | - return $this->data['executedUnavailableMigrationsList']; |
| 75 | + private function flattenExecutedMigrations( |
| 76 | + ExecutedMigrationsList $executedMigrations, |
| 77 | + AvailableMigrationsList $availableMigrations |
| 78 | + ): array { |
| 79 | + return array_map(static function (ExecutedMigration $migration) use ($availableMigrations) { |
| 80 | + $version = $migration->getVersion(); |
| 81 | + return [ |
| 82 | + 'version' => (string)$version, |
| 83 | + 'executed_at' => $migration->getExecutedAt(), |
| 84 | + 'execution_time' => $migration->getExecutionTime(), |
| 85 | + 'description' => $availableMigrations->hasMigration($version) |
| 86 | + ? $availableMigrations->getMigration($version)->getMigration()->getDescription() : null, |
| 87 | + ]; |
| 88 | + }, $executedMigrations->getItems()); |
140 | 89 | } |
141 | 90 |
|
142 | | - public function getDatabaseDriver() |
143 | | - { |
144 | | - return $this->data['Database Driver']; |
145 | | - } |
146 | | - public function getDatabaseName() |
147 | | - { |
148 | | - return $this->data['Database Name']; |
149 | | - } |
150 | | - public function getConfigurationSource() |
151 | | - { |
152 | | - return $this->data['Configuration Source']; |
153 | | - } |
154 | | - public function getVersionTableName() |
155 | | - { |
156 | | - return $this->data['Version Table Name']; |
157 | | - } |
158 | | - public function getVersionColumnName() |
159 | | - { |
160 | | - return $this->data['Version Column Name']; |
161 | | - } |
162 | | - public function getMigrationNamespace() |
163 | | - { |
164 | | - return $this->data['Migrations Namespace']; |
165 | | - } |
166 | | - public function getMigrationDirectory() |
| 91 | + private function flattenAvailableMigrations(AvailableMigrationsList $migrationsList): array |
167 | 92 | { |
168 | | - return $this->data['Migrations Directory']; |
| 93 | + return array_map(static function (AvailableMigration $migration) { |
| 94 | + return [ |
| 95 | + 'version' => (string)$migration->getVersion(), |
| 96 | + 'description' => $migration->getMigration()->getDescription(), |
| 97 | + ]; |
| 98 | + }, $migrationsList->getItems()); |
169 | 99 | } |
170 | 100 | } |
0 commit comments