Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Scoped.php
Go to the documentation of this file.
1 <?php
7 
10 
17 {
23  protected $_configScope;
24 
30  protected $_reader;
31 
37  protected $_cache;
38 
44  protected $_cacheId;
45 
51  protected $_scopePriorityScheme = [];
52 
58  protected $_loadedScopes = [];
59 
63  private $serializer;
64 
74  public function __construct(
75  \Magento\Framework\Config\ReaderInterface $reader,
76  \Magento\Framework\Config\ScopeInterface $configScope,
77  \Magento\Framework\Config\CacheInterface $cache,
78  $cacheId,
79  SerializerInterface $serializer = null
80  ) {
81  $this->_reader = $reader;
82  $this->_configScope = $configScope;
83  $this->_cache = $cache;
84  $this->_cacheId = $cacheId;
85  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
86  }
87 
95  public function get($path = null, $default = null)
96  {
97  $this->_loadScopedData();
98  return parent::get($path, $default);
99  }
100 
106  protected function _loadScopedData()
107  {
108  $scope = $this->_configScope->getCurrentScope();
109  if (false == isset($this->_loadedScopes[$scope])) {
110  if (false == in_array($scope, $this->_scopePriorityScheme)) {
111  $this->_scopePriorityScheme[] = $scope;
112  }
113  foreach ($this->_scopePriorityScheme as $scopeCode) {
114  if (false == isset($this->_loadedScopes[$scopeCode])) {
115  if ($scopeCode !== 'primary' && ($data = $this->_cache->load($scopeCode . '::' . $this->_cacheId))
116  ) {
117  $data = $this->serializer->unserialize($data);
118  } else {
119  $data = $this->_reader->read($scopeCode);
120  if ($scopeCode !== 'primary') {
121  $this->_cache->save(
122  $this->serializer->serialize($data),
123  $scopeCode . '::' . $this->_cacheId
124  );
125  }
126  }
127  $this->merge($data);
128  $this->_loadedScopes[$scopeCode] = true;
129  }
130  if ($scopeCode == $scope) {
131  break;
132  }
133  }
134  }
135  }
136 }
__construct(\Magento\Framework\Config\ReaderInterface $reader, \Magento\Framework\Config\ScopeInterface $configScope, \Magento\Framework\Config\CacheInterface $cache, $cacheId, SerializerInterface $serializer=null)
Definition: Scoped.php:74
merge(array $config)
Definition: Data.php:120