Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexerSetModeCommand.php
Go to the documentation of this file.
1 <?php
7 
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Output\OutputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputArgument;
13 
18 {
22  const INPUT_KEY_MODE = 'mode';
23  const INPUT_KEY_REALTIME = 'realtime';
24  const INPUT_KEY_SCHEDULE = 'schedule';
30  protected function configure()
31  {
32  $this->setName('indexer:set-mode')
33  ->setDescription('Sets index mode type')
34  ->setDefinition($this->getInputList());
35 
36  parent::configure();
37  }
38 
42  protected function execute(InputInterface $input, OutputInterface $output)
43  {
44  $errors = $this->validate($input);
45  if ($errors) {
46  throw new \InvalidArgumentException(implode("\n", $errors));
47  }
48 
49  $indexers = $this->getIndexers($input);
50 
52  foreach ($indexers as $indexer) {
53  try {
54  $previousStatus = $indexer->isScheduled() ? 'Update by Schedule' : 'Update on Save';
55  $indexer->setScheduled($input->getArgument(self::INPUT_KEY_MODE) === self::INPUT_KEY_SCHEDULE);
56  $currentStatus = $indexer->isScheduled() ? 'Update by Schedule' : 'Update on Save';
57  if ($previousStatus !== $currentStatus) {
58  $output->writeln(
59  'Index mode for Indexer ' . $indexer->getTitle() . ' was changed from \''
60  . $previousStatus . '\' to \'' . $currentStatus . '\''
61  );
62  } else {
63  $output->writeln('Index mode for Indexer ' . $indexer->getTitle() . ' has not been changed');
64  }
65  } catch (LocalizedException $e) {
66  $output->writeln($e->getMessage() . PHP_EOL);
67  // we must have an exit code higher than zero to indicate something was wrong
69  } catch (\Exception $e) {
70  $output->writeln($indexer->getTitle() . " indexer process unknown error:" . PHP_EOL);
71  $output->writeln($e->getMessage() . PHP_EOL);
72  // we must have an exit code higher than zero to indicate something was wrong
74  }
75  }
76 
77  return $returnValue;
78  }
79 
85  public function getInputList()
86  {
87  $modeOptions[] = new InputArgument(
88  self::INPUT_KEY_MODE,
89  InputArgument::OPTIONAL,
90  'Indexer mode type ['. self::INPUT_KEY_REALTIME . '|' . self::INPUT_KEY_SCHEDULE .']'
91  );
92  $optionsList = array_merge($modeOptions, parent::getInputList());
93  return $optionsList;
94  }
95 
102  public function validate(InputInterface $input)
103  {
104  $errors = [];
105  $acceptedValues = ' Accepted values for ' . self::INPUT_KEY_MODE . ' are \''
106  . self::INPUT_KEY_REALTIME . '\' or \'' . self::INPUT_KEY_SCHEDULE . '\'';
107 
108  $inputMode = $input->getArgument(self::INPUT_KEY_MODE);
109  if (!$inputMode) {
110  $errors[] = 'Missing argument \'' . self::INPUT_KEY_MODE .'\'.' . $acceptedValues;
111  } elseif (!in_array($inputMode, [self::INPUT_KEY_REALTIME, self::INPUT_KEY_SCHEDULE])) {
112  $errors[] = $acceptedValues;
113  }
114  return $errors;
115  }
116 }
execute(InputInterface $input, OutputInterface $output)
$errors
Definition: overview.phtml:9