Skip to content

Commit cbf828a

Browse files
committed
Separate interception config logic
Moves the logic responsible for loading, saving, clearing the interception config cache into it's own class
1 parent 7dae4c1 commit cbf828a

File tree

6 files changed

+157
-98
lines changed

6 files changed

+157
-98
lines changed

app/etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<preference for="Magento\Framework\App\CacheInterface" type="Magento\Framework\App\Cache\Proxy" />
2929
<preference for="Magento\Framework\App\Cache\StateInterface" type="Magento\Framework\App\Cache\State" />
3030
<preference for="Magento\Framework\App\Cache\TypeListInterface" type="Magento\Framework\App\Cache\TypeList" />
31+
<preference for="Magento\Framework\App\ObjectManager\ConfigWriterInterface" type="Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem" />
3132
<preference for="Magento\Store\Model\StoreManagerInterface" type="Magento\Store\Model\StoreManager" />
3233
<preference for="Magento\Framework\View\DesignInterface" type="Magento\Theme\Model\View\Design\Proxy" />
3334
<preference for="Magento\Framework\View\Design\ThemeInterface" type="Magento\Theme\Model\Theme" />
@@ -412,6 +413,11 @@
412413
<argument name="cacheId" xsi:type="string">interception</argument>
413414
</arguments>
414415
</type>
416+
<type name="Magento\Framework\Interception\Config\CacheManager">
417+
<arguments>
418+
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>
419+
</arguments>
420+
</type>
415421
<type name="Magento\Framework\Interception\PluginList\PluginList">
416422
<arguments>
417423
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>

dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public function setUpInterceptionConfig($pluginConfig)
5959
$areaList->expects($this->any())->method('getCodes')->will($this->returnValue([]));
6060
$configScope = new \Magento\Framework\Config\Scope($areaList, 'global');
6161
$cache = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
62-
$cache->expects($this->any())->method('load')->will($this->returnValue(false));
62+
$cacheManager = $this->createMock(\Magento\Framework\Interception\Config\CacheManager::class);
63+
$cacheManager->method('load')->willReturn(null);
6364
$definitions = new \Magento\Framework\ObjectManager\Definition\Runtime();
6465
$relations = new \Magento\Framework\ObjectManager\Relations\Runtime();
6566
$interceptionConfig = new Config\Config(
@@ -68,7 +69,10 @@ public function setUpInterceptionConfig($pluginConfig)
6869
$cache,
6970
$relations,
7071
$config,
71-
$definitions
72+
$definitions,
73+
'interception',
74+
null,
75+
$cacheManager
7276
);
7377
$interceptionDefinitions = new Definition\Runtime();
7478
$json = new \Magento\Framework\Serialize\Serializer\Json();

dev/tests/integration/testsuite/Magento/Framework/Interception/Config/ConfigTest.php renamed to dev/tests/integration/testsuite/Magento/Framework/Interception/Config/CacheManagerTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use Magento\Framework\App\Filesystem\DirectoryList;
1212

13-
class ConfigTest extends \PHPUnit\Framework\TestCase
13+
class CacheManagerTest extends \PHPUnit\Framework\TestCase
1414
{
1515
const CACHE_ID = 'interceptiontest';
1616

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

71-
foreach ($testConfig as $className => $hasPlugins) {
72-
$this->assertEquals($hasPlugins, $config->hasPlugins($className));
73-
}
71+
$this->assertEquals($testConfig, $config->load(self::CACHE_ID));
7472
}
7573

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

86-
foreach ($testConfig as $className => $hasPlugins) {
87-
$this->assertEquals($hasPlugins, $config->hasPlugins($className));
88-
}
84+
$this->assertEquals($testConfig, $config->load(self::CACHE_ID));
8985
}
9086

