Skip to content

Commit b0a1ef6

Browse files
committed
Merge remote-tracking branch 'tango/MAGETWO-97247' into TANGO-PR-2019-2.3
2 parents 3af00b3 + baab5fd commit b0a1ef6

File tree

11 files changed

+302
-208
lines changed

11 files changed

+302
-208
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77

88
use Magento\Config\App\Config\Type\System;
99
use Magento\Config\Console\Command\ConfigSetCommand;
10+
use Magento\Config\Model\Config\Factory as ConfigFactory;
1011
use Magento\Framework\App\Config\ConfigPathResolver;
1112
use Magento\Framework\App\DeploymentConfig;
13+
use Magento\Framework\App\ObjectManager;
1214
use Magento\Framework\Exception\CouldNotSaveException;
1315
use Magento\Config\Model\PreparedValueFactory;
14-
use Magento\Framework\App\Config\Value;
1516

1617
/**
1718
* Processes default flow of config:set command.
19+
*
1820
* This processor saves the value of configuration into database.
1921
*
20-
* {@inheritdoc}
22+
* @inheritdoc
2123
* @api
2224
* @since 100.2.0
2325
*/
@@ -44,26 +46,36 @@ class DefaultProcessor implements ConfigSetProcessorInterface
4446
*/
4547
private $preparedValueFactory;
4648

49+
/**
50+
* @var ConfigFactory
51+
*/
52+
private $configFactory;
53+
4754
/**
4855
* @param PreparedValueFactory $preparedValueFactory The factory for prepared value
4956
* @param DeploymentConfig $deploymentConfig The deployment configuration reader
5057
* @param ConfigPathResolver $configPathResolver The resolver for configuration paths according to source type
58+
* @param ConfigFactory|null $configFactory
5159
*/
5260
public function __construct(
5361
PreparedValueFactory $preparedValueFactory,
5462
DeploymentConfig $deploymentConfig,
55-
ConfigPathResolver $configPathResolver
63+
ConfigPathResolver $configPathResolver,
64+
ConfigFactory $configFactory = null
5665
) {
5766
$this->preparedValueFactory = $preparedValueFactory;
5867
$this->deploymentConfig = $deploymentConfig;
5968
$this->configPathResolver = $configPathResolver;
69+
70+
$this->configFactory = $configFactory ?? ObjectManager::getInstance()->get(ConfigFactory::class);
6071
}
6172

6273
/**
6374
* Processes database flow of config:set command.
75+
*
6476
* Requires installed application.
6577
*
66-
* {@inheritdoc}
78+
* @inheritdoc
6779
* @since 100.2.0
6880
*/
6981
public function process($path, $value, $scope, $scopeCode)
@@ -78,12 +90,12 @@ public function process($path, $value, $scope, $scopeCode)
7890
}
7991

8092
try {
81-
/** @var Value $backendModel */
82-
$backendModel = $this->preparedValueFactory->create($path, $value, $scope, $scopeCode);
83-
if ($backendModel instanceof Value) {
84-
$resourceModel = $backendModel->getResource();
85-
$resourceModel->save($backendModel);
86-
}
93+
$config = $this->configFactory->create([
94+
'scope' => $scope,
95+
'scope_code' => $scopeCode,
96+
]);
97+
$config->setDataByPath($path, $value);
98+
$config->save();
8799
} catch (\Exception $exception) {
88100
throw new CouldNotSaveException(__('%1', $exception->getMessage()), $exception);
89101
}

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

Lines changed: 93 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,32 @@
99
use Magento\Config\Model\Config\Structure\Element\Group;
1010
use Magento\Config\Model\Config\Structure\Element\Field;
1111
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\App\ScopeInterface;
13+
use Magento\Framework\App\ScopeResolverPool;
14+
use Magento\Store\Model\ScopeInterface as StoreScopeInterface;
15+
use Magento\Store\Model\ScopeTypeNormalizer;
1216

