MigrationsFlattener.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Bundle\MigrationsBundle\Collector;
  4. use Doctrine\Migrations\Metadata\AvailableMigration;
  5. use Doctrine\Migrations\Metadata\AvailableMigrationsList;
  6. use Doctrine\Migrations\Metadata\ExecutedMigration;
  7. use Doctrine\Migrations\Metadata\ExecutedMigrationsList;
  8. class MigrationsFlattener
  9. {
  10. public function flattenAvailableMigrations(AvailableMigrationsList $migrationsList, ?ExecutedMigrationsList $executedMigrations = null): array
  11. {
  12. return array_map(static function (AvailableMigration $migration) use ($executedMigrations) {
  13. $executedMigration = $executedMigrations && $executedMigrations->hasMigration($migration->getVersion())
  14. ? $executedMigrations->getMigration($migration->getVersion())
  15. : null;
  16. return [
  17. 'version' => (string)$migration->getVersion(),
  18. 'is_new' => !$executedMigration,
  19. 'is_unavailable' => false,
  20. 'description' => $migration->getMigration()->getDescription(),
  21. 'executed_at' => $executedMigration ? $executedMigration->getExecutedAt() : null,
  22. 'execution_time' => $executedMigration ? $executedMigration->getExecutionTime() : null,
  23. 'file' => (new \ReflectionClass($migration->getMigration()))->getFileName(),
  24. ];
  25. }, $migrationsList->getItems());
  26. }
  27. public function flattenExecutedMigrations(ExecutedMigrationsList $migrationsList, ?AvailableMigrationsList $availableMigrations = null): array
  28. {
  29. return array_map(static function (ExecutedMigration $migration) use ($availableMigrations) {
  30. $availableMigration = $availableMigrations && $availableMigrations->hasMigration($migration->getVersion())
  31. ? $availableMigrations->getMigration($migration->getVersion())->getMigration()
  32. : null;
  33. return [
  34. 'version' => (string)$migration->getVersion(),
  35. 'is_new' => false,
  36. 'is_unavailable' => !$availableMigration,
  37. 'description' => $availableMigration ? $availableMigration->getDescription() : null,
  38. 'executed_at' => $migration->getExecutedAt(),
  39. 'execution_time' => $migration->getExecutionTime(),
  40. 'file' => $availableMigration ? (new \ReflectionClass($availableMigration))->getFileName() : null,
  41. ];
  42. }, $migrationsList->getItems());
  43. }
  44. }