Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexerShowDimensionsModeCommand.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
15 use Symfony\Component\Console\Input\InputArgument;
16 
21 {
22  const INPUT_KEY_INDEXER = 'indexer';
23  const DIMENSION_MODE_NONE = 'none';
24  const XML_PATH_DIMENSIONS_MODE_MASK = 'indexer/%s/dimensions_mode';
28  private $commandName = 'indexer:show-dimensions-mode';
34  private $configReader;
38  private $indexers;
39 
45  public function __construct(
46  ObjectManagerFactory $objectManagerFactory,
47  ScopeConfigInterface $configReader,
48  array $indexers
49  ) {
50  $this->configReader = $configReader;
51  $this->indexers = $indexers;
52  parent::__construct($objectManagerFactory);
53  }
54 
58  protected function configure()
59  {
60  $this->setName($this->commandName)
61  ->setDescription('Shows Indexer Dimension Mode')
62  ->setDefinition($this->getInputList());
63  parent::configure();
64  }
65 
72  protected function execute(InputInterface $input, OutputInterface $output)
73  {
74  $errors = $this->validate($input);
75  if ($errors) {
76  throw new \InvalidArgumentException(implode(PHP_EOL, $errors));
77  }
78  $returnValue = Cli::RETURN_SUCCESS;
80  $indexer = $this->getObjectManager()->get(\Magento\Indexer\Model\Indexer::class);
81  try {
82  $selectedIndexers = $input->getArgument(self::INPUT_KEY_INDEXER);
83  if ($selectedIndexers) {
84  $indexersList = (array)$selectedIndexers;
85  } else {
86  $indexersList = $this->indexers;
87  }
88  foreach ($indexersList as $indexerId) {
89  $indexer->load($indexerId);
90  $configPath = sprintf(self::XML_PATH_DIMENSIONS_MODE_MASK, $indexerId);
91  $mode = $this->configReader->getValue($configPath) ?: self::DIMENSION_MODE_NONE;
92  $output->writeln(sprintf('%-50s ', $indexer->getTitle() . ':') . $mode);
93  }
94  } catch (\Exception $e) {
95  $output->writeln('"' . $indexer->getTitle() . '" indexer process unknown error:' . PHP_EOL);
96  $output->writeln($e->getMessage() . PHP_EOL);
97  // we must have an exit code higher than zero to indicate something was wrong
98  $returnValue = Cli::RETURN_FAILURE;
99  }
100 
101  return $returnValue;
102  }
103 
109  private function getInputList(): array
110  {
111  $optionDescription = 'Space-separated list of index types or omit to apply to all indexes';
112  $arguments[] = new InputArgument(
113  self::INPUT_KEY_INDEXER,
114  InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
115  $optionDescription . ' (' . implode($this->indexers) . ')'
116  );
117 
118  return $arguments;
119  }
120 
127  private function validate(InputInterface $input): array
128  {
129  $inputIndexer = (array)$input->getArgument(self::INPUT_KEY_INDEXER);
130  $acceptedValues = array_keys($this->indexers);
131  $errors = $this->validateArgument(self::INPUT_KEY_INDEXER, $inputIndexer, $acceptedValues);
132 
133  return $errors;
134  }
135 
144  private function validateArgument(string $inputKey, array $inputIndexer, array $acceptedValues): array
145  {
146  $errors = [];
147  $acceptedIndexerValues = ' Accepted values for "<' . $inputKey . '>" are \'' .
148  implode(',', $acceptedValues) . '\'';
149  if (!empty($inputIndexer) && !\array_intersect($inputIndexer, $acceptedValues)) {
150  $errors[] = 'Invalid value for "<' . $inputKey . '>" argument.' . $acceptedIndexerValues;
151  }
152 
153  return $errors;
154  }
155 }
__construct(ObjectManagerFactory $objectManagerFactory, ScopeConfigInterface $configReader, array $indexers)
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
$objectManagerFactory
$arguments
$errors
Definition: overview.phtml:9