Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SampleDataRemoveCommand.php
Go to the documentation of this file.
1 <?php
8 
9 use Symfony\Component\Console\Command\Command;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Input\ArrayInput;
15 use Symfony\Component\Console\Input\ArrayInputFactory;
18 use Composer\Console\Application;
19 use Composer\Console\ApplicationFactory;
20 
24 class SampleDataRemoveCommand extends Command
25 {
26  const OPTION_NO_UPDATE = 'no-update';
27 
31  private $filesystem;
32 
36  private $sampleDataDependency;
37 
42  private $arrayInputFactory;
43 
47  private $applicationFactory;
48 
55  public function __construct(
56  Filesystem $filesystem,
57  Dependency $sampleDataDependency,
58  ArrayInputFactory $arrayInputFactory,
59  ApplicationFactory $applicationFactory
60  ) {
61  $this->filesystem = $filesystem;
62  $this->sampleDataDependency = $sampleDataDependency;
63  $this->arrayInputFactory = $arrayInputFactory;
64  $this->applicationFactory = $applicationFactory;
65  parent::__construct();
66  }
67 
71  protected function configure()
72  {
73  $this->setName('sampledata:remove')
74  ->setDescription('Remove all sample data packages from composer.json');
75  $this->addOption(
76  self::OPTION_NO_UPDATE,
77  null,
78  InputOption::VALUE_NONE,
79  'Update composer.json without executing composer update'
80  );
81  parent::configure();
82  }
83 
87  protected function execute(InputInterface $input, OutputInterface $output)
88  {
89  $sampleDataPackages = $this->sampleDataDependency->getSampleDataPackages();
90  if (!empty($sampleDataPackages)) {
91  $baseDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath();
92  $commonArgs = ['--working-dir' => $baseDir, '--no-interaction' => 1, '--no-progress' => 1];
93  if ($input->getOption(self::OPTION_NO_UPDATE)) {
94  $commonArgs['--no-update'] = 1;
95  }
96  $packages = array_keys($sampleDataPackages);
97  $arguments = array_merge(['command' => 'remove', 'packages' => $packages], $commonArgs);
98  $commandInput = new ArrayInput($arguments);
99 
101  $application = $this->applicationFactory->create();
102  $application->setAutoExit(false);
103  $result = $application->run($commandInput, $output);
104  if ($result !== 0) {
105  $output->writeln('<info>' . 'There is an error during remove sample data.' . '</info>');
106  }
107  } else {
108  $output->writeln('<info>' . 'There is no sample data for current set of modules.' . '</info>');
109  }
110  }
111 }
$baseDir
Definition: autoload.php:9
$application
Definition: bootstrap.php:58
$arguments
$filesystem
__construct(Filesystem $filesystem, Dependency $sampleDataDependency, ArrayInputFactory $arrayInputFactory, ApplicationFactory $applicationFactory)