Skip to content

Commit 4d36a95

Browse files
author
Bohdan Korablov
committed
Merge remote-tracking branch 'falcons/MAGETWO-61610' into MAGETWO-63936-2
2 parents f88a510 + cbe16b8 commit 4d36a95

File tree

24 files changed

+1933
-53
lines changed

24 files changed

+1933
-53
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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\ConfigSet;
7+
8+
use Magento\Config\Console\Command\ConfigSetCommand;
9+
use Magento\Framework\Exception\ConfigurationMismatchException;
10+
use Magento\Framework\ObjectManagerInterface;
11+
12+
/**
13+
* Creates different implementations of config:set processors of type ConfigSetProcessorInterface.
14+
*
15+
* @see ConfigSetProcessorInterface
16+
* @see ConfigSetCommand
17+
*/
18+
class ConfigSetProcessorFactory
19+
{
20+
/**#@+
21+
* Constants for processors.
22+
*
23+
* default - save configuration
24+
* lock - save and lock configuration
25+
*/
26+
const TYPE_DEFAULT = 'default';
27+
const TYPE_LOCK = 'lock';
28+
/**#@-*/
29+
30+
/**
31+
* @var ObjectManagerInterface
32+
*/
33+
private $objectManager;
34+
35+
/**
36+
* List of class names that implement config:set processors
37+
*
38+
* @var array
39+
* @see ConfigSetProcessorInterface
40+
*/
41+
private $processors;
42+
43+
/**
44+
* @param ObjectManagerInterface $objectManager
45+
* @param array $processors
46+
*/
47+
public function __construct(
48+
ObjectManagerInterface $objectManager,
49+
array $processors = []
50+
) {
51+
$this->objectManager = $objectManager;
52+
$this->processors = $processors;
53+
}
54+
55+
/**
56+
* Creates an instance of specified processor.
57+
*
58+
* @param string $processorName The name of processor
59+
* @return ConfigSetProcessorInterface New processor instance
60+
* @throws ConfigurationMismatchException If processor type is not exists in processors array
61+
* or declared class has wrong implementation
62+
*/
63+
public function create($processorName)
64+
{
65+
if (!isset($this->processors[$processorName])) {
66+
throw new ConfigurationMismatchException(__('Class for type "%1" was not declared', $processorName));
67+
}
68+
69+
$object = $this->objectManager->create($this->processors[$processorName]);
70+
71+
if (!$object instanceof ConfigSetProcessorInterface) {
72+
throw new ConfigurationMismatchException(
73+
__('%1 should implement %2', get_class($object), ConfigSetProcessorInterface::class)
74+
);
75+
}
76+
77+
return $object;
78+
}
79+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\ConfigSet;
7+
8+
use Magento\Config\Console\Command\ConfigSetCommand;
9+
use Magento\Framework\Exception\CouldNotSaveException;
10+
11+
/**
12+
* Allows to process different flows of config:set command.
13+
*
14+
* @see ConfigSetCommand
15+
*/
16+
interface ConfigSetProcessorInterface
17+
{
18+
/**
19+
* Processes config:set command.
20+
*
21+
* @param string $path The configuration path in format group/section/field_name
22+
* @param string $value The configuration value
23+
* @param string $scope The configuration scope (default, website, or store)
24+
* @param string $scopeCode The scope code
25+
* @return void
26+
* @throws CouldNotSaveException An exception on processing error
27+
*/
28+
public function process($path, $value, $scope, $scopeCode);
29+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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\ConfigSet;
7+
8+
use Magento\Config\App\Config\Type\System;
9+
use Magento\Config\Console\Command\ConfigSetCommand;
10+
use Magento\Config\Model\Config;
11+
use Magento\Config\Model\ConfigFactory;
12+
use Magento\Framework\App\Config\ConfigPathResolver;
13+
use Magento\Framework\App\DeploymentConfig;
14+
use Magento\Framework\Exception\CouldNotSaveException;
15+
use Magento\Store\Model\ScopeInterface;
16+
17+
/**
18+
* Processes default flow of config:set command.
19+
* This processor saves the value of configuration into database.
20+
*
21+
* {@inheritdoc}
22+
*/
23+
class DefaultProcessor implements ConfigSetProcessorInterface
24+
{
25+
/**
26+
* The factory that creates config model instances.
27+
*
28+
* @see Config
29+
* @var ConfigFactory
30+
*/
31+
private $configFactory;
32+
33+
/**
34+
* The deployment configuration reader.
35+
*
36+
* @var DeploymentConfig
37+
*/
38+
private $deploymentConfig;
39+
40+
/**
41+
* The resolver for configuration paths according to source type.
42+
*
43+
* @var ConfigPathResolver
44+
*/
45+
private $configPathResolver;
46+
47+
/**
48+
* @param ConfigFactory $configFactory The factory that creates config model instances
49+
* @param DeploymentConfig $deploymentConfig The deployment configuration reader
50+
* @param ConfigPathResolver $configPathResolver The resolver for configuration paths according to source type
51+
*/
52+
public function __construct(
53+
ConfigFactory $configFactory,
54+
DeploymentConfig $deploymentConfig,
55+
ConfigPathResolver $configPathResolver
56+
) {
57+
$this->configFactory = $configFactory;
58+
$this->deploymentConfig = $deploymentConfig;
59+
$this->configPathResolver = $configPathResolver;
60+
}
61+
62+
/**
63+
* Processes database flow of config:set command.
64+
* Requires installed application.
65+
*
66+
* {@inheritdoc}
67+
*/
68+
public function process($path, $value, $scope, $scopeCode)
69+
{
70+
if (!$this->deploymentConfig->isAvailable()) {
71+
throw new CouldNotSaveException(
72+
__(
73+
'We can\'t save this option because Magento is not installed. '
74+
. 'To lock this value, enter the command again using the --%1 option.',
75+
ConfigSetCommand::OPTION_LOCK
76+
)
77+
);
78+
}
79+
80+
if ($this->isLocked($path, $scope, $scopeCode)) {
81+
throw new CouldNotSaveException(
82+
__(
83+
'The value you set has already been locked. To change the value, use the --%1 option.',
84+
ConfigSetCommand::OPTION_LOCK
85+
)
86+
);
87+
}
88+
89+
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);
98+
}
99+
100+
$config->save();
101+
} catch (\Exception $exception) {
102+
throw new CouldNotSaveException(__('%1', $exception->getMessage()), $exception);
103+
}
104+
}
105+
106+
/**
107+
* Checks whether configuration is locked in file storage.
108+
*
109+
* @param string $path The path to configuration
110+
* @param string $scope The scope of configuration
111+
* @param string $scopeCode The scope code of configuration
112+
* @return bool
113+
*/
114+
private function isLocked($path, $scope, $scopeCode)
115+
{
116+
$scopePath = $this->configPathResolver->resolve($path, $scope, $scopeCode, System::CONFIG_TYPE);
117+
118+
return $this->deploymentConfig->get($scopePath) !== null;
119+
}
120+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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\ConfigSet;
7+
8+
use Magento\Config\App\Config\Type\System;
9+
use Magento\Config\Model\Config\Structure;
10+
use Magento\Framework\App\Config\ConfigPathResolver;
11+
use Magento\Framework\App\Config\Value;
12+
use Magento\Framework\App\Config\ValueFactory;
13+
use Magento\Framework\App\DeploymentConfig;
14+
use Magento\Framework\Config\File\ConfigFilePool;
15+
use Magento\Framework\Exception\CouldNotSaveException;
16+
use Magento\Framework\Stdlib\ArrayManager;
17+
18+
/**
19+
* Processes file lock flow of config:set command.
20+
* This processor saves the value of configuration and lock it for editing in Admin interface.
21+
*
22+
* {@inheritdoc}
23+
*/
24+
class LockProcessor implements ConfigSetProcessorInterface
25+
{
26+
/**
27+
* The deployment configuration reader.
28+
*
29+
* @var DeploymentConfig
30+
*/
31+
private $deploymentConfig;
32+
33+
/**
34+
* The deployment configuration writer.
35+
*
36+
* @var DeploymentConfig\Writer
37+
*/
38+
private $deploymentConfigWriter;
39+
40+
/**
41+
* An array manager for different manipulations with arrays.
42+
*
43+
* @var ArrayManager
44+
*/
45+
private $arrayManager;
46+
47+
/**
48+
* The resolver for configuration paths according to source type.
49+
*
50+
* @var ConfigPathResolver
51+
*/
52+
private $configPathResolver;
53+
54+
/**
55+
* The manager for system configuration structure.
56+
*
57+
* @var Structure
58+
*/
59+
private $configStructure;
60+
61+
/**
62+
* The factory for configuration value objects.
63+
*
64+
* @see Value
65+
* @var ValueFactory
66+
*/
67+
private $configValueFactory;
68+
69+
/**
70+
* @param DeploymentConfig $deploymentConfig The deployment configuration reader
71+
* @param DeploymentConfig\Writer $writer The deployment configuration writer
72+
* @param ArrayManager $arrayManager An array manager for different manipulations with arrays
73+
* @param ConfigPathResolver $configPathResolver The resolver for configuration paths according to source type
74+
* @param Structure $configStructure The manager for system configuration structure
75+
* @param ValueFactory $configValueFactory The factory for configuration value objects
76+
*/
77+
public function __construct(
78+
DeploymentConfig $deploymentConfig,
79+
DeploymentConfig\Writer $writer,
80+
ArrayManager $arrayManager,
81+
ConfigPathResolver $configPathResolver,
82+
Structure $configStructure,
83+
ValueFactory $configValueFactory
84+
) {
85+
$this->deploymentConfig = $deploymentConfig;
86+
$this->deploymentConfigWriter = $writer;
87+
$this->arrayManager = $arrayManager;
88+
$this->configPathResolver = $configPathResolver;
89+
$this->configStructure = $configStructure;
90+
$this->configValueFactory = $configValueFactory;
91+
}
92+
93+
/**
94+
* Processes lock flow of config:set command.
95+
* Requires read access to filesystem.
96+
*
97+
* {@inheritdoc}
98+
*/
99+
public function process($path, $value, $scope, $scopeCode)
100+
{
101+
try {
102+
$configPath = $this->configPathResolver->resolve($path, $scope, $scopeCode, System::CONFIG_TYPE);
103+
/** @var Structure\Element\Field $field */
104+
$field = $this->deploymentConfig->isAvailable()
105+
? $this->configStructure->getElement($path)
106+
: null;
107+
/** @var Value $backendModel */
108+
$backendModel = $field && $field->hasBackendModel()
109+
? $field->getBackendModel()
110+
: $this->configValueFactory->create();
111+
112+
$backendModel->setPath($path);
113+
$backendModel->setScope($scope);
114+
$backendModel->setScopeId($scopeCode);
115+
$backendModel->setValue($value);
116+
117+
/**
118+
* Temporary solution until Magento introduce unified interface
119+
* for storing system configuration into database and configuration files.
120+
*/
121+
$backendModel->validateBeforeSave();
122+
$backendModel->beforeSave();
123+
124+
$this->deploymentConfigWriter->saveConfig(
125+
[ConfigFilePool::APP_CONFIG => $this->arrayManager->set($configPath, [], $backendModel->getValue())],
126+
false
127+
);
128+
129+
$backendModel->afterSave();
130+
} catch (\Exception $exception) {
131+
throw new CouldNotSaveException(__('%1', $exception->getMessage()), $exception);
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)