9187
public function interceptionCompiledConfigDataProvider()
@@ -121,16 +117,15 @@ private function initializeMetadataDirectory()
121117
* from altering the interception config that may have been generated during application
122118
* installation. Inject a new instance of the compileLoaded to bypass it's caching.
123119
*
124-
* @return \Magento\Framework\Interception\Config\Config
120+
* @return \Magento\Framework\Interception\Config\CacheManager
125121
*/
126122
private function getConfig()
127123
{
128124
return $this->objectManager->create(
129-
\Magento\Framework\Interception\Config\Config::class,
125+
\Magento\Framework\Interception\Config\CacheManager::class,
130126
[
131127
'cacheId' => self::CACHE_ID,
132128
'compiledLoader' => $this->objectManager->create(\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::class),
133-
'serializer' => $this->serializer,
134129
]
135130
);
136131
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Framework\Interception\Config;
10+
11+
/**
12+
* Interception cache manager responsible for handling interaction with compiled and
13+
* uncompiled interception data
14+
*/
15+
class CacheManager
16+
{
17+
/**
18+
* @var \Magento\Framework\Cache\FrontendInterface
19+
*/
20+
private $cache;
21+
22+
/**
23+
* @var \Magento\Framework\Serialize\SerializerInterface
24+
*/
25+
private $serializer;
26+
27+
/**
28+
* @var \Magento\Framework\App\ObjectManager\ConfigWriterInterface
29+
*/
30+
private $configWriter;
31+
32+
/**
33+
* @var \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled
34+
*/
35+
private $compiledLoader;
36+
37+
/**
38+
* @param \Magento\Framework\Cache\FrontendInterface $cache
39+
* @param \Magento\Framework\Serialize\SerializerInterface $serializer
40+
* @param \Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter
41+
* @param \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader
42+
*/
43+
public function __construct(
44+
\Magento\Framework\Cache\FrontendInterface $cache,
45+
\Magento\Framework\Serialize\SerializerInterface $serializer,
46+
\Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter,
47+
\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader
48+
) {
49+
$this->cache = $cache;
50+
$this->serializer = $serializer;
51+
$this->configWriter = $configWriter;
52+
$this->compiledLoader = $compiledLoader;
53+
}
54+
55+
/**
56+
* Load the interception config from cache
57+
*
58+
* @param string $key
59+
* @return array|null
60+
*/
61+
public function load(string $key): ?array
62+
{
63+
if ($this->isCompiled($key)) {
64+
return $this->compiledLoader->load($key);
65+
}
66+
67+
$intercepted = $this->cache->load($key);
68+
return $intercepted ? $this->serializer->unserialize($intercepted) : null;
69+
}
70+
71+
/**
72+
* Save config to cache backend
73+
*
74+
* @param string $key
75+
* @param array $data
76+
*/
77+
public function save(string $key, array $data)
78+
{
79+
$this->cache->save($this->serializer->serialize($data), $key);
80+
}
81+
82+
/**
83+
* Save config to filesystem
84+
*
85+
* @param string $key
86+
* @param array $data
87+
*/
88+
public function saveCompiled(string $key, array $data)
89+
{
90+
$this->configWriter->write($key, $data);
91+
}
92+
93+
/**
94+
* Purge interception cache
95+
*
96+
* @param string $key
97+
*/
98+
public function clean(string $key)
99+
{
100+
$this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [$key]);
101+
}
102+
103+
/**
104+
* Check for the compiled config with the generated metadata
105+
*
106+
* @param string $key
107+
* @return bool
108+
*/
109+
private function isCompiled(string $key): bool
110+
{
111+
return file_exists(\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::getFilePath($key));
112+
}
113+
}

lib/internal/Magento/Framework/Interception/Config/Config.php

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Magento\Framework\Interception\Config;
99

1010
use Magento\Framework\Serialize\SerializerInterface;
11-
use Magento\Framework\Serialize\Serializer\Serialize;
1211

