/** * @dataProvider getScopeDataProvider * * @param string $scopeType * @param string $scope * @param array $data * @param string|null $cachedData */ public function testGetScope($scopeType, $scope, array $data, $cachedData) { $scopeCode = $scope instanceof \Magento\Framework\App\ScopeInterface ? $scope->getCode() : $scope; $cacheKey = "test_cache_id|{$scopeType}|{$scopeCode}"; $this->_readerPool->expects( $this->any() )->method( 'getReader' )->with( $scopeType )->will( $this->returnValue($this->_reader) ); $this->_cache->expects($this->once())->method('load')->with($cacheKey)->will($this->returnValue($cachedData)); if (!$cachedData) { $this->_reader->expects($this->once())->method('read')->with('testScope')->will($this->returnValue($data)); $this->_cache->expects( $this->once() )->method( 'save' )->with( serialize($data), $cacheKey, [\Magento\Framework\App\Config\ScopePool::CACHE_TAG] ); } $configData = $this->getMockBuilder('\Magento\Framework\App\Config\Data') ->disableOriginalConstructor() ->getMock(); $this->_dataFactory->expects( $this->once() )->method( 'create' )->with( ['data' => $data] )->will( $this->returnValue($configData) ); $this->assertInstanceOf( '\Magento\Framework\App\Config\DataInterface', $this->_object->getScope($scopeType, $scope) ); // second call to check caching $this->assertInstanceOf( '\Magento\Framework\App\Config\DataInterface', $this->_object->getScope($scopeType, $scope) ); }
/** * Retrieve config section * * @param string $scopeType * @param string|\Magento\Framework\DataObject|null $scopeCode * @return \Magento\Framework\App\Config\DataInterface */ public function getScope($scopeType, $scopeCode = null) { $scopeCode = $this->_getScopeCode($scopeType, $scopeCode); $code = $scopeType . '|' . $scopeCode; if (!isset($this->_scopes[$code])) { // Key by url to support dynamic {{base_url}} and port assignments $host = $this->getRequest()->getHttpHost(); $port = $this->getRequest()->getServer('SERVER_PORT'); $path = $this->getRequest()->getBasePath(); $urlInfo = $host . $port . trim($path, '/'); $cacheKey = $this->_cacheId . '|' . $code . '|' . $urlInfo; $data = $this->_cache->load($cacheKey); if ($data) { $data = unserialize($data); } else { $reader = $this->_readerPool->getReader($scopeType); if ($scopeType === ScopeConfigInterface::SCOPE_TYPE_DEFAULT) { $data = $reader->read(); } else { $data = $reader->read($scopeCode); } $this->_cache->save(serialize($data), $cacheKey, [self::CACHE_TAG]); } $this->_scopes[$code] = $this->_dataFactory->create(['data' => $data]); } return $this->_scopes[$code]; }
/** * Retrieve config section * * @param string $scopeType * @param string|\Magento\Framework\DataObject|null $scopeCode * @return \Magento\Framework\App\Config\DataInterface */ public function getScope($scopeType, $scopeCode = null) { $scopeCode = $this->_getScopeCode($scopeType, $scopeCode); $code = $scopeType . '|' . $scopeCode; if (!isset($this->_scopes[$code])) { $cacheKey = $this->_cacheId . '|' . $code; $data = $this->_cache->load($cacheKey); if ($data) { $data = unserialize($data); } else { $reader = $this->_readerPool->getReader($scopeType); if ($scopeType === ScopeConfigInterface::SCOPE_TYPE_DEFAULT) { $data = $reader->read(); } else { $data = $reader->read($scopeCode); } $this->_cache->save(serialize($data), $cacheKey, [self::CACHE_TAG]); } $this->_scopes[$code] = $this->_dataFactory->create(['data' => $data]); } return $this->_scopes[$code]; }