AbstractCacheManageCommand.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Console\Command;
  7. use Symfony\Component\Console\Input\InputArgument;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. /**
  10. * @api
  11. * @since 100.0.2
  12. */
  13. abstract class AbstractCacheManageCommand extends AbstractCacheCommand
  14. {
  15. /**
  16. * Input argument types
  17. */
  18. const INPUT_KEY_TYPES = 'types';
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function configure()
  23. {
  24. $this->addArgument(
  25. self::INPUT_KEY_TYPES,
  26. InputArgument::IS_ARRAY,
  27. 'Space-separated list of cache types or omit to apply to all cache types.'
  28. );
  29. parent::configure();
  30. }
  31. /**
  32. * Get requested cache types
  33. *
  34. * @param InputInterface $input
  35. * @return array
  36. */
  37. protected function getRequestedTypes(InputInterface $input)
  38. {
  39. $requestedTypes = [];
  40. if ($input->getArgument(self::INPUT_KEY_TYPES)) {
  41. $requestedTypes = $input->getArgument(self::INPUT_KEY_TYPES);
  42. $requestedTypes = array_filter(array_map('trim', $requestedTypes), 'strlen');
  43. }
  44. if (empty($requestedTypes)) {
  45. return $this->cacheManager->getAvailableTypes();
  46. } else {
  47. $availableTypes = $this->cacheManager->getAvailableTypes();
  48. $unsupportedTypes = array_diff($requestedTypes, $availableTypes);
  49. if ($unsupportedTypes) {
  50. throw new \InvalidArgumentException(
  51. "The following requested cache types are not supported: '" . join("', '", $unsupportedTypes)
  52. . "'." . PHP_EOL . 'Supported types: ' . join(", ", $availableTypes)
  53. );
  54. }
  55. return array_values(array_intersect($availableTypes, $requestedTypes));
  56. }
  57. }
  58. }