1312
class Config implements \Magento\Framework\Interception\ConfigInterface
1413
{
@@ -35,7 +34,7 @@ class Config implements \Magento\Framework\Interception\ConfigInterface
3534

3635
/**
3736
* Cache
38-
*
37+
* @deprecated
3938
* @var \Magento\Framework\Cache\FrontendInterface
4039
*/
4140
protected $_cache;
@@ -74,33 +73,24 @@ class Config implements \Magento\Framework\Interception\ConfigInterface
7473
protected $_scopeList;
7574

7675
/**
77-
* @var SerializerInterface
78-
*/
79-
private $serializer;
80-
81-
/**
82-
* @var \Magento\Framework\App\ObjectManager\ConfigWriterInterface
83-
*/
84-
private $configWriter;
85-
86-
/**
87-
* @var \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled
76+
* @var CacheManager
8877
*/
89-
private $compiledLoader;
78+
private $cacheManager;
9079

9180
/**
9281
* Config constructor
9382
*
9483
* @param \Magento\Framework\Config\ReaderInterface $reader
9584
* @param \Magento\Framework\Config\ScopeListInterface $scopeList
96-
* @param \Magento\Framework\Cache\FrontendInterface $cache
85+
* @param \Magento\Framework\Cache\FrontendInterface $cache @deprecated
9786
* @param \Magento\Framework\ObjectManager\RelationsInterface $relations
9887
* @param \Magento\Framework\Interception\ObjectManager\ConfigInterface $omConfig
9988
* @param \Magento\Framework\ObjectManager\DefinitionInterface $classDefinitions
10089
* @param string $cacheId
101-
* @param SerializerInterface|null $serializer
102-
* @param \Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter
103-
* @param \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader
90+
* @param SerializerInterface|null $serializer @deprecated
91+
* @param CacheManager $cacheManager
92+
*
93+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
10494
*/
10595
public function __construct(
10696
\Magento\Framework\Config\ReaderInterface $reader,
@@ -111,8 +101,7 @@ public function __construct(
111101
\Magento\Framework\ObjectManager\DefinitionInterface $classDefinitions,
112102
$cacheId = 'interception',
113103
SerializerInterface $serializer = null,
114-
\Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter = null,
115-
\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader = null
104+
CacheManager $cacheManager = null
116105
) {
117106
$this->_omConfig = $omConfig;
118107
$this->_relations = $relations;
@@ -121,14 +110,9 @@ public function __construct(
121110
$this->_cacheId = $cacheId;
122111
$this->_reader = $reader;
123112
$this->_scopeList = $scopeList;
124-
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
125-
->get(Serialize::class);
126-
$this->configWriter = $configWriter ?: \Magento\Framework\App\ObjectManager::getInstance()
127-
->get(\Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem::class);
128-
$this->compiledLoader = $compiledLoader ?: \Magento\Framework\App\ObjectManager::getInstance()
129-
->get(\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::class);
130-
$intercepted = $this->loadIntercepted();
131-
if ($intercepted !== false) {
113+
$this->cacheManager = $cacheManager ?? \Magento\Framework\App\ObjectManager::getInstance()->get(CacheManager::class);
114+
$intercepted = $this->cacheManager->load($cacheId);
115+
if ($intercepted !== null) {
132116
$this->_intercepted = $intercepted;
133117
} else {
134118
$this->initializeUncompiled($this->_classDefinitions->getClasses());
@@ -145,7 +129,7 @@ public function initialize($classDefinitions = [])
145129
{
146130
$this->generateIntercepted($classDefinitions);
147131

148-
$this->configWriter->write($this->_cacheId, $this->_intercepted);
132+
$this->cacheManager->saveCompiled($this->_cacheId, $this->_intercepted);
149133
}
150134

151135
/**
@@ -199,11 +183,11 @@ public function hasPlugins($type)
199183
*/
200184
private function initializeUncompiled($classDefinitions = [])
201185
{
202-
$this->_cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [$this->_cacheId]);
186+
$this->cacheManager->clean($this->_cacheId);
203187

204188
$this->generateIntercepted($classDefinitions);
205189

206-
$this->_cache->save($this->serializer->serialize($this->_intercepted), $this->_cacheId);
190+
$this->cacheManager->save($this->_cacheId, $this->_intercepted);
207191
}
208192

209193
/**
@@ -230,29 +214,4 @@ private function generateIntercepted($classDefinitions)
230214
$this->hasPlugins($class);
231215
}
232216
}
233-
234-
/**
235-
* Load the interception config from cache
236-
*
237-
* @return array|false
238-
*/
239-
private function loadIntercepted()
240-
{
241-
if ($this->isCompiled()) {
242-
return $this->compiledLoader->load($this->_cacheId);
243-
}
244-
245-
$intercepted = $this->_cache->load($this->_cacheId);
246-
return $intercepted ? $this->serializer->unserialize($intercepted) : false;
247-
}
248-
249-
/**
250-
* Check for the compiled config with the generated metadata
251-
*
252-
* @return bool
253-
*/
254-
private function isCompiled()
255-
{
256-
return file_exists(\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::getFilePath($this->_cacheId));
257-
}
258217
}

0 commit comments

Comments
 (0)