Skip to content

Commit bc5b02d

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

File tree

3 files changed

+94
-7
lines changed

3 files changed

+94
-7
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\App\Config\Source;
7+
8+
use Magento\Framework\App\Config\ConfigSourceInterface;
9+
use Magento\Framework\DataObject;
10+
use Magento\Config\Model\Placeholder\PlaceholderFactory;
11+
use Magento\Config\Model\Placeholder\PlaceholderInterface;
12+
use Magento\Framework\Stdlib\ArrayManager;
13+
14+
/**
15+
* Class for retrieving configuration from environment variables.
16+
*/
17+
class EnvironmentConfigSource implements ConfigSourceInterface
18+
{
19+
/**
20+
* @var ArrayManager
21+
*/
22+
private $arrayManager;
23+
24+
/**
25+
* @var PlaceholderInterface
26+
*/
27+
private $placeholder;
28+
29+
public function __construct(
30+
ArrayManager $arrayManager,
31+
PlaceholderFactory $placeholderFactory
32+
) {
33+
$this->arrayManager = $arrayManager;
34+
$this->placeholder = $placeholderFactory->create(PlaceholderFactory::TYPE_ENVIRONMENT);
35+
}
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
public function get($path = '')
41+
{
42+
$data = new DataObject($this->loadConfig());
43+
return $data->getData($path) ?: [];
44+
}
45+
46+
/**
47+
* Loads config from environment variables.
48+
*
49+
* @return array
50+
*/
51+
private function loadConfig()
52+
{
53+
$config = [];
54+
55+
$environmentVariables = $_ENV;
56+
57+
foreach ($environmentVariables as $template => $value) {
58+
if (!$this->placeholder->isApplicable($template)) {
59+
continue;
60+
}
61+
62+
$config = $this->arrayManager->set(
63+
$this->placeholder->restore($template),
64+
$config,
65+
$value
66+
);
67+
}
68+
69+
return $config;
70+
}
71+
}

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
class ValueProcessor
2020
{
2121
/**
22-
* System configuration structure.
22+
* System configuration structure factory.
2323
*
24-
* @var Structure
24+
* @var StructureFactory
2525
*/
26-
private $configStructure;
26+
private $configStructureFactory;
2727

2828
/**
2929
* Factory of object that implement \Magento\Framework\App\Config\ValueInterface.
@@ -32,6 +32,11 @@ class ValueProcessor
3232
*/
3333
private $configValueFactory;
3434

35+
/**
36+
* @var ScopeInterface
37+
*/
38+
private $scope;
39+
3540
/**
3641
* @param ScopeInterface $scope
3742
* @param StructureFactory $structureFactory
@@ -42,8 +47,8 @@ public function __construct(
4247
StructureFactory $structureFactory,
4348
ValueFactory $valueFactory
4449
) {
45-
$scope->setCurrentScope(Area::AREA_ADMINHTML);
46-
$this->configStructure = $structureFactory->create();
50+
$this->scope = $scope;
51+
$this->configStructureFactory = $structureFactory;
4752
$this->configValueFactory = $valueFactory;
4853
}
4954

@@ -58,10 +63,17 @@ public function __construct(
5863
*/
5964
public function process($scope, $scopeCode, $value, $path)
6065
{
66+
$areaScope = $this->scope->getCurrentScope();
67+
$this->scope->setCurrentScope(Area::AREA_ADMINHTML);
68+
/** @var Structure $configStructure */
69+
$configStructure = $this->configStructureFactory->create();
70+
$this->scope->setCurrentScope($areaScope);
71+
6172
/** @var Field $field */
62-
$field = $this->configStructure->getElement($path);
73+
$field = $configStructure->getElement($path);
74+
6375
/** @var Value $backendModel */
64-
$backendModel = $field->hasBackendModel()
76+
$backendModel = $field && $field->hasBackendModel()
6577
? $field->getBackendModel()
6678
: $this->configValueFactory->create();
6779
$backendModel->setPath($path);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@
201201
<item name="source" xsi:type="object">systemConfigInitialDataProvider</item>
202202
<item name="sortOrder" xsi:type="string">1000</item>
203203
</item>
204+
<item name="env" xsi:type="array">
205+
<item name="source" xsi:type="object">Magento\Config\App\Config\Source\EnvironmentConfigSource</item>
206+
<item name="sortOrder" xsi:type="string">2000</item>
207+
</item>
204208
</argument>
205209
</arguments>
206210
</virtualType>

0 commit comments

Comments
 (0)