Skip to content

Commit

Permalink
#32 added ScopePool and ReaderPool for 2.2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
tomazpu committed Oct 31, 2017
1 parent 52ae946 commit 6268ee6
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 3 deletions.
36 changes: 36 additions & 0 deletions Model/App/Config/ReaderPool.php
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];
}
}
168 changes: 168 additions & 0 deletions Model/App/Config/ScopePool.php
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]);
}
}
6 changes: 3 additions & 3 deletions Model/Support.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Support
protected $_paymentConfig;

/**
* @var \Magento\Framework\App\Config\ScopePool
* @var \Wirecard\CheckoutSeamless\Model\App\Config\ScopePool
*/
protected $_scopePool;

Expand Down Expand Up @@ -184,15 +184,15 @@ class Support

/**
* @param \Wirecard\CheckoutSeamless\Helper\Data $dataHelper
* @param \Magento\Framework\App\Config\ScopePool $scopePool
* @param \Wirecard\CheckoutSeamless\Model\App\Config\ScopePool $scopePool
* @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
* @param \Magento\Framework\Module\ModuleList\Loader $moduleLoader
* @param \Magento\Payment\Model\Config $paymentConfig
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Wirecard\CheckoutSeamless\Helper\Data $dataHelper,
\Magento\Framework\App\Config\ScopePool $scopePool,
\Wirecard\CheckoutSeamless\Model\App\Config\ScopePool $scopePool,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
\Magento\Framework\Module\ModuleList\Loader $moduleLoader,
\Magento\Payment\Model\Config $paymentConfig
Expand Down

0 comments on commit 6268ee6

Please sign in to comment.