Skip to content

Commit fd69577

Browse files
author
Bohdan Korablov
committed
MAGETWO-63382: CLI Improvements: Configuration management - Command config:show
1 parent 46ace61 commit fd69577

File tree

8 files changed

+92
-165
lines changed

8 files changed

+92
-165
lines changed

app/code/Magento/Config/Console/Command/ConfigShow/ConfigSourceAggregated.php

Lines changed: 0 additions & 62 deletions
This file was deleted.

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Magento\Framework\App\Config\ConfigSourceInterface;
99
use Magento\Framework\App\Config\MetadataProcessor;
1010
use Magento\Framework\App\Config\ScopeConfigInterface;
11-
use Magento\Framework\App\Config\ScopePathResolver;
11+
use Magento\Framework\App\Config\ConfigPathResolver;
1212
use Magento\Framework\Exception\LocalizedException;
1313
use Symfony\Component\Console\Command\Command;
1414
use Magento\Framework\App\Scope\ValidatorInterface;
@@ -42,7 +42,7 @@ class ConfigShowCommand extends Command
4242
private $configSource;
4343

4444
/**
45-
* @var ScopePathResolver
45+
* @var ConfigPathResolver
4646
*/
4747
private $pathResolver;
4848

@@ -51,17 +51,22 @@ class ConfigShowCommand extends Command
5151
*/
5252
private $metadataProcessor;
5353

