Skip to content

Commit 974e7ba

Browse files
author
Yaroslav Onischenko
authored
Merge pull request #1031 from magento-falcons/MAGETWO-67357
[Falcons] Delivery of deployment improvements
2 parents d5f2939 + a6d5beb commit 974e7ba

File tree

19 files changed

+555
-167
lines changed

19 files changed

+555
-167
lines changed

app/code/Magento/Config/Console/Command/ConfigSet/DefaultProcessor.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
use Magento\Config\App\Config\Type\System;
99
use Magento\Config\Console\Command\ConfigSetCommand;
10-
use Magento\Config\Model\Config;
11-
use Magento\Config\Model\ConfigFactory;
1210
use Magento\Framework\App\Config\ConfigPathResolver;
1311
use Magento\Framework\App\DeploymentConfig;
1412
use Magento\Framework\Exception\CouldNotSaveException;
15-
use Magento\Store\Model\ScopeInterface;
13+
use Magento\Config\Model\PreparedValueFactory;
14+
use Magento\Framework\App\Config\Value;
1615

1716
/**
1817
* Processes default flow of config:set command.
@@ -22,14 +21,6 @@
2221
*/
2322
class DefaultProcessor implements ConfigSetProcessorInterface
2423
{
25-
/**
26-
* The factory that creates config model instances.
27-
*
28-
* @see Config
29-
* @var ConfigFactory
30-
*/
31-
private $configFactory;
32-
3324
/**
3425
* The deployment configuration reader.
3526
*
@@ -45,16 +36,23 @@ class DefaultProcessor implements ConfigSetProcessorInterface
4536
private $configPathResolver;
4637

4738
/**
48-
* @param ConfigFactory $configFactory The factory that creates config model instances
39+
* The factory for prepared value.
40+
*
41+
* @var PreparedValueFactory
42+
*/
43+
private $preparedValueFactory;
44+
45+
/**
46+
* @param PreparedValueFactory $preparedValueFactory The factory for prepared value
4947
* @param DeploymentConfig $deploymentConfig The deployment configuration reader
5048
* @param ConfigPathResolver $configPathResolver The resolver for configuration paths according to source type
5149
*/
5250
public function __construct(
53-
ConfigFactory $configFactory,
51+
PreparedValueFactory $preparedValueFactory,
5452
DeploymentConfig $deploymentConfig,
5553
ConfigPathResolver $configPathResolver
5654
) {
57-
$this->configFactory = $configFactory;
55+
$this->preparedValueFactory = $preparedValueFactory;
5856
$this->deploymentConfig = $deploymentConfig;
5957
$this->configPathResolver = $configPathResolver;
6058
}
@@ -87,17 +85,12 @@ public function process($path, $value, $scope, $scopeCode)
8785
}
8886

