Skip to content

Commit 04ce39f

Browse files
author
Oleksii Korshenko
authored
ENGCOM-585: [Forwardport] Add option "lock-config" for shell command "config:set" #13832
2 parents 6297fe8 + 24d7bb8 commit 04ce39f

File tree

16 files changed

+426
-55
lines changed

16 files changed

+426
-55
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ class ConfigSetProcessorFactory
2828
* lock - save and lock configuration
2929
*/
3030
const TYPE_DEFAULT = 'default';
31+
32+
/**
33+
* @deprecated
34+
* @see TYPE_LOCK_ENV or TYPE_LOCK_CONFIG
35+
*/
3136
const TYPE_LOCK = 'lock';
37+
const TYPE_LOCK_ENV = 'lock-env';
38+
const TYPE_LOCK_CONFIG = 'lock-config';
3239
/**#@-*/
3340

3441
/**#@-*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function process($path, $value, $scope, $scopeCode)
7272
throw new CouldNotSaveException(
7373
__(
7474
'The value you set has already been locked. To change the value, use the --%1 option.',
75-
ConfigSetCommand::OPTION_LOCK
75+
ConfigSetCommand::OPTION_LOCK_ENV
7676
)
7777
);
7878
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
/**
1818
* Processes file lock flow of config:set command.
19-
* This processor saves the value of configuration and lock it for editing in Admin interface.
19+
* This processor saves the value of configuration into app/etc/env.php
20+
* and locks it for editing in Admin interface.
2021
*
2122
* {@inheritdoc}
2223
*/
@@ -49,23 +50,30 @@ class LockProcessor implements ConfigSetProcessorInterface
4950
* @var ConfigPathResolver
5051
*/
5152
private $configPathResolver;
53+
/**
54+
* @var string
55+
*/
56+
private $target;
5257

5358
/**
5459
* @param PreparedValueFactory $preparedValueFactory The factory for prepared value
5560
* @param DeploymentConfig\Writer $writer The deployment configuration writer
5661
* @param ArrayManager $arrayManager An array manager for different manipulations with arrays
5762
* @param ConfigPathResolver $configPathResolver The resolver for configuration paths according to source type
63+
* @param string $target
5864
*/
5965
public function __construct(
6066
PreparedValueFactory $preparedValueFactory,
6167
DeploymentConfig\Writer $writer,
6268
ArrayManager $arrayManager,
63-
ConfigPathResolver $configPathResolver
69+
ConfigPathResolver $configPathResolver,
70+
$target = ConfigFilePool::APP_ENV
6471
) {
6572
$this->preparedValueFactory = $preparedValueFactory;
6673
$this->deploymentConfigWriter = $writer;
6774
$this->arrayManager = $arrayManager;
6875
$this->configPathResolver = $configPathResolver;
76+
$this->target = $target;
6977
}
7078

