-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#32 added ScopePool and ReaderPool for 2.2 support
- Loading branch information
Showing
3 changed files
with
207 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
/** | ||
* Copyright © 2013-2017 Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Wirecard\CheckoutSeamless\Model\App\Config; | ||
|
||
class ReaderPool | ||
{ | ||
/** | ||
* List of readers | ||
* | ||
* @var array | ||
*/ | ||
protected $_readers = []; | ||
|
||
/** | ||
* @param \Magento\Framework\App\Config\Scope\ReaderInterface[] $readers | ||
*/ | ||
public function __construct( | ||
array $readers | ||
) { | ||
$this->_readers = $readers; | ||
} | ||
|
||
/** | ||
* Retrieve reader by scope type | ||
* | ||
* @param string $scopeType | ||
* @return mixed | ||
*/ | ||
public function getReader($scopeType) | ||
{ | ||
return $this->_readers[$scopeType]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php | ||
/** | ||
* Copyright © 2013-2017 Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Wirecard\CheckoutSeamless\Model\App\Config; | ||
|
||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Framework\App\RequestInterface; | ||
|
||
/** | ||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) | ||
* @deprecated | ||
*/ | ||
class ScopePool | ||
{ | ||
const CACHE_TAG = 'config_scopes'; | ||
|
||
/** | ||
* @var \Magento\Framework\App\Config\Scope\ReaderPoolInterface | ||
*/ | ||
protected $_readerPool; | ||
|
||
/** | ||
* @var DataFactory | ||
*/ | ||
protected $_dataFactory; | ||
|
||
/** | ||
* @var \Magento\Framework\Cache\FrontendInterface | ||
*/ | ||
protected $_cache; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $_cacheId; | ||
|
||
/** | ||
* @var DataInterface[] | ||
*/ | ||
protected $_scopes = []; | ||
|
||
/** | ||
* @var \Magento\Framework\App\ScopeResolverPool | ||
*/ | ||
protected $_scopeResolverPool; | ||
|
||
/** | ||
* @var RequestInterface | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @var ScopeCodeResolver | ||
*/ | ||
private $scopeCodeResolver; | ||
|
||
/** | ||
* @param \Wirecard\CheckoutSeamless\Model\App\Config\ReaderPool $readerPool | ||
* @param DataFactory $dataFactory | ||
* @param \Magento\Framework\Cache\FrontendInterface $cache | ||
* @param \Magento\Framework\App\ScopeResolverPool $scopeResolverPool | ||
* @param string $cacheId | ||
*/ | ||
public function __construct( | ||
\Wirecard\CheckoutSeamless\Model\App\Config\ReaderPool $readerPool, | ||
\Magento\Framework\App\Config\DataFactory $dataFactory, | ||
\Magento\Framework\Cache\FrontendInterface $cache, | ||
\Magento\Framework\App\ScopeResolverPool $scopeResolverPool, | ||
$cacheId = 'default_config_cache' | ||
) | ||
{ | ||
$this->_readerPool = $readerPool; | ||
$this->_dataFactory = $dataFactory; | ||
$this->_cache = $cache; | ||
$this->_cacheId = $cacheId; | ||
$this->_scopeResolverPool = $scopeResolverPool; | ||
} | ||
|
||
/** | ||
* 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 scope code value | ||
* | ||
* @param string $scopeType | ||
* @param string|\Magento\Framework\DataObject|null $scopeCode | ||
* @return string | ||
*/ | ||
protected function _getScopeCode($scopeType, $scopeCode) | ||
{ | ||
return $this->getScopeCodeResolver()->resolve($scopeType, $scopeCode); | ||
} | ||
|
||
/** | ||
* @deprecated | ||
* @return ScopeCodeResolver | ||
*/ | ||
public function getScopeCodeResolver() | ||
{ | ||
if ($this->scopeCodeResolver === null) { | ||
$this->scopeCodeResolver = ObjectManager::getInstance()->get(ScopeCodeResolver::class); | ||
} | ||
return $this->scopeCodeResolver; | ||
} | ||
|
||
/** | ||
* @deprecated | ||
* @return RequestInterface | ||
*/ | ||
private function getRequest() | ||
{ | ||
if ($this->request === null) { | ||
$this->request = ObjectManager::getInstance()->get(RequestInterface::class); | ||
} | ||
return $this->request; | ||
} | ||
|
||
/** | ||
* Clear cache of all scopes | ||
* | ||
* @return void | ||
*/ | ||
public function clean() | ||
{ | ||
$this->_scopes = []; | ||
$this->_cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [self::CACHE_TAG]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters