Skip to content

Commit 00ea825

Browse files
author
Bohdan Korablov
committed
MAGETWO-63382: CLI Improvements: Configuration management - Command config:show
1 parent 228da06 commit 00ea825

File tree

4 files changed

+117
-74
lines changed

4 files changed

+117
-74
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\Console\Command\ConfigShow;
7+
8+
use Magento\Framework\App\Config\Value;
9+
use Magento\Framework\App\Config\ValueFactory;
10+
use Magento\Config\Model\Config\Structure;
11+
use Magento\Config\Model\Config\StructureFactory;
12+
use Magento\Config\Model\Config\Structure\Element\Field;
13+
use Magento\Framework\Config\ScopeInterface;
14+
use Magento\Framework\App\Area;
15+
16+
/**
17+
* Class processes value using backend model.
18+
*/
19+
class ValueProcessor
20+
{
21+
/**
22+
* System configuration structure.
23+
*
24+
* @var Structure
25+
*/
26+
private $configStructure;
27+
28+
/**
29+
* Factory of object that implement \Magento\Framework\App\Config\ValueInterface.
30+
*
31+
* @var ValueFactory
32+
*/
33+
private $configValueFactory;
34+
35+
/**
36+
* @param ScopeInterface $scope
37+
* @param StructureFactory $structureFactory
38+
* @param ValueFactory $valueFactory
39+
*/
40+
public function __construct(
41+
ScopeInterface $scope,
42+
StructureFactory $structureFactory,
43+
ValueFactory $valueFactory
44+
) {
45+
$scope->setCurrentScope(Area::AREA_ADMINHTML);
46+
$this->configStructure = $structureFactory->create();
47+
$this->configValueFactory = $valueFactory;
48+
}
49+
50+
/**
51+
* Processes value using backend model.
52+
*
53+
* @param string $scope
54+
* @param string $scopeCode
55+
* @param string $value
56+
* @param string $path
57+
* @return string
58+
*/
59+
public function process($scope, $scopeCode, $value, $path)
60+
{
61+
/** @var Field $field */
62+
$field = $this->configStructure->getElement($path);
63+
/** @var Value $backendModel */
64+
$backendModel = $field->hasBackendModel()
65+
? $field->getBackendModel()
66+
: $this->configValueFactory->create();
67+
$backendModel->setPath($path);
68+
$backendModel->setScope($scope);
69+
$backendModel->setScopeId($scopeCode);
70+
$backendModel->setValue($value);
71+
$backendModel->afterLoad();
72+
73+
return $backendModel->getValue();
74+
}
75+
}

app/code/Magento/Config/Console/Command/ConfigShowCommand.php

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace Magento\Config\Console\Command;
77

88
use Magento\Framework\App\Config\ConfigSourceInterface;
9-
use Magento\Framework\App\Config\MetadataProcessor;
109
use Magento\Framework\App\Config\ScopeConfigInterface;
1110
use Magento\Framework\App\Config\ConfigPathResolver;
1211
use Magento\Framework\Exception\LocalizedException;
@@ -17,6 +16,7 @@
1716
use Symfony\Component\Console\Input\InputArgument;
1817
use Symfony\Component\Console\Input\InputOption;
1918
use Magento\Framework\Console\Cli;
19+
use Magento\Config\Console\Command\ConfigShow\ValueProcessor;
2020

2121
/**
2222
* Command provides possibility to show system configuration.
@@ -47,28 +47,43 @@ class ConfigShowCommand extends Command
4747
private $pathResolver;
4848

4949
/**
50-
* @var MetadataProcessor
50+
* @var ValueProcessor
5151
*/
52-
private $metadataProcessor;
52+
private $valueProcessor;
53+
54+
/**
55+
* @var string
56+
*/
57+
private $scope;
58+
59+
/**
60+
* @var string
61+
*/
62+
private $scopeCode;
63+
64+
/**
65+
* @var string
66+
*/
67+
private $inputPath;
5368

