-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathConfigShowCommand.php
252 lines (232 loc) · 8.04 KB
/
ConfigShowCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* Copyright 2017 Adobe
* All Rights Reserved.
*/
namespace Magento\Config\Console\Command;
use Magento\Config\Console\Command\ConfigShow\ValueProcessor;
use Magento\Config\Model\Config\PathValidatorFactory;
use Magento\Framework\App\Config\ConfigPathResolver;
use Magento\Framework\App\Config\ConfigSourceInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Scope\ValidatorInterface;
use Magento\Framework\Console\Cli;
use Magento\Framework\Exception\ValidatorException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Command provides possibility to show saved system configuration.
*
* @api
* @since 101.0.0
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ConfigShowCommand extends Command
{
/**#@+
* Names of input arguments or options.
*/
public const INPUT_OPTION_SCOPE = 'scope';
public const INPUT_OPTION_SCOPE_CODE = 'scope-code';
public const INPUT_ARGUMENT_PATH = 'path';
/**#@-*/
/**
* @var ValidatorInterface
*/
private $scopeValidator;
/**
* Source of configurations.
*
* @var ConfigSourceInterface
*/
private $configSource;
/**
* Config path resolver.
*
* @var ConfigPathResolver
*/
private $pathResolver;
/**
* Class for processing value using backend model.
*
* @var ValueProcessor
*/
private $valueProcessor;
/**
* The scope of configuration.
*
* @var string
*/
private $scope;
/**
* The scope code of configuration.
*
* @var string
*/
private $scopeCode;
/**
* The configuration path.
*
* @var string
*/
private $inputPath;
/**
* @var PathValidatorFactory
*/
private $pathValidatorFactory;
/**
* @var EmulatedAdminhtmlAreaProcessor
*/
private $emulatedAreaProcessor;
/**
* @var LocaleEmulatorInterface|mixed
*/
private mixed $localeEmulator;
/**
* @param ValidatorInterface $scopeValidator
* @param ConfigSourceInterface $configSource
* @param ConfigPathResolver $pathResolver
* @param ValueProcessor $valueProcessor
* @param PathValidatorFactory|null $pathValidatorFactory
* @param EmulatedAdminhtmlAreaProcessor|null $emulatedAreaProcessor
* @param LocaleEmulatorInterface|null $localeEmulator
* @internal param ScopeConfigInterface $appConfig
*/
public function __construct(
ValidatorInterface $scopeValidator,
ConfigSourceInterface $configSource,
ConfigPathResolver $pathResolver,
ValueProcessor $valueProcessor,
?PathValidatorFactory $pathValidatorFactory = null,
?EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null,
?LocaleEmulatorInterface $localeEmulator = null
) {
parent::__construct();
$this->scopeValidator = $scopeValidator;
$this->configSource = $configSource;
$this->pathResolver = $pathResolver;
$this->valueProcessor = $valueProcessor;
$this->pathValidatorFactory = $pathValidatorFactory
?: ObjectManager::getInstance()->get(PathValidatorFactory::class);
$this->emulatedAreaProcessor = $emulatedAreaProcessor
?: ObjectManager::getInstance()->get(EmulatedAdminhtmlAreaProcessor::class);
$this->localeEmulator = $localeEmulator;
}
/**
* @inheritdoc
* @since 101.0.0
*/
protected function configure()
{
$this->addArgument(
self::INPUT_ARGUMENT_PATH,
InputArgument::OPTIONAL,
'Configuration path, for example section_id/group_id/field_id'
);
$this->addOption(
self::INPUT_OPTION_SCOPE,
null,
InputOption::VALUE_OPTIONAL,
'Scope for configuration, if not specified, then \'default\' scope will be used',
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
);
$this->addOption(
self::INPUT_OPTION_SCOPE_CODE,
null,
InputOption::VALUE_OPTIONAL,
'Scope code (required only if scope is not `default`)',
''
);
$this->setName('config:show')
->setDescription(
'Shows configuration value for given path. If path is not specified, all saved values will be shown'
);
parent::configure();
}
/**
* Displays configuration value for given configuration path.
*
* Shows error message if configuration for given path doesn't exist
* or scope/scope-code doesn't pass validation.
*
* @inheritdoc
* @since 101.0.0
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$this->scope = $input->getOption(self::INPUT_OPTION_SCOPE);
$this->scopeCode = $input->getOption(self::INPUT_OPTION_SCOPE_CODE);
$inputPath = $input->getArgument(self::INPUT_ARGUMENT_PATH);
$this->inputPath = $inputPath !== null ? trim($inputPath, '/') : '';
$configValue = $this->emulatedAreaProcessor->process(function () {
return $this->localeEmulator->emulate(function () {
$this->scopeValidator->isValid($this->scope, $this->scopeCode);
$configPath = $this->pathResolver
->resolve($this->inputPath, $this->scope, $this->scopeCode);
$value = $this->configSource->get($configPath);
if (!$value) {
$configPath = $this->pathResolver
->resolve($this->inputPath, $this->scope, strtolower($this->scopeCode));
$value = $this->configSource->get($configPath);
if (!$value) {
$value = $this->configSource->get(strtolower((string)$configPath));
}
}
return $value;
});
});
if (empty($configValue)) {
throw new ValidatorException(
__('The "%1" path doesn\'t exist. Verify and try again.', $this->inputPath)
);
}
$this->outputResult($output, $configValue, $this->inputPath);
return Cli::RETURN_SUCCESS;
} catch (\Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
return Cli::RETURN_FAILURE;
}
}
/**
* Outputs single configuration value or list of values if array given.
*
* @param OutputInterface $output an OutputInterface instance
* @param string|array $configValue can be string when $configPath is a path for concreate field.
* In other cases $configValue should be an array
* ```php
* [
* 'section' =>
* [
* 'group1' =>
* [
* 'field1' => 'value1',
* 'field2' => 'value2'
* ],
* 'group2' =>
* [
* 'field1' => 'value3'
* ]
* ]
* ]
* ```
* @param string $configPath base configuration path
* @return void
*/
private function outputResult(OutputInterface $output, $configValue, $configPath)
{
if (!is_array($configValue)) {
$value = $this->valueProcessor->process($this->scope, $this->scopeCode, $configValue, $configPath);
$output->writeln($this->inputPath === $configPath ? [$value] : sprintf("%s - %s", $configPath, $value));
} elseif (is_array($configValue)) {
foreach ($configValue as $name => $value) {
$childPath = empty($configPath) ? $name : ($configPath . '/' . $name);
$this->outputResult($output, $value, $childPath);
}
}
}
}