vendor/doctrine/doctrine-migrations-bundle/src/Collector/MigrationsCollector.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Bundle\MigrationsBundle\Collector;
  4. use Doctrine\DBAL\Exception;
  5. use Doctrine\Migrations\DependencyFactory;
  6. use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  10. use Symfony\Component\VarDumper\Cloner\Data;
  11. use Throwable;
  12. use function count;
  13. use function get_class;
  14. /** @final */
  15. class MigrationsCollector extends DataCollector
  16. {
  17. /** @var DependencyFactory */
  18. private $dependencyFactory;
  19. /** @var MigrationsFlattener */
  20. private $flattener;
  21. public function __construct(DependencyFactory $dependencyFactory, MigrationsFlattener $migrationsFlattener)
  22. {
  23. $this->dependencyFactory = $dependencyFactory;
  24. $this->flattener = $migrationsFlattener;
  25. }
  26. /** @return void */
  27. public function collect(Request $request, Response $response, ?Throwable $exception = null)
  28. {
  29. if ($this->data !== []) {
  30. return;
  31. }
  32. $metadataStorage = $this->dependencyFactory->getMetadataStorage();
  33. $planCalculator = $this->dependencyFactory->getMigrationPlanCalculator();
  34. try {
  35. $executedMigrations = $metadataStorage->getExecutedMigrations();
  36. } catch (Exception $dbalException) {
  37. $this->dependencyFactory->getLogger()->error(
  38. 'error while trying to collect executed migrations',
  39. ['exception' => $dbalException]
  40. );
  41. return;
  42. }
  43. $availableMigrations = $planCalculator->getMigrations();
  44. $this->data['available_migrations_count'] = count($availableMigrations);
  45. $unavailableMigrations = $executedMigrations->unavailableSubset($availableMigrations);
  46. $this->data['unavailable_migrations_count'] = count($unavailableMigrations);
  47. $newMigrations = $availableMigrations->newSubset($executedMigrations);
  48. $this->data['new_migrations'] = $this->flattener->flattenAvailableMigrations($newMigrations);
  49. $this->data['executed_migrations'] = $this->flattener->flattenExecutedMigrations($executedMigrations, $availableMigrations);
  50. $this->data['storage'] = get_class($metadataStorage);
  51. $configuration = $this->dependencyFactory->getConfiguration();
  52. $storage = $configuration->getMetadataStorageConfiguration();
  53. if ($storage instanceof TableMetadataStorageConfiguration) {
  54. $this->data['table'] = $storage->getTableName();
  55. $this->data['column'] = $storage->getVersionColumnName();
  56. }
  57. $connection = $this->dependencyFactory->getConnection();
  58. $this->data['driver'] = get_class($connection->getDriver());
  59. $this->data['name'] = $connection->getDatabase();
  60. $this->data['namespaces'] = $configuration->getMigrationDirectories();
  61. }
  62. /** @return string */
  63. public function getName()
  64. {
  65. return 'doctrine_migrations';
  66. }
  67. /** @return array<string, mixed>|Data */
  68. public function getData()
  69. {
  70. return $this->data;
  71. }
  72. /** @return void */
  73. public function reset()
  74. {
  75. $this->data = [];
  76. }
  77. }