SampleDataRemoveCommand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SampleData\Console\Command;
  7. use Symfony\Component\Console\Command\Command;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Input\InputOption;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11. use Magento\SampleData\Model\Dependency;
  12. use Symfony\Component\Console\Input\ArrayInput;
  13. use Symfony\Component\Console\Input\ArrayInputFactory;
  14. use Magento\Framework\App\Filesystem\DirectoryList;
  15. use Magento\Framework\Filesystem;
  16. use Composer\Console\Application;
  17. use Composer\Console\ApplicationFactory;
  18. /**
  19. * Command for remove Sample Data packages
  20. */
  21. class SampleDataRemoveCommand extends Command
  22. {
  23. const OPTION_NO_UPDATE = 'no-update';
  24. /**
  25. * @var Filesystem
  26. */
  27. private $filesystem;
  28. /**
  29. * @var Dependency
  30. */
  31. private $sampleDataDependency;
  32. /**
  33. * @var ArrayInputFactory
  34. * @deprecated 100.1.0
  35. */
  36. private $arrayInputFactory;
  37. /**
  38. * @var ApplicationFactory
  39. */
  40. private $applicationFactory;
  41. /**
  42. * @param Filesystem $filesystem
  43. * @param Dependency $sampleDataDependency
  44. * @param ArrayInputFactory $arrayInputFactory
  45. * @param ApplicationFactory $applicationFactory
  46. */
  47. public function __construct(
  48. Filesystem $filesystem,
  49. Dependency $sampleDataDependency,
  50. ArrayInputFactory $arrayInputFactory,
  51. ApplicationFactory $applicationFactory
  52. ) {
  53. $this->filesystem = $filesystem;
  54. $this->sampleDataDependency = $sampleDataDependency;
  55. $this->arrayInputFactory = $arrayInputFactory;
  56. $this->applicationFactory = $applicationFactory;
  57. parent::__construct();
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. protected function configure()
  63. {
  64. $this->setName('sampledata:remove')
  65. ->setDescription('Remove all sample data packages from composer.json');
  66. $this->addOption(
  67. self::OPTION_NO_UPDATE,
  68. null,
  69. InputOption::VALUE_NONE,
  70. 'Update composer.json without executing composer update'
  71. );
  72. parent::configure();
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. protected function execute(InputInterface $input, OutputInterface $output)
  78. {
  79. $sampleDataPackages = $this->sampleDataDependency->getSampleDataPackages();
  80. if (!empty($sampleDataPackages)) {
  81. $baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
  82. $commonArgs = ['--working-dir' => $baseDir, '--no-interaction' => 1, '--no-progress' => 1];
  83. if ($input->getOption(self::OPTION_NO_UPDATE)) {
  84. $commonArgs['--no-update'] = 1;
  85. }
  86. $packages = array_keys($sampleDataPackages);
  87. $arguments = array_merge(['command' => 'remove', 'packages' => $packages], $commonArgs);
  88. $commandInput = new ArrayInput($arguments);
  89. /** @var Application $application */
  90. $application = $this->applicationFactory->create();
  91. $application->setAutoExit(false);
  92. $result = $application->run($commandInput, $output);
  93. if ($result !== 0) {
  94. $output->writeln('<info>' . 'There is an error during remove sample data.' . '</info>');
  95. }
  96. } else {
  97. $output->writeln('<info>' . 'There is no sample data for current set of modules.' . '</info>');
  98. }
  99. }
  100. }