8987
try {
90-
/** @var Config $config */
91-
$config = $this->configFactory->create();
92-
$config->setDataByPath($path, $value);
93-
94-
if (in_array($scope, [ScopeInterface::SCOPE_WEBSITE, ScopeInterface::SCOPE_WEBSITES])) {
95-
$config->setWebsite($scopeCode);
96-
} elseif (in_array($scope, [ScopeInterface::SCOPE_STORE, ScopeInterface::SCOPE_STORES])) {
97-
$config->setStore($scopeCode);
88+
/** @var Value $backendModel */
89+
$backendModel = $this->preparedValueFactory->create($path, $value, $scope, $scopeCode);
90+
if ($backendModel instanceof Value) {
91+
$resourceModel = $backendModel->getResource();
92+
$resourceModel->save($backendModel);
9893
}
99-
100-
$config->save();
10194
} catch (\Exception $exception) {
10295
throw new CouldNotSaveException(__('%1', $exception->getMessage()), $exception);
10396
}

app/code/Magento/Config/Console/Command/ConfigSet/LockProcessor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,28 @@
2222
class LockProcessor implements ConfigSetProcessorInterface
2323
{
2424
/**
25-
* The factory for prepared value.
25+
* The factory for prepared value
2626
*
2727
* @var PreparedValueFactory
2828
*/
2929
private $preparedValueFactory;
3030

3131
/**
32-
* The deployment configuration writer.
32+
* The deployment configuration writer
3333
*
3434
* @var DeploymentConfig\Writer
3535
*/
3636
private $deploymentConfigWriter;
3737

3838
/**
39-
* An array manager for different manipulations with arrays.
39+
* An array manager for different manipulations with arrays
4040
*
4141
* @var ArrayManager
4242
*/
4343
private $arrayManager;
4444

4545
/**
46-
* The resolver for configuration paths according to source type.
46+
* The resolver for configuration paths according to source type
4747
*
4848
* @var ConfigPathResolver
4949
*/

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\App\Config\Value;
1414
use Magento\Framework\App\Config\ValueFactory;
1515
use Magento\Framework\Config\ScopeInterface;
16+
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
1617

1718
/**
1819
* Class processes values using backend model which declared in system.xml.
@@ -46,18 +47,29 @@ class ValueProcessor
4647
private $scope;
4748

4849
/**
49-
* @param ScopeInterface $scope
50-
* @param StructureFactory $structureFactory
51-
* @param ValueFactory $valueFactory
50+
* The json serializer.
51+
*
52+
* @var JsonSerializer
53+
*/
54+
private $jsonSerializer;
55+
56+
/**
57+
* @param ScopeInterface $scope The object for managing configuration scope
58+
* @param StructureFactory $structureFactory The system configuration structure factory.
59+
* @param ValueFactory $valueFactory The factory of object that
60+
* implement \Magento\Framework\App\Config\ValueInterface
61+
* @param JsonSerializer $jsonSerializer The json serializer
5262
*/
5363
public function __construct(
5464
ScopeInterface $scope,
5565
StructureFactory $structureFactory,
56-
ValueFactory $valueFactory
66+
ValueFactory $valueFactory,
67+
JsonSerializer $jsonSerializer
5768
) {
5869
$this->scope = $scope;
5970
$this->configStructureFactory = $structureFactory;
6071
$this->configValueFactory = $valueFactory;
72+
$this->jsonSerializer = $jsonSerializer;
6173
}
6274

6375
/**
@@ -78,7 +90,7 @@ public function process($scope, $scopeCode, $value, $path)
7890
$this->scope->setCurrentScope($areaScope);
7991

8092
/** @var Field $field */
81-
$field = $configStructure->getElement($path);
93+
$field = $configStructure->getElementByConfigPath($path);
8294

8395
/** @var Value $backendModel */
8496
$backendModel = $field && $field->hasBackendModel()
@@ -94,7 +106,12 @@ public function process($scope, $scopeCode, $value, $path)
94106
$backendModel->setScopeId($scopeCode);
95107
$backendModel->setValue($value);
96108
$backendModel->afterLoad();
109+
$processedValue = $backendModel->getValue();
97110

98-
return $backendModel->getValue();
111+
/**
112+
* If $processedValue is array it means that $value is json array (string).
113+
* It should be converted to string for displaying.
114+
*/
115+
return is_array($processedValue) ? $this->jsonSerializer->serialize($processedValue) : $processedValue;
99116
}
100117
}

app/code/Magento/Config/Model/Config/Structure.php

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,37 @@
77