7179
/**
@@ -97,7 +105,7 @@ public function process($path, $value, $scope, $scopeCode)
97105
* we'll write value just after all validations are triggered.
98106
*/
99107
$this->deploymentConfigWriter->saveConfig(
100-
[ConfigFilePool::APP_ENV => $this->arrayManager->set($configPath, [], $value)],
108+
[$this->target => $this->arrayManager->set($configPath, [], $value)],
101109
false
102110
);
103111
}

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\App\Config\ScopeConfigInterface;
1010
use Magento\Framework\App\Scope\ValidatorInterface;
1111
use Magento\Config\Model\Config\PathValidator;
12+
use Magento\Framework\Config\File\ConfigFilePool;
1213
use Magento\Framework\Exception\LocalizedException;
1314
use Magento\Framework\Exception\ConfigurationMismatchException;
1415
use Magento\Framework\Exception\CouldNotSaveException;
@@ -98,27 +99,60 @@ public function __construct(
9899
* @param boolean $lock The lock flag
99100
* @return string Processor response message
100101
* @throws ValidatorException If some validation is wrong
101-
* @throws CouldNotSaveException If cannot save config value
102-
* @throws ConfigurationMismatchException If processor can not be instantiated
103102
* @since 100.2.0
103+
* @deprecated
104+
* @see processWithLockTarget()
104105
*/
105106
public function process($path, $value, $scope, $scopeCode, $lock)
106107
{
108+
return $this->processWithLockTarget($path, $value, $scope, $scopeCode, $lock);
109+
}
110+
111+
/**
112+
* Processes config:set command with the option to set a target file.
113+
*
114+
* @param string $path The configuration path in format section/group/field_name
115+
* @param string $value The configuration value
116+
* @param string $scope The configuration scope (default, website, or store)
117+
* @param string $scopeCode The scope code
118+
* @param boolean $lock The lock flag
119+
* @param string $lockTarget
120+
* @return string Processor response message
121+
* @throws ValidatorException If some validation is wrong
122+
*/
123+
public function processWithLockTarget(
124+
$path,
125+
$value,
126+
$scope,
127+
$scopeCode,
128+
$lock,
129+
$lockTarget = ConfigFilePool::APP_ENV
130+
) {
107131
try {
108132
$this->scopeValidator->isValid($scope, $scopeCode);
109133
$this->pathValidator->validate($path);
110134
} catch (LocalizedException $exception) {
111135
throw new ValidatorException(__($exception->getMessage()), $exception);
112136
}
113137

114-
$processor = $lock
115-
? $this->configSetProcessorFactory->create(ConfigSetProcessorFactory::TYPE_LOCK)
116-
: $this->configSetProcessorFactory->create(ConfigSetProcessorFactory::TYPE_DEFAULT);
117-
$message = $lock
118-
? 'Value was saved and locked.'
119-
: 'Value was saved.';
138+
$processor =
139+
$lock
140+
? ( $lockTarget == ConfigFilePool::APP_ENV
141+
? $this->configSetProcessorFactory->create(ConfigSetProcessorFactory::TYPE_LOCK_ENV)
142+
: $this->configSetProcessorFactory->create(ConfigSetProcessorFactory::TYPE_LOCK_CONFIG)
143+
)
144+
: $this->configSetProcessorFactory->create(ConfigSetProcessorFactory::TYPE_DEFAULT)
145+
;
146+
147+
$message =
148+
$lock
149+
? ( $lockTarget == ConfigFilePool::APP_ENV
150+
? 'Value was saved in app/etc/env.php and locked.'
151+
: 'Value was saved in app/etc/config.php and locked.'
152+
)
153+
: 'Value was saved.';
120154

121-
// The processing flow depends on --lock option.
155+
// The processing flow depends on --lock and --share options.
122156
$processor->process($path, $value, $scope, $scopeCode);
123157

124158
$this->hash->regenerate(System::CONFIG_TYPE);

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Config\Console\Command;
78

89
use Magento\Config\App\Config\Type\System;
910
use Magento\Config\Console\Command\ConfigSet\ProcessorFacadeFactory;
1011
use Magento\Deploy\Model\DeploymentConfig\ChangeDetector;
1112
use Magento\Framework\App\DeploymentConfig;
1213
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Framework\Config\File\ConfigFilePool;
1315
use Magento\Framework\Console\Cli;
1416
use Symfony\Component\Console\Command\Command;
1517
use Symfony\Component\Console\Input\InputArgument;
@@ -34,6 +36,8 @@ class ConfigSetCommand extends Command
3436
const OPTION_SCOPE = 'scope';
3537
const OPTION_SCOPE_CODE = 'scope-code';
3638
const OPTION_LOCK = 'lock';
39+
const OPTION_LOCK_ENV = 'lock-env';
40+
const OPTION_LOCK_CONFIG = 'lock-config';
3741
/**#@-*/
3842

3943
/**#@-*/
@@ -108,11 +112,24 @@ protected function configure()
108112
InputArgument::OPTIONAL,
109113
'Scope code (required only if scope is not \'default\')'
110114
),
115+
new InputOption(
116+
static::OPTION_LOCK_ENV,
117+
'le',
118+
InputOption::VALUE_NONE,
119+
'Lock value which prevents modification in the Admin (will be saved in app/etc/env.php)'
120+
),
121+
new InputOption(
122+
static::OPTION_LOCK_CONFIG,
123+
'lc',
124+
InputOption::VALUE_NONE,
125+
'Lock and share value with other installations, prevents modification in the Admin '
126+
. '(will be saved in app/etc/config.php)'
127+
),
111128
new InputOption(
112129
static::OPTION_LOCK,
113130
'l',
114131
InputOption::VALUE_NONE,
115-
'Lock value which prevents modification in the Admin'
132+
'Deprecated, use the --' . static::OPTION_LOCK_ENV . ' option instead.'
116133
),
117134
]);
118135

@@ -146,12 +163,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
146163

147164
try {
148165
$message = $this->emulatedAreaProcessor->process(function () use ($input) {
149-
return $this->processorFacadeFactory->create()->process(
166+
167+
$lock = $input->getOption(static::OPTION_LOCK_ENV)
168+
|| $input->getOption(static::OPTION_LOCK_CONFIG)
169+
|| $input->getOption(static::OPTION_LOCK);
170+
171+
$lockTargetPath = ConfigFilePool::APP_ENV;
172+
if ($input->getOption(static::OPTION_LOCK_CONFIG)) {
173+
$lockTargetPath = ConfigFilePool::APP_CONFIG;
174+
}
175+
176+
return $this->processorFacadeFactory->create()->processWithLockTarget(
150177
$input->getArgument(static::ARG_PATH),
151178
$input->getArgument(static::ARG_VALUE),
152179
$input->getOption(static::OPTION_SCOPE),
153180
$input->getOption(static::OPTION_SCOPE_CODE),
154-
$input->getOption(static::OPTION_LOCK)
181+
$lock,
182+
$lockTargetPath
155183
);
156184
});
157185

app/code/Magento/Config/Test/Unit/Console/Command/ConfigSet/ConfigSetProcessorFactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function setUp()
4141
$this->model = new ConfigSetProcessorFactory(
4242
$this->objectManagerMock,
4343
[
44-
ConfigSetProcessorFactory::TYPE_LOCK => LockProcessor::class,
44+
ConfigSetProcessorFactory::TYPE_LOCK_ENV => LockProcessor::class,
4545
ConfigSetProcessorFactory::TYPE_DEFAULT => DefaultProcessor::class,
4646
'wrongType' => \stdClass::class,
4747
]
@@ -59,7 +59,7 @@ public function testCreate()
5959

6060
$this->assertInstanceOf(
6161
ConfigSetProcessorInterface::class,
62-
$this->model->create(ConfigSetProcessorFactory::TYPE_LOCK)
62+
$this->model->create(ConfigSetProcessorFactory::TYPE_LOCK_ENV)
6363
);
6464
}
6565

app/code/Magento/Config/Test/Unit/Console/Command/ConfigSet/DefaultProcessorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ private function configMockForProcessTest($path, $scope, $scopeCode)
166166

167167
/**
168168
* @expectedException \Magento\Framework\Exception\LocalizedException
169-
* @expectedExceptionMessage The value you set has already been locked. To change the value, use the --lock option.
169+
* @codingStandardsIgnoreStart
170+
* @expectedExceptionMessage The value you set has already been locked. To change the value, use the --lock-env option.
171+
* @codingStandardsIgnoreEnd
170172
*/
171173
public function testProcessLockedValue()
172174
{

0 commit comments

Comments
 (0)