Skip to content

Commit

Permalink
Separate interception config logic
Browse files Browse the repository at this point in the history
Moves the logic responsible for loading, saving, clearing the interception
config cache into it's own class
  • Loading branch information
pmclain committed Nov 2, 2018
1 parent 7dae4c1 commit cbf828a
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 98 deletions.
6 changes: 6 additions & 0 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<preference for="Magento\Framework\App\CacheInterface" type="Magento\Framework\App\Cache\Proxy" />
<preference for="Magento\Framework\App\Cache\StateInterface" type="Magento\Framework\App\Cache\State" />
<preference for="Magento\Framework\App\Cache\TypeListInterface" type="Magento\Framework\App\Cache\TypeList" />
<preference for="Magento\Framework\App\ObjectManager\ConfigWriterInterface" type="Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem" />
<preference for="Magento\Store\Model\StoreManagerInterface" type="Magento\Store\Model\StoreManager" />
<preference for="Magento\Framework\View\DesignInterface" type="Magento\Theme\Model\View\Design\Proxy" />
<preference for="Magento\Framework\View\Design\ThemeInterface" type="Magento\Theme\Model\Theme" />
Expand Down Expand Up @@ -412,6 +413,11 @@
<argument name="cacheId" xsi:type="string">interception</argument>
</arguments>
</type>
<type name="Magento\Framework\Interception\Config\CacheManager">
<arguments>
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>
</arguments>
</type>
<type name="Magento\Framework\Interception\PluginList\PluginList">
<arguments>
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public function setUpInterceptionConfig($pluginConfig)
$areaList->expects($this->any())->method('getCodes')->will($this->returnValue([]));
$configScope = new \Magento\Framework\Config\Scope($areaList, 'global');
$cache = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
$cache->expects($this->any())->method('load')->will($this->returnValue(false));
$cacheManager = $this->createMock(\Magento\Framework\Interception\Config\CacheManager::class);
$cacheManager->method('load')->willReturn(null);
$definitions = new \Magento\Framework\ObjectManager\Definition\Runtime();
$relations = new \Magento\Framework\ObjectManager\Relations\Runtime();
$interceptionConfig = new Config\Config(
Expand All @@ -68,7 +69,10 @@ public function setUpInterceptionConfig($pluginConfig)
$cache,
$relations,
$config,
$definitions
$definitions,
'interception',
null,
$cacheManager
);
$interceptionDefinitions = new Definition\Runtime();
$json = new \Magento\Framework\Serialize\Serializer\Json();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Magento\Framework\App\Filesystem\DirectoryList;

class ConfigTest extends \PHPUnit\Framework\TestCase
class CacheManagerTest extends \PHPUnit\Framework\TestCase
{
const CACHE_ID = 'interceptiontest';

Expand Down Expand Up @@ -68,9 +68,7 @@ public function testInstantiateFromCompiled(array $testConfig)
$this->configWriter->write(self::CACHE_ID, $testConfig);
$config = $this->getConfig();

foreach ($testConfig as $className => $hasPlugins) {
$this->assertEquals($hasPlugins, $config->hasPlugins($className));
}
$this->assertEquals($testConfig, $config->load(self::CACHE_ID));
}

/**
Expand All @@ -83,9 +81,7 @@ public function testInstantiateFromCache(array $testConfig)
$this->cache->save($this->serializer->serialize($testConfig), self::CACHE_ID);
$config = $this->getConfig();

foreach ($testConfig as $className => $hasPlugins) {
$this->assertEquals($hasPlugins, $config->hasPlugins($className));
}
$this->assertEquals($testConfig, $config->load(self::CACHE_ID));
}

public function interceptionCompiledConfigDataProvider()
Expand Down Expand Up @@ -121,16 +117,15 @@ private function initializeMetadataDirectory()
* from altering the interception config that may have been generated during application
* installation. Inject a new instance of the compileLoaded to bypass it's caching.
*
* @return \Magento\Framework\Interception\Config\Config
* @return \Magento\Framework\Interception\Config\CacheManager
*/
private function getConfig()
{
return $this->objectManager->create(
\Magento\Framework\Interception\Config\Config::class,
\Magento\Framework\Interception\Config\CacheManager::class,
[
'cacheId' => self::CACHE_ID,
'compiledLoader' => $this->objectManager->create(\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::class),
'serializer' => $this->serializer,
]
);
}
Expand Down
113 changes: 113 additions & 0 deletions lib/internal/Magento/Framework/Interception/Config/CacheManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Framework\Interception\Config;

/**
* Interception cache manager responsible for handling interaction with compiled and
* uncompiled interception data
*/
class CacheManager
{
/**
* @var \Magento\Framework\Cache\FrontendInterface
*/
private $cache;

/**
* @var \Magento\Framework\Serialize\SerializerInterface
*/
private $serializer;

/**
* @var \Magento\Framework\App\ObjectManager\ConfigWriterInterface
*/
private $configWriter;

/**
* @var \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled
*/
private $compiledLoader;

/**
* @param \Magento\Framework\Cache\FrontendInterface $cache
* @param \Magento\Framework\Serialize\SerializerInterface $serializer
* @param \Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter
* @param \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader
*/
public function __construct(
\Magento\Framework\Cache\FrontendInterface $cache,
\Magento\Framework\Serialize\SerializerInterface $serializer,
\Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter,
\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader
) {
$this->cache = $cache;
$this->serializer = $serializer;
$this->configWriter = $configWriter;
$this->compiledLoader = $compiledLoader;
}

/**
* Load the interception config from cache
*
* @param string $key
* @return array|null
*/
public function load(string $key): ?array
{
if ($this->isCompiled($key)) {
return $this->compiledLoader->load($key);
}

$intercepted = $this->cache->load($key);
return $intercepted ? $this->serializer->unserialize($intercepted) : null;
}

/**
* Save config to cache backend
*
* @param string $key
* @param array $data
*/
public function save(string $key, array $data)
{
$this->cache->save($this->serializer->serialize($data), $key);
}

/**
* Save config to filesystem
*
* @param string $key
* @param array $data
*/
public function saveCompiled(string $key, array $data)
{
$this->configWriter->write($key, $data);
}

/**
* Purge interception cache
*
* @param string $key
*/
public function clean(string $key)
{
$this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [$key]);
}

/**
* Check for the compiled config with the generated metadata
*
* @param string $key
* @return bool
*/
private function isCompiled(string $key): bool
{
return file_exists(\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::getFilePath($key));
}
}
71 changes: 15 additions & 56 deletions lib/internal/Magento/Framework/Interception/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Magento\Framework\Interception\Config;

use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\Serialize\Serializer\Serialize;

class Config implements \Magento\Framework\Interception\ConfigInterface
{
Expand All @@ -35,7 +34,7 @@ class Config implements \Magento\Framework\Interception\ConfigInterface

/**
* Cache
*
* @deprecated
* @var \Magento\Framework\Cache\FrontendInterface
*/
protected $_cache;
Expand Down Expand Up @@ -74,33 +73,24 @@ class Config implements \Magento\Framework\Interception\ConfigInterface
protected $_scopeList;

/**
* @var SerializerInterface
*/
private $serializer;

/**
* @var \Magento\Framework\App\ObjectManager\ConfigWriterInterface
*/
private $configWriter;

/**
* @var \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled
* @var CacheManager
*/
private $compiledLoader;
private $cacheManager;

/**
* Config constructor
*
* @param \Magento\Framework\Config\ReaderInterface $reader
* @param \Magento\Framework\Config\ScopeListInterface $scopeList
* @param \Magento\Framework\Cache\FrontendInterface $cache
* @param \Magento\Framework\Cache\FrontendInterface $cache @deprecated
* @param \Magento\Framework\ObjectManager\RelationsInterface $relations
* @param \Magento\Framework\Interception\ObjectManager\ConfigInterface $omConfig
* @param \Magento\Framework\ObjectManager\DefinitionInterface $classDefinitions
* @param string $cacheId
* @param SerializerInterface|null $serializer
* @param \Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter
* @param \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader
* @param SerializerInterface|null $serializer @deprecated
* @param CacheManager $cacheManager
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
\Magento\Framework\Config\ReaderInterface $reader,
Expand All @@ -111,8 +101,7 @@ public function __construct(
\Magento\Framework\ObjectManager\DefinitionInterface $classDefinitions,
$cacheId = 'interception',
SerializerInterface $serializer = null,
\Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter = null,
\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader = null
CacheManager $cacheManager = null
) {
$this->_omConfig = $omConfig;
$this->_relations = $relations;
Expand All @@ -121,14 +110,9 @@ public function __construct(
$this->_cacheId = $cacheId;
$this->_reader = $reader;
$this->_scopeList = $scopeList;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(Serialize::class);
$this->configWriter = $configWriter ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem::class);
$this->compiledLoader = $compiledLoader ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::class);
$intercepted = $this->loadIntercepted();
if ($intercepted !== false) {
$this->cacheManager = $cacheManager ?? \Magento\Framework\App\ObjectManager::getInstance()->get(CacheManager::class);
$intercepted = $this->cacheManager->load($cacheId);
if ($intercepted !== null) {
$this->_intercepted = $intercepted;
} else {
$this->initializeUncompiled($this->_classDefinitions->getClasses());
Expand All @@ -145,7 +129,7 @@ public function initialize($classDefinitions = [])
{
$this->generateIntercepted($classDefinitions);

$this->configWriter->write($this->_cacheId, $this->_intercepted);
$this->cacheManager->saveCompiled($this->_cacheId, $this->_intercepted);
}

/**
Expand Down Expand Up @@ -199,11 +183,11 @@ public function hasPlugins($type)
*/
private function initializeUncompiled($classDefinitions = [])
{
$this->_cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [$this->_cacheId]);
$this->cacheManager->clean($this->_cacheId);

$this->generateIntercepted($classDefinitions);

$this->_cache->save($this->serializer->serialize($this->_intercepted), $this->_cacheId);
$this->cacheManager->save($this->_cacheId, $this->_intercepted);
}

/**
Expand All @@ -230,29 +214,4 @@ private function generateIntercepted($classDefinitions)
$this->hasPlugins($class);
}
}

/**
* Load the interception config from cache
*
* @return array|false
*/
private function loadIntercepted()
{
if ($this->isCompiled()) {
return $this->compiledLoader->load($this->_cacheId);
}

$intercepted = $this->_cache->load($this->_cacheId);
return $intercepted ? $this->serializer->unserialize($intercepted) : false;
}

/**
* Check for the compiled config with the generated metadata
*
* @return bool
*/
private function isCompiled()
{
return file_exists(\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::getFilePath($this->_cacheId));
}
}
Loading

0 comments on commit cbf828a

Please sign in to comment.