9
9
use Magento \Config \Model \Config \Structure \Element \Group ;
10
10
use Magento \Config \Model \Config \Structure \Element \Field ;
11
11
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 ;
12
15
13
16
/**
14
17
* Backend config model
15
18
*
16
19
* Used to save configuration
20
+ *
17
21
* @author Magento Core Team <[email protected] >
18
22
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19
23
* @api
20
24
* @since 100.0.2
25
+ * @method string getSection()
26
+ * @method void setSection(string $section)
27
+ * @method string getWebsite()
28
+ * @method void setWebsite(string $website)
29
+ * @method string getStore()
30
+ * @method void setStore(string $store)
31
+ * @method string getScope()
32
+ * @method void setScope(string $scope)
33
+ * @method int getScopeId()
34
+ * @method void setScopeId(int $scopeId)
35
+ * @method string getScopeCode()
36
+ * @method void setScopeCode(string $scopeCode)
21
37
*/
22
38
class Config extends \Magento \Framework \DataObject
23
39
{
@@ -87,6 +103,11 @@ class Config extends \Magento\Framework\DataObject
87
103
*/
88
104
private $ settingChecker ;
89
105
106
+ /**
107
+ * @var ScopeResolverPool
108
+ */
109
+ private $ scopeResolverPool ;
110
+
90
111
/**
91
112
* @param \Magento\Framework\App\Config\ReinitableConfigInterface $config
92
113
* @param \Magento\Framework\Event\ManagerInterface $eventManager
@@ -97,6 +118,7 @@ class Config extends \Magento\Framework\DataObject
97
118
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
98
119
* @param Config\Reader\Source\Deployed\SettingChecker|null $settingChecker
99
120
* @param array $data
121
+ * @param ScopeResolverPool|null $scopeResolverPool
100
122
*/
101
123
public function __construct (
102
124
\Magento \Framework \App \Config \ReinitableConfigInterface $ config ,
@@ -107,7 +129,8 @@ public function __construct(
107
129
\Magento \Framework \App \Config \ValueFactory $ configValueFactory ,
108
130
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
109
131
SettingChecker $ settingChecker = null ,
110
- array $ data = []
132
+ array $ data = [],
133
+ ScopeResolverPool $ scopeResolverPool = null
111
134
) {
112
135
parent ::__construct ($ data );
113
136
$ this ->_eventManager = $ eventManager ;
@@ -117,7 +140,8 @@ public function __construct(
117
140
$ this ->_configLoader = $ configLoader ;
118
141
$ this ->_configValueFactory = $ configValueFactory ;
119
142
$ this ->_storeManager = $ storeManager ;
120
- $ this ->settingChecker = $ settingChecker ?: ObjectManager::getInstance ()->get (SettingChecker::class);
143
+ $ this ->settingChecker = $ settingChecker ?? ObjectManager::getInstance ()->get (SettingChecker::class);
144
+ $ this ->scopeResolverPool = $ scopeResolverPool ?? ObjectManager::getInstance ()->get (ScopeResolverPool::class);
121
145
}
122
146
123
147
/**
@@ -505,9 +529,8 @@ public function setDataByPath($path, $value)
505
529
}
506
530
507
531
/**
508
- * Get scope name and scopeId
532
+ * Resolve scope data
509
533
*
510
- * @todo refactor to scope resolver
511
534
* @return void
512
535
*/
513
536
private function initScope ()
@@ -522,24 +545,33 @@ private function initScope()
522
545
$ this ->setStore ('' );
523
546
}
524
547
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 ();
548
+ if ($ this ->getScope () && $ this ->getScopeId () && $ this ->getScopeCode ()) {
549
+ $ scope = $ this ->scopeResolverPool ->get ($ this ->getScope ())
550
+ ->getScope ($ this ->getScopeId ());
551
+ if (StoreScopeInterface::SCOPE_WEBSITE === $ scope ->getScopeType ()) {
552
+ $ this ->setWebsite ($ scope ->getId ());
553
+ } elseif (StoreScopeInterface::SCOPE_STORE === $ scope ->getScopeType ()) {
554
+ $ this ->setStore ($ scope ->getId ());
555
+ }
535
556
} else {
536
- $ scope = 'default ' ;
537
- $ scopeId = 0 ;
538
- $ scopeCode = '' ;
557
+ switch (true ) {
558
+ case $ this ->getStore ():
559
+ $ scope = $ this ->scopeResolverPool ->get (StoreScopeInterface::SCOPE_STORE )
560
+ ->getScope ($ this ->getStore ());
561
+ break ;
562
+ case $ this ->getWebsite ():
563
+ $ scope = $ this ->scopeResolverPool ->get (StoreScopeInterface::SCOPE_WEBSITE )
564
+ ->getScope ($ this ->getWebsite ());
565
+ break ;
566
+ default :
567
+ $ scope = $ this ->scopeResolverPool ->get (ScopeInterface::SCOPE_DEFAULT )
568
+ ->getScope (0 );
569
+ break ;
570
+ }
571
+ $ this ->setScope ($ scope ->getScopeType ());
572
+ $ this ->setScopeId ($ scope ->getId ());
573
+ $ this ->setScopeCode ($ scope ->getCode ());
539
574
}
540
- $ this ->setScope ($ scope );
541
- $ this ->setScopeId ($ scopeId );
542
- $ this ->setScopeCode ($ scopeCode );
543
575
}
544
576
545
577
/**
0 commit comments