54+
/**
55+
* @var string
56+
*/
57+
private $сonfigPath;
58+
5459
/**
5560
* @param ValidatorInterface $scopeValidator
5661
* @param ConfigSourceInterface $configSource
57-
* @param ScopePathResolver $pathResolver
62+
* @param ConfigPathResolver $pathResolver
5863
* @param MetadataProcessor $metadataProcessor
5964
* @internal param ScopeConfigInterface $appConfig
6065
*/
6166
public function __construct(
6267
ValidatorInterface $scopeValidator,
6368
ConfigSourceInterface $configSource,
64-
ScopePathResolver $pathResolver,
69+
ConfigPathResolver $pathResolver,
6570
MetadataProcessor $metadataProcessor
6671
) {
6772
parent::__construct();
@@ -112,11 +117,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
112117
{
113118
$scope = $input->getOption(self::INPUT_OPTION_SCOPE);
114119
$scopeCode = $input->getOption(self::INPUT_OPTION_SCOPE_CODE);
115-
$inputPath = $input->getArgument(self::INPUT_ARGUMENT_PATH);
120+
$this->сonfigPath = $input->getArgument(self::INPUT_ARGUMENT_PATH);
116121

117122
try {
118123
$this->scopeValidator->isValid($scope, $scopeCode);
119-
$configPath = $this->pathResolver->resolve($inputPath, $scope, $scopeCode);
124+
$configPath = $this->pathResolver->resolve($this->сonfigPath, $scope, $scopeCode);
120125
$configValue = $this->configSource->get($configPath);
121126
} catch (LocalizedException $e) {
122127
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
@@ -126,12 +131,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
126131
if ($configValue === null) {
127132
$output->writeln(sprintf(
128133
'<error>%s</error>',
129-
__('Configuration for path: "%1" doesn\'t exist', $inputPath)->render()
134+
__('Configuration for path: "%1" doesn\'t exist', $this->сonfigPath)->render()
130135
));
131136
return Cli::RETURN_FAILURE;
132137
}
133138

134-
$this->outputResult($output, $configValue, $inputPath);
139+
$this->outputResult($output, $configValue, $this->сonfigPath);
135140
return Cli::RETURN_SUCCESS;
136141
}
137142

@@ -140,23 +145,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
140145
*
141146
* @param OutputInterface $output An OutputInterface instance
142147
* @param mixed $configValue single value or array of values
143-
* @param $configPath base configuration path
144-
* @param int $level depth level for nested configuration
148+
* @param string $configPath base configuration path
145149
* @return void
146150
*/
147-
private function outputResult(OutputInterface $output, $configValue, $configPath, $level = 0)
151+
private function outputResult(OutputInterface $output, $configValue, $configPath)
148152
{
149-
$margin = str_repeat(" ", max($level - 1, 0) * 2);
150-
if (is_string($configValue)) {
153+
if (!is_array($configValue)) {
151154
$value = $this->metadataProcessor->processValue($configValue, $configPath);
152-
$output->writeln(sprintf("%s%s - %s", $margin, $configPath, $value));
155+
$output->writeln($this->сonfigPath === $configPath ? $value : sprintf("%s - %s", $configPath, $value));
153156
} else if (is_array($configValue)) {
154-
if ($level > 0) {
155-
$output->writeln(sprintf("%s%s:", $margin, $configPath ?: 'config'));
156-
}
157157
foreach ($configValue as $name => $value) {
158158
$childPath = empty($configPath) ? $name : ($configPath . '/' . $name);
159-
$this->outputResult($output, $value, $childPath, $level + 1);
159+
$this->outputResult($output, $value, $childPath);
160160
}
161161
}
162162
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\Framework\App\Scope\ValidatorInterface;
1414
use Symfony\Component\Console\Tester\CommandTester;
1515
use Magento\Framework\App\Config\MetadataProcessor;
16-
use Magento\Framework\App\Config\ScopePathResolver;
16+
use Magento\Framework\App\Config\ConfigPathResolver;
1717

1818
class ConfigShowCommandTest extends \PHPUnit_Framework_TestCase
1919
{
@@ -33,7 +33,7 @@ class ConfigShowCommandTest extends \PHPUnit_Framework_TestCase
3333
private $metadataProcessorMock;
3434

3535
/**
36-
* @var ScopePathResolver|MockObject
36+
* @var ConfigPathResolver|MockObject
3737
*/
3838
private $pathResolverMock;
3939

@@ -47,7 +47,7 @@ protected function setUp()
4747
$this->metadataProcessorMock = $this->getMockBuilder(MetadataProcessor::class)
4848
->disableOriginalConstructor()
4949
->getMock();
50-
$this->pathResolverMock = $this->getMockBuilder(ScopePathResolver::class)
50+
$this->pathResolverMock = $this->getMockBuilder(ConfigPathResolver::class)
5151
->disableOriginalConstructor()
5252
->getMock();
5353
$this->scopeValidatorMock = $this->getMockBuilder(ValidatorInterface::class)

app/code/Magento/Config/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
<argument name="placeholder" xsi:type="object">Magento\Config\Model\Placeholder\Environment</argument>
191191
</arguments>
192192
</type>
193-
<virtualType name="configShowSourceAggregated" type="Magento\Config\Console\Command\ConfigShow\ConfigSourceAggregated">
193+
<virtualType name="configShowSourceAggregated" type="Magento\Framework\App\Config\ConfigSourceAggregated">
194194
<arguments>
195195
<argument name="sources" xsi:type="array">
196196
<item name="dynamic" xsi:type="array">
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\App\Config;
7+
8+
use Magento\Store\Model\ScopeInterface;
9+
10+
/**
11+
* Resolves config path by input parameters.
12+
*/
13+
class ConfigPathResolver
14+
{
15+
/**
16+
* @var ScopeCodeResolver
17+
*/
18+
private $scopeCodeResolver;
19+
20+
/**
21+
* @param ScopeCodeResolver $scopeCodeResolver
22+
*/
23+
public function __construct(ScopeCodeResolver $scopeCodeResolver)
24+
{
25+
$this->scopeCodeResolver = $scopeCodeResolver;
26+
}
27+
28+
/**
29+
* Creates full config path for given params.
30+
* If $type variable was provided, it will be used as first part of path.
31+
*
32+
* @param string $path The path of configuration
33+
* @param string $scope The scope of configuration
34+
* @param string|int|null $scopeCode The scope code or identifier of configuration
35+
* @param string|null $type The type of configuration
36+
* @return string Resolved configuration path
37+
*/
38+
public function resolve($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null, $type = null)
39+
{
40+
$path = trim($path, '/');
41+
$scope = rtrim($scope, 's');
42+
43+
/**
44+
* Scope name is currently stored in plural form.
45+
* Desired behavior will be changed in mayor release.
46+
*/
47+
if (in_array($scope, [ScopeInterface::SCOPE_STORE, ScopeInterface::SCOPE_WEBSITE])) {
48+
$scope .= 's';
49+
}
50+
51+
$scopePath = $type ? $type . '/' . $scope : $scope;
52+
53+
if ($scope !== ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
54+
if (is_numeric($scopeCode) || $scopeCode === null) {
55+
$scopeCode = $this->scopeCodeResolver->resolve($scope, $scopeCode);
56+
}
57+
58+
$scopePath .= '/' . $scopeCode;
59+
}
60+
61+
return trim($scopePath . '/' . $path, '/');
62+
}
63+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(array $sources = [])
2828
* Retrieve aggregated configuration from all available sources.
2929
*
3030
* @param string $path
31-
* @return array
31+
* @return string|array
3232
*/
3333
public function get($path = '')
3434
{
@@ -37,7 +37,11 @@ public function get($path = '')
3737
foreach ($this->sources as $sourceConfig) {
3838
/** @var ConfigSourceInterface $source */
3939
$source = $sourceConfig['source'];
40-
$data = array_replace_recursive($data, $source->get($path));
40+
$configData = $source->get($path);
41+
if (!is_array($configData)) {
42+
return $configData;
43+
}
44+
$data = array_replace_recursive($data, $configData);
4145
}
4246
return $data;
4347
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface ConfigSourceInterface
1616
* Retrieve configuration raw data array.
1717
*
1818
* @param string $path
19-
* @return array
19+
* @return string|array
2020
*/
2121
public function get($path = '');
2222
}

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

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)