5469
/**
5570
* @param ValidatorInterface $scopeValidator
5671
* @param ConfigSourceInterface $configSource
5772
* @param ConfigPathResolver $pathResolver
58-
* @param MetadataProcessor $metadataProcessor
73+
* @param ValueProcessor $valueProcessor
5974
* @internal param ScopeConfigInterface $appConfig
6075
*/
6176
public function __construct(
6277
ValidatorInterface $scopeValidator,
6378
ConfigSourceInterface $configSource,
6479
ConfigPathResolver $pathResolver,
65-
MetadataProcessor $metadataProcessor
80+
ValueProcessor $valueProcessor
6681
) {
6782
parent::__construct();
6883
$this->scopeValidator = $scopeValidator;
6984
$this->configSource = $configSource;
7085
$this->pathResolver = $pathResolver;
71-
$this->metadataProcessor = $metadataProcessor;
86+
$this->valueProcessor = $valueProcessor;
7287
}
7388

7489
/**
@@ -110,50 +125,48 @@ protected function configure()
110125
*/
111126
protected function execute(InputInterface $input, OutputInterface $output)
112127
{
113-
$scope = $input->getOption(self::INPUT_OPTION_SCOPE);
114-
$scopeCode = $input->getOption(self::INPUT_OPTION_SCOPE_CODE);
115-
$inputPath = $input->getArgument(self::INPUT_ARGUMENT_PATH);
128+
$this->scope = $input->getOption(self::INPUT_OPTION_SCOPE);
129+
$this->scopeCode = $input->getOption(self::INPUT_OPTION_SCOPE_CODE);
130+
$this->inputPath = $input->getArgument(self::INPUT_ARGUMENT_PATH);
116131

117132
try {
118-
$this->scopeValidator->isValid($scope, $scopeCode);
119-
$configPath = $this->pathResolver->resolve($inputPath, $scope, $scopeCode);
133+
$this->scopeValidator->isValid($this->scope, $this->scopeCode);
134+
$configPath = $this->pathResolver->resolve($this->inputPath, $this->scope, $this->scopeCode);
120135
$configValue = $this->configSource->get($configPath);
121136
} catch (LocalizedException $e) {
122137
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
123138
return Cli::RETURN_FAILURE;
124139
}
125140

126-
if ($configValue === null) {
141+
if (empty($configValue)) {
127142
$output->writeln(sprintf(
128143
'<error>%s</error>',
129-
__('Configuration for path: "%1" doesn\'t exist', $inputPath)->render()
144+
__('Configuration for path: "%1" doesn\'t exist', $this->inputPath)->render()
130145
));
131146
return Cli::RETURN_FAILURE;
132147
}
133148

134-
$this->outputResult($input, $output, $configValue, $inputPath);
149+
$this->outputResult($output, $configValue, $this->inputPath);
135150
return Cli::RETURN_SUCCESS;
136151
}
137152

138153
/**
139154
* Outputs single configuration value or list of values if array given.
140155
*
141-
* @param InputInterface $input an InputInterface instance
142156
* @param OutputInterface $output an OutputInterface instance
143157
* @param mixed $configValue single value or array of values
144158
* @param string $configPath base configuration path
145159
* @return void
146160
*/
147-
private function outputResult(InputInterface $input, OutputInterface $output, $configValue, $configPath)
161+
private function outputResult(OutputInterface $output, $configValue, $configPath)
148162
{
149163
if (!is_array($configValue)) {
150-
$value = $this->metadataProcessor->processValue($configValue, $configPath);
151-
$inputPath = $input->getArgument(self::INPUT_ARGUMENT_PATH);
152-
$output->writeln($inputPath === $configPath ? $value : sprintf("%s - %s", $configPath, $value));
153-
} else if (is_array($configValue)) {
164+
$value = $this->valueProcessor->process($this->scope, $this->scopeCode, $configValue, $configPath);
165+
$output->writeln($this->inputPath === $configPath ? $value : sprintf("%s - %s", $configPath, $value));
166+
} elseif (is_array($configValue)) {
154167
foreach ($configValue as $name => $value) {
155168
$childPath = empty($configPath) ? $name : ($configPath . '/' . $name);
156-
$this->outputResult($input, $output, $value, $childPath);
169+
$this->outputResult($output, $value, $childPath);
157170
}
158171
}
159172
}

app/code/Magento/Config/Test/Unit/Console/Command/ConfigShowCommandTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1313
use Magento\Framework\App\Scope\ValidatorInterface;
1414
use Symfony\Component\Console\Tester\CommandTester;
15-
use Magento\Framework\App\Config\MetadataProcessor;
1615
use Magento\Framework\App\Config\ConfigPathResolver;
16+
use Magento\Config\Console\Command\ConfigShow\ValueProcessor;
1717

1818
class ConfigShowCommandTest extends \PHPUnit_Framework_TestCase
1919
{
@@ -28,9 +28,9 @@ class ConfigShowCommandTest extends \PHPUnit_Framework_TestCase
2828
private $configSourceMock;
2929

3030
/**
31-
* @var MetadataProcessor|MockObject
31+
* @var ValueProcessor|MockObject
3232
*/
33-
private $metadataProcessorMock;
33+
private $valueProcessorMock;
3434

3535
/**
3636
* @var ConfigPathResolver|MockObject
@@ -44,7 +44,7 @@ class ConfigShowCommandTest extends \PHPUnit_Framework_TestCase
4444

4545
protected function setUp()
4646
{
47-
$this->metadataProcessorMock = $this->getMockBuilder(MetadataProcessor::class)
47+
$this->valueProcessorMock = $this->getMockBuilder(ValueProcessor::class)
4848
->disableOriginalConstructor()
4949
->getMock();
5050
$this->pathResolverMock = $this->getMockBuilder(ConfigPathResolver::class)
@@ -59,7 +59,7 @@ protected function setUp()
5959
$this->scopeValidatorMock,
6060
$this->configSourceMock,
6161
$this->pathResolverMock,
62-
$this->metadataProcessorMock
62+
$this->valueProcessorMock
6363
);
6464
}
6565

@@ -82,9 +82,9 @@ public function testExecute()
8282
->method('get')
8383
->with($resolvedConfigPath)
8484
->willReturn('someValue');
85-
$this->metadataProcessorMock->expects($this->once())
86-
->method('processValue')
87-
->with('someValue')
85+
$this->valueProcessorMock->expects($this->once())
86+
->method('process')
87+
->with($scope, $scopeCode, 'someValue', $configPath)
8888
->willReturn('someProcessedValue');
8989

9090
$tester = $this->getConfigShowCommandTester($configPath, $scope, $scopeCode);

lib/internal/Magento/Framework/App/Config/MetadataProcessor.php

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -88,49 +88,4 @@ public function process(array $data)
8888
}
8989
return $data;
9090
}
91-
92-
/**
93-
* Processes value into correct form.
94-
* Example: decrypt a value.
95-
*
96-
* @param string|int $value The value to process
97-
* @param string $path The path of the value
98-
* @return string The processed value
99-
*/
100-
public function processValue($value, $path)
101-
{
102-
if (isset($this->_metadata[$path])) {
103-
/** @var \Magento\Framework\App\Config\Data\ProcessorInterface $processor */
104-
$processor = $this->_processorFactory->get($this->_metadata[$path]['backendModel']);
105-
106-
return $processor->processValue($value);
107-
}
108-
109-
return $value;
110-
}
111-
112-
/**
113-
* Prepares value for saving.
114-
* Example: encrypt a value.
115-
*
116-
* @param string|int $value The value to prepare
117-
* @param string $path The path of the value
118-
* @return string The prepared value
119-
*/
120-
public function prepareValue($value, $path)
121-
{
122-
if (isset($this->_metadata[$path])) {
123-
/** @var \Magento\Framework\App\Config\Data\ProcessorInterface $processor */
124-
$processor = $this->_processorFactory->get($this->_metadata[$path]['backendModel']);
125-
126-
if ($processor instanceof \Magento\Framework\App\Config\Value) {
127-
$processor->setValue($value);
128-
$processor->beforeSave();
129-
130-
return $processor->getValue();
131-
}
132-
}
133-
134-
return $value;
135-
}
13691
}

0 commit comments

Comments
 (0)