ConfigShowCommand.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Console\Command;
  7. use Magento\Config\Console\Command\ConfigShow\ValueProcessor;
  8. use Magento\Framework\App\Config\ConfigPathResolver;
  9. use Magento\Framework\App\Config\ConfigSourceInterface;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\App\Scope\ValidatorInterface;
  12. use Magento\Framework\Console\Cli;
  13. use Symfony\Component\Console\Command\Command;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. /**
  19. * Command provides possibility to show saved system configuration.
  20. *
  21. * @api
  22. * @since 101.0.0
  23. */
  24. class ConfigShowCommand extends Command
  25. {
  26. /**#@+
  27. * Names of input arguments or options.
  28. */
  29. const INPUT_OPTION_SCOPE = 'scope';
  30. const INPUT_OPTION_SCOPE_CODE = 'scope-code';
  31. const INPUT_ARGUMENT_PATH = 'path';
  32. /**#@-*/
  33. /**#@-*/
  34. private $scopeValidator;
  35. /**
  36. * Source of configurations.
  37. *
  38. * @var ConfigSourceInterface
  39. */
  40. private $configSource;
  41. /**
  42. * Config path resolver.
  43. *
  44. * @var ConfigPathResolver
  45. */
  46. private $pathResolver;
  47. /**
  48. * Class for processing value using backend model.
  49. *
  50. * @var ValueProcessor
  51. */
  52. private $valueProcessor;
  53. /**
  54. * The scope of configuration.
  55. *
  56. * @var string
  57. */
  58. private $scope;
  59. /**
  60. * The scope code of configuration.
  61. *
  62. * @var string
  63. */
  64. private $scopeCode;
  65. /**
  66. * The configuration path.
  67. *
  68. * @var string
  69. */
  70. private $inputPath;
  71. /**
  72. * @param ValidatorInterface $scopeValidator
  73. * @param ConfigSourceInterface $configSource
  74. * @param ConfigPathResolver $pathResolver
  75. * @param ValueProcessor $valueProcessor
  76. * @internal param ScopeConfigInterface $appConfig
  77. */
  78. public function __construct(
  79. ValidatorInterface $scopeValidator,
  80. ConfigSourceInterface $configSource,
  81. ConfigPathResolver $pathResolver,
  82. ValueProcessor $valueProcessor
  83. ) {
  84. parent::__construct();
  85. $this->scopeValidator = $scopeValidator;
  86. $this->configSource = $configSource;
  87. $this->pathResolver = $pathResolver;
  88. $this->valueProcessor = $valueProcessor;
  89. }
  90. /**
  91. * @inheritdoc
  92. * @since 101.0.0
  93. */
  94. protected function configure()
  95. {
  96. $this->addArgument(
  97. self::INPUT_ARGUMENT_PATH,
  98. InputArgument::OPTIONAL,
  99. 'Configuration path, for example section_id/group_id/field_id'
  100. );
  101. $this->addOption(
  102. self::INPUT_OPTION_SCOPE,
  103. null,
  104. InputOption::VALUE_OPTIONAL,
  105. 'Scope for configuration, if not specified, then \'default\' scope will be used',
  106. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  107. );
  108. $this->addOption(
  109. self::INPUT_OPTION_SCOPE_CODE,
  110. null,
  111. InputOption::VALUE_OPTIONAL,
  112. 'Scope code (required only if scope is not `default`)',
  113. ''
  114. );
  115. $this->setName('config:show')
  116. ->setDescription(
  117. 'Shows configuration value for given path. If path is not specified, all saved values will be shown'
  118. );
  119. parent::configure();
  120. }
  121. /**
  122. * Displays configuration value for given configuration path.
  123. *
  124. * Shows error message if configuration for given path doesn't exist
  125. * or scope/scope-code doesn't pass validation.
  126. *
  127. * {@inheritdoc}
  128. * @since 101.0.0
  129. */
  130. protected function execute(InputInterface $input, OutputInterface $output)
  131. {
  132. try {
  133. $this->scope = $input->getOption(self::INPUT_OPTION_SCOPE);
  134. $this->scopeCode = $input->getOption(self::INPUT_OPTION_SCOPE_CODE);
  135. $this->inputPath = trim($input->getArgument(self::INPUT_ARGUMENT_PATH), '/');
  136. $this->scopeValidator->isValid($this->scope, $this->scopeCode);
  137. $configPath = $this->pathResolver->resolve($this->inputPath, $this->scope, $this->scopeCode);
  138. $configValue = $this->configSource->get($configPath);
  139. if (empty($configValue)) {
  140. $output->writeln(sprintf(
  141. '<error>%s</error>',
  142. __('Configuration for path: "%1" doesn\'t exist', $this->inputPath)->render()
  143. ));
  144. return Cli::RETURN_FAILURE;
  145. }
  146. $this->outputResult($output, $configValue, $this->inputPath);
  147. return Cli::RETURN_SUCCESS;
  148. } catch (\Exception $e) {
  149. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  150. return Cli::RETURN_FAILURE;
  151. }
  152. }
  153. /**
  154. * Outputs single configuration value or list of values if array given.
  155. *
  156. * @param OutputInterface $output an OutputInterface instance
  157. * @param string|array $configValue can be string when $configPath is a path for concreate field.
  158. * In other cases $configValue should be an array
  159. * ```php
  160. * [
  161. * 'section' =>
  162. * [
  163. * 'group1' =>
  164. * [
  165. * 'field1' => 'value1',
  166. * 'field2' => 'value2'
  167. * ],
  168. * 'group2' =>
  169. * [
  170. * 'field1' => 'value3'
  171. * ]
  172. * ]
  173. * ]
  174. * ```
  175. * @param string $configPath base configuration path
  176. * @return void
  177. */
  178. private function outputResult(OutputInterface $output, $configValue, $configPath)
  179. {
  180. if (!is_array($configValue)) {
  181. $value = $this->valueProcessor->process($this->scope, $this->scopeCode, $configValue, $configPath);
  182. $output->writeln($this->inputPath === $configPath ? $value : sprintf("%s - %s", $configPath, $value));
  183. } elseif (is_array($configValue)) {
  184. foreach ($configValue as $name => $value) {
  185. $childPath = empty($configPath) ? $name : ($configPath . '/' . $name);
  186. $this->outputResult($output, $value, $childPath);
  187. }
  188. }
  189. }
  190. }