1317
/**
1418
* Backend config model
1519
*
1620
* Used to save configuration
21+
*
1722
* @author Magento Core Team <core@magentocommerce.com>
1823
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1924
* @api
2025
* @since 100.0.2
26+
* @method string getSection()
27+
* @method void setSection(string $section)
28+
* @method string getWebsite()
29+
* @method void setWebsite(string $website)
30+
* @method string getStore()
31+
* @method void setStore(string $store)
32+
* @method string getScope()
33+
* @method void setScope(string $scope)
34+
* @method int getScopeId()
35+
* @method void setScopeId(int $scopeId)
36+
* @method string getScopeCode()
37+
* @method void setScopeCode(string $scopeCode)
2138
*/
2239
class Config extends \Magento\Framework\DataObject
2340
{
@@ -87,6 +104,16 @@ class Config extends \Magento\Framework\DataObject
87104
*/
88105
private $settingChecker;
89106

107+
/**
108+
* @var ScopeResolverPool
109+
*/
110+
private $scopeResolverPool;
111+
112+
/**
113+
* @var ScopeTypeNormalizer
114+
*/
115+
private $scopeTypeNormalizer;
116+
90117
/**
91118
* @param \Magento\Framework\App\Config\ReinitableConfigInterface $config
92119
* @param \Magento\Framework\Event\ManagerInterface $eventManager
@@ -97,6 +124,9 @@ class Config extends \Magento\Framework\DataObject
97124
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
98125
* @param Config\Reader\Source\Deployed\SettingChecker|null $settingChecker
99126
* @param array $data
127+
* @param ScopeResolverPool|null $scopeResolverPool
128+
* @param ScopeTypeNormalizer|null $scopeTypeNormalizer
129+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
100130
*/
101131
public function __construct(
102132
\Magento\Framework\App\Config\ReinitableConfigInterface $config,
@@ -107,7 +137,9 @@ public function __construct(
107137
\Magento\Framework\App\Config\ValueFactory $configValueFactory,
108138
\Magento\Store\Model\StoreManagerInterface $storeManager,
109139
SettingChecker $settingChecker = null,
110-
array $data = []
140+
array $data = [],
141+
ScopeResolverPool $scopeResolverPool = null,
142+
ScopeTypeNormalizer $scopeTypeNormalizer = null
111143
) {
112144
parent::__construct($data);
113145
$this->_eventManager = $eventManager;
@@ -117,7 +149,12 @@ public function __construct(
117149
$this->_configLoader = $configLoader;
118150
$this->_configValueFactory = $configValueFactory;
119151
$this->_storeManager = $storeManager;
120-
$this->settingChecker = $settingChecker ?: ObjectManager::getInstance()->get(SettingChecker::class);
152+
$this->settingChecker = $settingChecker
153+
?? ObjectManager::getInstance()->get(SettingChecker::class);
154+
$this->scopeResolverPool = $scopeResolverPool
155+
?? ObjectManager::getInstance()->get(ScopeResolverPool::class);
156+
$this->scopeTypeNormalizer = $scopeTypeNormalizer
157+
?? ObjectManager::getInstance()->get(ScopeTypeNormalizer::class);
121158
}
122159

123160
/**
@@ -505,41 +542,75 @@ public function setDataByPath($path, $value)
505542
}
506543

507544
/**
508-
* Get scope name and scopeId
545+
* Set scope data
509546
*
510-
* @todo refactor to scope resolver
511547
* @return void
512548
*/
513549
private function initScope()
514550
{
515551
if ($this->getSection() === null) {
516552
$this->setSection('');
517553
}
554+
555+
$scope = $this->retrieveScope();
556+
$this->setScope($this->scopeTypeNormalizer->normalize($scope->getScopeType()));
557+
$this->setScopeCode($scope->getCode());
558+
$this->setScopeId($scope->getId());
559+
518560
if ($this->getWebsite() === null) {
519-
$this->setWebsite('');
561+
$this->setWebsite(StoreScopeInterface::SCOPE_WEBSITES === $this->getScope() ? $scope->getId() : '');
520562
}
521563
if ($this->getStore() === null) {
522-
$this->setStore('');
564+
$this->setStore(StoreScopeInterface::SCOPE_STORES === $this->getScope() ? $scope->getId() : '');
523565
}
566+
}
524567

525-
if ($this->getStore()) {
526-
$scope = 'stores';
527-
$store = $this->_storeManager->getStore($this->getStore());
528-
$scopeId = (int)$store->getId();
529-
$scopeCode = $store->getCode();
530-
} elseif ($this->getWebsite()) {
531-
$scope = 'websites';
532-
$website = $this->_storeManager->getWebsite($this->getWebsite());
533-
$scopeId = (int)$website->getId();
534-
$scopeCode = $website->getCode();
568+
/**
569+
* Retrieve scope from initial data
570+
*
571+
* @return ScopeInterface
572+
*/
573+
private function retrieveScope(): ScopeInterface
574+
{
575+
$scopeType = $this->getScope();
576+
if (!$scopeType) {
577+
switch (true) {
578+
case $this->getStore():
579+
$scopeType = StoreScopeInterface::SCOPE_STORES;
580+
$scopeIdentifier = $this->getStore();
581+
break;
582+
case $this->getWebsite():
583+
$scopeType = StoreScopeInterface::SCOPE_WEBSITES;
584+
$scopeIdentifier = $this->getWebsite();
585+
break;
586+
default:
587+
$scopeType = ScopeInterface::SCOPE_DEFAULT;
588+
$scopeIdentifier = null;
589+
break;
590+
}
535591
} else {
536-
$scope = 'default';
537-
$scopeId = 0;
538-
$scopeCode = '';
592+
switch (true) {
593+
case $this->getScopeId() !== null:
594+
$scopeIdentifier = $this->getScopeId();
595+
break;
596+
case $this->getScopeCode() !== null:
597+
$scopeIdentifier = $this->getScopeCode();
598+
break;
599+
case $this->getStore() !== null:
600+
$scopeIdentifier = $this->getStore();
601+
break;
602+
case $this->getWebsite() !== null:
603+
$scopeIdentifier = $this->getWebsite();
604+
break;
605+
default:
606+
$scopeIdentifier = null;
607+
break;
608+
}
539609
}
540-
$this->setScope($scope);
541-
$this->setScopeId($scopeId);
542-
$this->setScopeCode($scopeCode);
610+
$scope = $this->scopeResolverPool->get($scopeType)
611+
->getScope($scopeIdentifier);
612+
613+
return $scope;
543614
}
544615

545616
/**

app/code/Magento/Config/Model/Config/Backend/Currency/AbstractCurrency.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
namespace Magento\Config\Model\Config\Backend\Currency;
1515

1616
/**
17+
* Base currency class
18+
*
1719
* @api
1820
* @since 100.0.2
1921
*/
@@ -26,18 +28,19 @@ abstract class AbstractCurrency extends \Magento\Framework\App\Config\Value
2628
*/
2729
protected function _getAllowedCurrencies()
2830
{
29-
if (!$this->isFormData() || $this->getData('groups/options/fields/allow/inherit')) {
30-
return explode(
31+
$allowValue = $this->getData('groups/options/fields/allow/value');
32+
$allowedCurrencies = $allowValue === null || $this->getData('groups/options/fields/allow/inherit')
33+
? explode(
3134
',',
3235
(string)$this->_config->getValue(
3336
\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_ALLOW,
3437
$this->getScope(),
3538
$this->getScopeId()
3639
)
37-
);
38-
}
40+
)
41+
: (array) $allowValue;
3942

40-
return (array)$this->getData('groups/options/fields/allow/value');
43+
return $allowedCurrencies;
4144
}
4245

4346
/**

0 commit comments

Comments
 (0)