AbstractIndexerManageCommand.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Indexer\Console\Command;
  7. use Magento\Framework\Indexer\IndexerInterface;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Input\InputArgument;
  10. /**
  11. * An Abstract class for all Indexer related commands.
  12. */
  13. abstract class AbstractIndexerManageCommand extends AbstractIndexerCommand
  14. {
  15. /**
  16. * Indexer name option
  17. */
  18. const INPUT_KEY_INDEXERS = 'index';
  19. /**
  20. * Returns the ordered list of indexers.
  21. *
  22. * @param InputInterface $input
  23. * @return IndexerInterface[]
  24. * @throws \InvalidArgumentException
  25. */
  26. protected function getIndexers(InputInterface $input)
  27. {
  28. $requestedTypes = [];
  29. if ($input->getArgument(self::INPUT_KEY_INDEXERS)) {
  30. $requestedTypes = $input->getArgument(self::INPUT_KEY_INDEXERS);
  31. $requestedTypes = array_filter(array_map('trim', $requestedTypes), 'strlen');
  32. }
  33. if (empty($requestedTypes)) {
  34. $indexers = $this->getAllIndexers();
  35. } else {
  36. $availableIndexers = $this->getAllIndexers();
  37. $unsupportedTypes = array_diff($requestedTypes, array_keys($availableIndexers));
  38. if ($unsupportedTypes) {
  39. throw new \InvalidArgumentException(
  40. "The following requested index types are not supported: '" . join("', '", $unsupportedTypes)
  41. . "'." . PHP_EOL . 'Supported types: ' . join(", ", array_keys($availableIndexers))
  42. );
  43. }
  44. $indexers = array_intersect_key($availableIndexers, array_flip($requestedTypes));
  45. }
  46. return $indexers;
  47. }
  48. /**
  49. * Get list of options and arguments for the command
  50. *
  51. * @return mixed
  52. */
  53. public function getInputList()
  54. {
  55. return [
  56. new InputArgument(
  57. self::INPUT_KEY_INDEXERS,
  58. InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
  59. 'Space-separated list of index types or omit to apply to all indexes.'
  60. ),
  61. ];
  62. }
  63. }