Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigShowCommand.php
Go to the documentation of this file.
1 <?php
7 
14 use Symfony\Component\Console\Command\Command;
15 use Symfony\Component\Console\Input\InputArgument;
16 use Symfony\Component\Console\Input\InputInterface;
17 use Symfony\Component\Console\Input\InputOption;
18 use Symfony\Component\Console\Output\OutputInterface;
19 
26 class ConfigShowCommand extends Command
27 {
31  const INPUT_OPTION_SCOPE = 'scope';
32  const INPUT_OPTION_SCOPE_CODE = 'scope-code';
33  const INPUT_ARGUMENT_PATH = 'path';
37  private $scopeValidator;
38 
44  private $configSource;
45 
51  private $pathResolver;
52 
58  private $valueProcessor;
59 
65  private $scope;
66 
72  private $scopeCode;
73 
79  private $inputPath;
80 
88  public function __construct(
89  ValidatorInterface $scopeValidator,
90  ConfigSourceInterface $configSource,
91  ConfigPathResolver $pathResolver,
92  ValueProcessor $valueProcessor
93  ) {
94  parent::__construct();
95  $this->scopeValidator = $scopeValidator;
96  $this->configSource = $configSource;
97  $this->pathResolver = $pathResolver;
98  $this->valueProcessor = $valueProcessor;
99  }
100 
105  protected function configure()
106  {
107  $this->addArgument(
108  self::INPUT_ARGUMENT_PATH,
109  InputArgument::OPTIONAL,
110  'Configuration path, for example section_id/group_id/field_id'
111  );
112  $this->addOption(
113  self::INPUT_OPTION_SCOPE,
114  null,
115  InputOption::VALUE_OPTIONAL,
116  'Scope for configuration, if not specified, then \'default\' scope will be used',
118  );
119  $this->addOption(
120  self::INPUT_OPTION_SCOPE_CODE,
121  null,
122  InputOption::VALUE_OPTIONAL,
123  'Scope code (required only if scope is not `default`)',
124  ''
125  );
126  $this->setName('config:show')
127  ->setDescription(
128  'Shows configuration value for given path. If path is not specified, all saved values will be shown'
129  );
130  parent::configure();
131  }
132 
142  protected function execute(InputInterface $input, OutputInterface $output)
143  {
144  try {
145  $this->scope = $input->getOption(self::INPUT_OPTION_SCOPE);
146  $this->scopeCode = $input->getOption(self::INPUT_OPTION_SCOPE_CODE);
147  $this->inputPath = trim($input->getArgument(self::INPUT_ARGUMENT_PATH), '/');
148 
149  $this->scopeValidator->isValid($this->scope, $this->scopeCode);
150  $configPath = $this->pathResolver->resolve($this->inputPath, $this->scope, $this->scopeCode);
151  $configValue = $this->configSource->get($configPath);
152 
153  if (empty($configValue)) {
154  $output->writeln(sprintf(
155  '<error>%s</error>',
156  __('Configuration for path: "%1" doesn\'t exist', $this->inputPath)->render()
157  ));
158  return Cli::RETURN_FAILURE;
159  }
160 
161  $this->outputResult($output, $configValue, $this->inputPath);
162  return Cli::RETURN_SUCCESS;
163  } catch (\Exception $e) {
164  $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
165  return Cli::RETURN_FAILURE;
166  }
167  }
168 
194  private function outputResult(OutputInterface $output, $configValue, $configPath)
195  {
196  if (!is_array($configValue)) {
197  $value = $this->valueProcessor->process($this->scope, $this->scopeCode, $configValue, $configPath);
198  $output->writeln($this->inputPath === $configPath ? $value : sprintf("%s - %s", $configPath, $value));
199  } elseif (is_array($configValue)) {
200  foreach ($configValue as $name => $value) {
201  $childPath = empty($configPath) ? $name : ($configPath . '/' . $name);
202  $this->outputResult($output, $value, $childPath);
203  }
204  }
205  }
206 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
execute(InputInterface $input, OutputInterface $output)
__construct(ValidatorInterface $scopeValidator, ConfigSourceInterface $configSource, ConfigPathResolver $pathResolver, ValueProcessor $valueProcessor)
if(!isset($_GET['name'])) $name
Definition: log.php:14