IndexerShowDimensionsModeCommand.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Indexer\Console\Command;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Magento\Framework\App\ObjectManagerFactory;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Magento\Framework\Console\Cli;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. /**
  15. * Command to show indexers dimension modes
  16. */
  17. class IndexerShowDimensionsModeCommand extends AbstractIndexerCommand
  18. {
  19. const INPUT_KEY_INDEXER = 'indexer';
  20. const DIMENSION_MODE_NONE = 'none';
  21. const XML_PATH_DIMENSIONS_MODE_MASK = 'indexer/%s/dimensions_mode';
  22. /**
  23. * @var string
  24. */
  25. private $commandName = 'indexer:show-dimensions-mode';
  26. /**
  27. * ScopeConfigInterface
  28. *
  29. * @var ScopeConfigInterface
  30. */
  31. private $configReader;
  32. /**
  33. * @var string[]
  34. */
  35. private $indexers;
  36. /**
  37. * @param ObjectManagerFactory $objectManagerFactory
  38. * @param ScopeConfigInterface $configReader
  39. * @param array $indexers
  40. */
  41. public function __construct(
  42. ObjectManagerFactory $objectManagerFactory,
  43. ScopeConfigInterface $configReader,
  44. array $indexers
  45. ) {
  46. $this->configReader = $configReader;
  47. $this->indexers = $indexers;
  48. parent::__construct($objectManagerFactory);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function configure()
  54. {
  55. $this->setName($this->commandName)
  56. ->setDescription('Shows Indexer Dimension Mode')
  57. ->setDefinition($this->getInputList());
  58. parent::configure();
  59. }
  60. /**
  61. * {@inheritdoc}
  62. * @param InputInterface $input
  63. * @param OutputInterface $output
  64. * @return int
  65. */
  66. protected function execute(InputInterface $input, OutputInterface $output)
  67. {
  68. $errors = $this->validate($input);
  69. if ($errors) {
  70. throw new \InvalidArgumentException(implode(PHP_EOL, $errors));
  71. }
  72. $returnValue = Cli::RETURN_SUCCESS;
  73. /** @var \Magento\Indexer\Model\Indexer $indexer */
  74. $indexer = $this->getObjectManager()->get(\Magento\Indexer\Model\Indexer::class);
  75. try {
  76. $selectedIndexers = $input->getArgument(self::INPUT_KEY_INDEXER);
  77. if ($selectedIndexers) {
  78. $indexersList = (array)$selectedIndexers;
  79. } else {
  80. $indexersList = $this->indexers;
  81. }
  82. foreach ($indexersList as $indexerId) {
  83. $indexer->load($indexerId);
  84. $configPath = sprintf(self::XML_PATH_DIMENSIONS_MODE_MASK, $indexerId);
  85. $mode = $this->configReader->getValue($configPath) ?: self::DIMENSION_MODE_NONE;
  86. $output->writeln(sprintf('%-50s ', $indexer->getTitle() . ':') . $mode);
  87. }
  88. } catch (\Exception $e) {
  89. $output->writeln('"' . $indexer->getTitle() . '" indexer process unknown error:' . PHP_EOL);
  90. $output->writeln($e->getMessage() . PHP_EOL);
  91. // we must have an exit code higher than zero to indicate something was wrong
  92. $returnValue = Cli::RETURN_FAILURE;
  93. }
  94. return $returnValue;
  95. }
  96. /**
  97. * Get list of arguments for the command
  98. *
  99. * @return InputArgument[]
  100. */
  101. private function getInputList(): array
  102. {
  103. $optionDescription = 'Space-separated list of index types or omit to apply to all indexes';
  104. $arguments[] = new InputArgument(
  105. self::INPUT_KEY_INDEXER,
  106. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  107. $optionDescription . ' (' . implode($this->indexers) . ')'
  108. );
  109. return $arguments;
  110. }
  111. /**
  112. * Check if all arguments are provided
  113. *
  114. * @param InputInterface $input
  115. * @return string[]
  116. */
  117. private function validate(InputInterface $input): array
  118. {
  119. $inputIndexer = (array)$input->getArgument(self::INPUT_KEY_INDEXER);
  120. $acceptedValues = array_keys($this->indexers);
  121. $errors = $this->validateArgument(self::INPUT_KEY_INDEXER, $inputIndexer, $acceptedValues);
  122. return $errors;
  123. }
  124. /**
  125. * Validate command argument and return errors in case if argument is invalid
  126. *
  127. * @param string $inputKey
  128. * @param array $inputIndexer
  129. * @param array $acceptedValues
  130. * @return array
  131. */
  132. private function validateArgument(string $inputKey, array $inputIndexer, array $acceptedValues): array
  133. {
  134. $errors = [];
  135. $acceptedIndexerValues = ' Accepted values for "<' . $inputKey . '>" are \'' .
  136. implode(',', $acceptedValues) . '\'';
  137. if (!empty($inputIndexer) && !\array_intersect($inputIndexer, $acceptedValues)) {
  138. $errors[] = 'Invalid value for "<' . $inputKey . '>" argument.' . $acceptedIndexerValues;
  139. }
  140. return $errors;
  141. }
  142. }