vendor/hautelook/alice-bundle/src/Console/Command/Doctrine/DoctrineOrmLoadDataFixturesCommand.php line 91

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Hautelook\AliceBundle package.
  4. *
  5. * (c) Baldur Rensch <brensch@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Hautelook\AliceBundle\Console\Command\Doctrine;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use DomainException;
  14. use const E_USER_DEPRECATED;
  15. use Hautelook\AliceBundle\LoaderInterface as AliceBundleLoaderInterface;
  16. use InvalidArgumentException;
  17. use function sprintf;
  18. use Symfony\Bundle\FrameworkBundle\Console\Application as FrameworkBundleConsoleApplication;
  19. use Symfony\Component\Console\Application as ConsoleApplication;
  20. use Symfony\Component\Console\Attribute\AsCommand;
  21. use Symfony\Component\Console\Command\Command;
  22. use Symfony\Component\Console\Helper\QuestionHelper;
  23. use Symfony\Component\Console\Input\InputInterface;
  24. use Symfony\Component\Console\Input\InputOption;
  25. use Symfony\Component\Console\Output\OutputInterface;
  26. use Symfony\Component\Console\Question\ConfirmationQuestion;
  27. use function trigger_error;
  28. /**
  29. * Command used to load the fixtures.
  30. */
  31. #[AsCommand(name: 'hautelook:fixtures:load', description: 'Load data fixtures to your database.')]
  32. class DoctrineOrmLoadDataFixturesCommand extends Command
  33. {
  34. private ManagerRegistry $doctrine;
  35. private AliceBundleLoaderInterface $loader;
  36. public function __construct(
  37. string $name,
  38. ManagerRegistry $managerRegistry,
  39. AliceBundleLoaderInterface $loader
  40. ) {
  41. parent::__construct($name);
  42. $this->doctrine = $managerRegistry;
  43. $this->loader = $loader;
  44. }
  45. protected function configure(): void
  46. {
  47. $this
  48. ->setDescription('Load data fixtures to your database.')
  49. ->addOption(
  50. 'bundle',
  51. 'b',
  52. InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
  53. 'Bundles where fixtures should be loaded.'
  54. )
  55. ->addOption(
  56. 'no-bundles',
  57. null,
  58. InputOption::VALUE_NONE,
  59. 'Fixtures from bundles will not be loaded.'
  60. )
  61. ->addOption(
  62. 'manager',
  63. 'm',
  64. InputOption::VALUE_REQUIRED,
  65. 'The entity manager to use for this command. If not specified, use the default Doctrine fixtures entity'
  66. .'manager.'
  67. )
  68. ->addOption(
  69. 'append',
  70. null,
  71. InputOption::VALUE_NONE,
  72. 'Append the data fixtures instead of deleting all data from the database first.'
  73. )
  74. ->addOption(
  75. 'purge-with-truncate',
  76. null,
  77. InputOption::VALUE_NONE,
  78. 'Purge data by using a database-level TRUNCATE statement when using Doctrine fixtures.'
  79. )
  80. ;
  81. }
  82. public function setApplication(ConsoleApplication $application = null): void
  83. {
  84. if (null !== $application
  85. && !($application instanceof FrameworkBundleConsoleApplication)
  86. ) {
  87. throw new InvalidArgumentException(
  88. sprintf(
  89. 'Expected application to be an instance of "%s".',
  90. FrameworkBundleConsoleApplication::class,
  91. ),
  92. );
  93. }
  94. parent::setApplication($application);
  95. }
  96. protected function execute(InputInterface $input, OutputInterface $output): int
  97. {
  98. // Warn the user that the database will be purged
  99. // Ask him to confirm his choice
  100. if (
  101. $input->isInteractive()
  102. && !$input->getOption('append')
  103. && !$this->askConfirmation(
  104. $input,
  105. $output,
  106. '<question>Careful, database will be purged. Do you want to continue y/N ?</question>',
  107. false
  108. )
  109. ) {
  110. return self::SUCCESS;
  111. }
  112. $noBundles = $input->getOption('no-bundles') ?? false;
  113. if (!$noBundles) {
  114. @trigger_error(
  115. 'The configuration parameter hautelook_alice.root_dirs should be used to specify the directories to include. If done or if you do not need to load bundle\'s fixtures, use the --no-bundles option',
  116. E_USER_DEPRECATED
  117. );
  118. }
  119. $bundles = $input->getOption('bundle');
  120. if ($bundles) {
  121. @trigger_error(
  122. 'The option --bundle is deprecated. Use the configuration parameter hautelook_alice.root_dirs to include the desired directories instead',
  123. E_USER_DEPRECATED
  124. );
  125. }
  126. if ($bundles && $noBundles) {
  127. throw new DomainException('--bundle and --no-bundles flags cannot be specified both in same time.');
  128. }
  129. $manager = $this->doctrine->getManager($input->getOption('manager'));
  130. $environment = $input->getOption('env');
  131. $append = $input->getOption('append');
  132. $truncate = $input->getOption('purge-with-truncate');
  133. /** @var FrameworkBundleConsoleApplication $application */
  134. $application = $this->getApplication();
  135. $this->loader->load($application, $manager, $bundles, $environment, $append, $truncate, $noBundles);
  136. return 0;
  137. }
  138. /**
  139. * Prompts to the user a message to ask him a confirmation.
  140. *
  141. *
  142. * @return bool User choice
  143. */
  144. private function askConfirmation(
  145. InputInterface $input,
  146. OutputInterface $output,
  147. string $question,
  148. bool $default
  149. ): bool {
  150. /** @var QuestionHelper $questionHelper */
  151. $questionHelper = $this->getHelperSet()->get('question');
  152. $question = new ConfirmationQuestion($question, $default);
  153. return (bool) $questionHelper->ask($input, $output, $question);
  154. }
  155. }