vendor/easycorp/easyadmin-bundle/src/Command/MakeAdminMigrationCommand.php line 39

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Command;
  3. use EasyCorp\Bundle\EasyAdminBundle\Maker\Migrator;
  4. use Symfony\Component\Console\Command\Command;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\ConsoleSectionOutput;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. use Symfony\Component\Console\Question\Question;
  10. use Symfony\Component\Console\Style\SymfonyStyle;
  11. use Symfony\Component\Console\Terminal;
  12. use Symfony\Component\Filesystem\Filesystem;
  13. /**
  14. * A command to transform EasyAdmin 2 YAML configuration into the PHP files required by EasyAdmin 3.
  15. *
  16. * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  17. */
  18. class MakeAdminMigrationCommand extends Command
  19. {
  20. protected static $defaultName = 'make:admin:migration';
  21. protected static $defaultDescription = 'Migrates EasyAdmin2 YAML config into EasyAdmin 3 PHP config classes';
  22. public const SUCCESS = 0;
  23. public const FAILURE = 1;
  24. private $migrator;
  25. private $projectDir;
  26. /** @var InputInterface */
  27. private $input;
  28. /** @var ConsoleSectionOutput */
  29. private $progressSection;
  30. private $progressSectionLines = [];
  31. /** @var ConsoleSectionOutput */
  32. private $temporarySection;
  33. public function __construct(Migrator $migrator, string $projectDir, string $name = null)
  34. {
  35. parent::__construct($name);
  36. $this->migrator = $migrator;
  37. $this->projectDir = $projectDir;
  38. }
  39. public function configure()
  40. {
  41. $this
  42. ->setDescription(self::$defaultDescription)
  43. ->setHelp($this->getCommandHelp())
  44. ->addArgument('ea2-backup-file', InputArgument::OPTIONAL, 'The path to the EasyAdmin 2 backup file you want to migrate from.')
  45. ;
  46. }
  47. /**
  48. * @return int
  49. */
  50. protected function execute(InputInterface $input, OutputInterface $output)
  51. {
  52. $io = new SymfonyStyle($input, $output);
  53. $fs = new Filesystem();
  54. $this->input = $input;
  55. $this->progressSection = $output->section();
  56. $this->temporarySection = $output->section();
  57. $this->clearScreen($output);
  58. $io->title('Migration from EasyAdmin2 to EasyAdmin 3');
  59. $this->addStep('<info>Step 1/3.</info> Find the file with the EasyAdmin 2 config backup.');
  60. $ea2ConfigBackupPath = $input->getArgument('ea2-backup-file') ?: $this->projectDir.'/easyadmin-config.backup';
  61. if (!$fs->exists($ea2ConfigBackupPath)) {
  62. $this->temporarySection->write(sprintf(
  63. '<error> ERROR </error> The config backup file was not found in %s. To generate this file, run the <comment>make:admin:migration</comment> command in your application BEFORE upgrading to EasyAdmin 3 (the command must be run while still using EasyAdmin 2).',
  64. $ea2ConfigBackupPath
  65. ));
  66. }
  67. while (!$fs->exists($ea2ConfigBackupPath)) {
  68. $ea2ConfigBackupPath = $this->askQuestion('Absolute path of <comment>easyadmin-config.backup</comment> file:');
  69. }
  70. $this->temporarySection->write(sprintf('<bg=green;fg=black> OK </> The backup file was found at "%s"', $ea2ConfigBackupPath));
  71. $ea2Config = unserialize(file_get_contents($ea2ConfigBackupPath), ['allowed_classes' => false]);
  72. $this->askQuestion('Press <comment>\<Enter></comment> to continue...');
  73. $this->addStep(sprintf('<bg=green;fg=black> OK </> The backup file was found at "%s"', $ea2ConfigBackupPath));
  74. $this->temporarySection->clear();
  75. $this->addStep('');
  76. $this->addStep('<info>Step 2/3.</info> Select the directory where the new PHP files will be generated.');
  77. $this->temporarySection->write(sprintf('Type the relative path from your project directory, which is: %s', $this->projectDir));
  78. $relativeOutputDir = $this->askQuestion('Directory [<comment>src/Controller/Admin/</comment>]:', 'src/Controller/Admin');
  79. $outputDir = $this->projectDir.'/'.ltrim($relativeOutputDir, '/');
  80. $fs->mkdir($outputDir);
  81. if (!$fs->exists($outputDir)) {
  82. $this->temporarySection->clear();
  83. $this->temporarySection->write(sprintf('<error> ERROR </error> The "%s" directory does not exist and cannot be created, so the PHP files cannot be generated.', $outputDir));
  84. return self::FAILURE;
  85. }
  86. $this->addStep(sprintf('<bg=green;fg=black> OK </> Output dir = "%s"', $outputDir));
  87. $this->temporarySection->clear();
  88. $this->addStep('');
  89. $this->addStep('<info>Step 3/3.</info> Define the namespace of the new PHP files that will be generated.');
  90. $namespace = $this->askQuestion('Namespace [<comment>App\\Controller\\Admin</comment>]:', 'App\\Controller\\Admin');
  91. $namespace = str_replace('/', '\\', $namespace);
  92. $this->addStep(sprintf('<bg=green;fg=black> OK </> Namespace = "%s"', $namespace));
  93. $this->temporarySection->clear();
  94. $this->migrator->migrate($ea2Config, $outputDir, $namespace, $this->temporarySection);
  95. $this->temporarySection->write('');
  96. $io->success(sprintf('The migration completed successfully. You can find the generated files at "%s".', $relativeOutputDir));
  97. return self::SUCCESS;
  98. }
  99. private function clearScreen(OutputInterface $output): void
  100. {
  101. // clears the entire screen
  102. $output->write("\x1b[2J");
  103. // moves cursor to top left position
  104. $output->write("\x1b[1;1H");
  105. }
  106. private function addStep(string $newLine): void
  107. {
  108. $this->progressSectionLines[] = $newLine;
  109. $this->progressSection->clear();
  110. $terminal = new Terminal();
  111. foreach ($this->progressSectionLines as $line) {
  112. $this->progressSection->write($line);
  113. }
  114. $this->progressSection->write('');
  115. $this->progressSection->write(str_repeat('━', $terminal->getWidth()));
  116. $this->progressSection->write('');
  117. }
  118. private function askQuestion(string $questionText, $defaultAnswer = null)
  119. {
  120. $helper = $this->getHelper('question');
  121. $question = new Question($questionText, $defaultAnswer);
  122. return $helper->ask($this->input, $this->temporarySection, $question);
  123. }
  124. private function getCommandHelp()
  125. {
  126. return <<<'HELP'
  127. The <info>%command.name%</info> command migrates the YAML-based configuration of
  128. an EasyAdmin 2 application into the PHP config classes used by EasyAdmin 3.
  129. Follow the steps shown by the command to select the YAML configuration file
  130. and the location and namespace of the newly generated classes.
  131. If you prefer, you can pass the path to the EasyAdmin 2 YAML config backup file
  132. as an argument:
  133. <info>php %command.full_name% /path/to/some/easyadmin-config.backup</info>
  134. HELP
  135. ;
  136. }
  137. }