88
/**
99
* System configuration structure.
10+
*
11+
* All paths are declared in module's system.xml.
12+
*
13+
* ```xml
14+
* <section id="section_id">
15+
* <group id="group_id" ...>
16+
* <field id="field_one_id" ...>
17+
* <label>Field One</label>
18+
* ...
19+
* </field>
20+
* <field id="field_two_id" ...>
21+
* <label>Field Two</label>
22+
* <config_path>section/group/field</config_path>
23+
* ...
24+
* </field>
25+
* </group>
26+
* </section>
27+
* ```
28+
*
29+
* Structure path is the nested path of node ids (section, group, field).
30+
*
31+
* Config path is the path which is declared in <config_path> node.
32+
* If this node is not provided then config path is the same as structure path.
33+
*
34+
* With the example above you can see that the field <field id="field_one_id"> has the next paths:
35+
* - the structure path section_id/group_id/field_one_id
36+
* - the configuration path section_id/group_id/field_one_id
37+
*
38+
* Also you can see that the field <field id="field_two_id"> has the next paths:
39+
* - the structure path section_id/group_id/field_two_id
40+
* - the configuration path section/group/field
1041
*/
1142
class Structure implements \Magento\Config\Model\Config\Structure\SearchInterface
1243
{
@@ -57,6 +88,24 @@ class Structure implements \Magento\Config\Model\Config\Structure\SearchInterfac
5788
*/
5889
protected $sectionList;
5990

91+
/**
92+
* Collects config paths and their structure paths from configuration files
93+
*
94+
* For example:
95+
* ```php
96+
* [
97+
* 'section_id/group_id/field_one_id' => [
98+
* 'section_id/group_id/field_one_id'
99+
* ],
100+
* 'section/group/field' => [
101+
* 'section_id/group_id/field_two_id'
102+
* ]
103+
* ```
104+
*
105+
* @var array
106+
*/
107+
private $mappedPaths;
108+
60109
/**
61110
* @param \Magento\Config\Model\Config\Structure\Data $structureData
62111
* @param \Magento\Config\Model\Config\Structure\Element\Iterator\Tab $tabIterator
@@ -114,16 +163,33 @@ public function getSectionList()
114163
}
115164

116165
/**
117-
* Find element by path
166+
* Find element by structure path
118167
*
119-
* @param string $path
168+
* @param string $path The structure path
120169
* @return \Magento\Config\Model\Config\Structure\ElementInterface|null
121170
*/
122171
public function getElement($path)
123172
{
124173
return $this->getElementByPathParts(explode('/', $path));
125174
}
126175

176+
/**
177+
* Find element by config path
178+
*
179+
* @param string $path The configuration path
180+
* @return \Magento\Config\Model\Config\Structure\ElementInterface|null
181+
*/
182+
public function getElementByConfigPath($path)
183+
{
184+
$allPaths = $this->getFieldPaths();
185+
186+
if (isset($allPaths[$path])) {
187+
$path = array_shift($allPaths[$path]);
188+
}
189+
190+
return $this->getElementByPathParts(explode('/', $path));
191+
}
192+
127193
/**
128194
* Retrieve first available section in config structure
129195
*
@@ -291,7 +357,11 @@ public function getFieldPaths()
291357
{
292358
$sections = !empty($this->_data['sections']) ? $this->_data['sections'] : [];
293359

294-
return $this->getFieldsRecursively($sections);
360+
if (!$this->mappedPaths) {
361+
$this->mappedPaths = $this->getFieldsRecursively($sections);
362+
}
363+
364+
return $this->mappedPaths;
295365
}
296366

297367
/**

app/code/Magento/Config/Model/PreparedValueFactory.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\App\Config\ValueInterface;
1313
use Magento\Framework\App\Config\Value;
1414
use Magento\Framework\App\ScopeInterface;
15+
use Magento\Store\Model\ScopeInterface as StoreScopeInterface;
1516
use Magento\Framework\App\ScopeResolverPool;
1617
use Magento\Framework\Exception\RuntimeException;
1718

@@ -87,7 +88,7 @@ public function create($path, $value, $scope, $scopeCode = null)
8788
/** @var Structure $structure */
8889
$structure = $this->structureFactory->create();
8990
/** @var Structure\ElementInterface $field */
90-
$field = $structure->getElement($path);
91+
$field = $structure->getElementByConfigPath($path);
9192
$configPath = $path;
9293
/** @var string $backendModelName */
9394
if ($field instanceof Structure\Element\Field && $field->hasBackendModel()) {
@@ -105,6 +106,12 @@ public function create($path, $value, $scope, $scopeCode = null)
105106
if ($backendModel instanceof Value) {
106107
$scopeId = 0;
107108

109+
if (in_array($scope, [StoreScopeInterface::SCOPE_WEBSITE, StoreScopeInterface::SCOPE_WEBSITES])) {
110+
$scope = StoreScopeInterface::SCOPE_WEBSITES;
111+
} elseif (in_array($scope, [StoreScopeInterface::SCOPE_STORE, StoreScopeInterface::SCOPE_STORES])) {
112+
$scope = StoreScopeInterface::SCOPE_STORES;
113+
}
114+
108115
if ($scope !== ScopeInterface::SCOPE_DEFAULT) {
109116
$scopeResolver = $this->scopeResolverPool->get($scope);
110117
$scopeId = $scopeResolver->getScope($scopeCode)->getId();

0 commit comments

Comments
 (0)