Skip to content

Commit 7e4a72e

Browse files
committed
MC-31618: Move static config to files - PLUGIN_LIST
- Implement 2 interfaces by one plugin list generator;
1 parent 7cf7e0c commit 7e4a72e

File tree

8 files changed

+104
-106
lines changed

8 files changed

+104
-106
lines changed

app/etc/di.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@
209209
<preference for="Magento\Framework\MessageQueue\QueueFactoryInterface" type="Magento\Framework\MessageQueue\QueueFactory" />
210210
<preference for="Magento\Framework\Search\Request\IndexScopeResolverInterface" type="Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver"/>
211211
<preference for="Magento\Framework\HTTP\ClientInterface" type="Magento\Framework\HTTP\Client\Curl" />
212-
<preference for="Magento\Framework\Interception\ConfigLoaderInterface" type="Magento\Framework\Interception\ConfigLoader" />
213-
<preference for="Magento\Framework\Interception\ConfigWriterInterface" type="Magento\Framework\Interception\ConfigWriter" />
212+
<preference for="Magento\Framework\Interception\ConfigLoaderInterface" type="Magento\Framework\Interception\PluginListGenerator" />
213+
<preference for="Magento\Framework\Interception\ConfigWriterInterface" type="Magento\Framework\Interception\PluginListGenerator" />
214214
<type name="Magento\Framework\Model\ResourceModel\Db\TransactionManager" shared="false" />
215215
<type name="Magento\Framework\Acl\Data\Cache">
216216
<arguments>
@@ -433,7 +433,7 @@
433433
</argument>
434434
</arguments>
435435
</type>
436-
<type name="Magento\Framework\Interception\ConfigWriter">
436+
<type name="Magento\Framework\Interception\PluginListGenerator">
437437
<arguments>
438438
<argument name="reader" xsi:type="object">Magento\Framework\ObjectManager\Config\Reader\Dom\Proxy</argument>
439439
<argument name="logger" xsi:type="object">\Psr\Log\LoggerInterface\Proxy</argument>

dev/tests/integration/framework/Magento/TestFramework/Interception/PluginList.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\TestFramework\Interception;
77

88
use Magento\Framework\Interception\ConfigLoaderInterface;
9-
use Magento\Framework\Interception\ConfigWriterInterface;
9+
use Magento\Framework\Interception\PluginListGenerator;
1010
use Magento\Framework\Serialize\SerializerInterface;
1111

1212
/**
@@ -34,7 +34,7 @@ class PluginList extends \Magento\Framework\Interception\PluginList\PluginList
3434
* @param string|null $cacheId
3535
* @param SerializerInterface|null $serializer
3636
* @param ConfigLoaderInterface|null $configLoader
37-
* @param ConfigWriterInterface|null $configWriter
37+
* @param PluginListGenerator|null $pluginListGenerator
3838
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
3939
*/
4040
public function __construct(
@@ -50,7 +50,7 @@ public function __construct(
5050
$cacheId = 'plugins',
5151
SerializerInterface $serializer = null,
5252
ConfigLoaderInterface $configLoader = null,
53-
ConfigWriterInterface $configWriter = null
53+
PluginListGenerator $pluginListGenerator = null
5454
) {
5555
parent::__construct(
5656
$reader,
@@ -65,7 +65,7 @@ public function __construct(
6565
$cacheId,
6666
$serializer,
6767
$configLoader,
68-
$configWriter
68+
$pluginListGenerator
6969
);
7070
$this->_originScopeScheme = $this->_scopePriorityScheme;
7171
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ public function setUpInterceptionConfig($pluginConfig)
8383
$cacheManager->method('load')->willReturn(null);
8484
$definitions = new \Magento\Framework\ObjectManager\Definition\Runtime();
8585
$relations = new \Magento\Framework\ObjectManager\Relations\Runtime();
86-
$dirList = new DirectoryList(DirectoryList::GENERATED_METADATA);
87-
$configLoader = new \Magento\Framework\Interception\ConfigLoader($dirList);
86+
$configLoader = $this->createMock(ConfigLoaderInterface::class);
8887
$logger = $this->createMock(\Psr\Log\LoggerInterface::class);
89-
$configWriter = $this->createMock(\Magento\Framework\App\ObjectManager\ConfigWriterInterface::class);
88+
$directoryList = $this->createMock(DirectoryList::class);
89+
$configWriter = $this->createMock(PluginListGenerator::class);
9090
$interceptionConfig = new Config\Config(
9191
$this->_configReader,
9292
$configScope,
@@ -112,6 +112,7 @@ public function setUpInterceptionConfig($pluginConfig)
112112
\Magento\Framework\Serialize\SerializerInterface::class => $json,
113113
\Magento\Framework\Interception\ConfigLoaderInterface::class => $configLoader,
114114
\Psr\Log\LoggerInterface::class => $logger,
115+
\Magento\Framework\App\Filesystem\DirectoryList::class => $directoryList,
115116
\Magento\Framework\App\ObjectManager\ConfigWriterInterface::class => $configWriter
116117
];
117118
$this->_objectManager = new \Magento\Framework\ObjectManager\ObjectManager(
@@ -128,7 +129,7 @@ public function setUpInterceptionConfig($pluginConfig)
128129
\Magento\Framework\Interception\PluginListInterface::class =>
129130
\Magento\Framework\Interception\PluginList\PluginList::class,
130131
\Magento\Framework\Interception\ConfigWriterInterface::class =>
131-
\Magento\Framework\Interception\ConfigWriter::class
132+
\Magento\Framework\Interception\PluginListGenerator::class
132133
],
133134
]
134135
);

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use Magento\Framework\Config\ReaderInterface;
1111
use Magento\Framework\Config\ScopeInterface;
1212
use Magento\Framework\Interception\ConfigLoaderInterface;
13-
use Magento\Framework\Interception\ConfigWriterInterface;
1413
use Magento\Framework\Interception\DefinitionInterface;
14+
use Magento\Framework\Interception\PluginListGenerator;
1515
use Magento\Framework\Interception\PluginListInterface as InterceptionPluginList;
1616
use Magento\Framework\Interception\ObjectManager\ConfigInterface;
1717
use Magento\Framework\ObjectManager\RelationsInterface;
@@ -88,9 +88,9 @@ class PluginList extends Scoped implements InterceptionPluginList
8888
private $configLoader;
8989

9090
/**
91-
* @var ConfigWriterInterface
91+
* @var PluginListGenerator
9292
*/
93-
private $configWriter;
93+
private $pluginListGenerator;
9494

9595
/**
9696
* Constructor
@@ -107,7 +107,7 @@ class PluginList extends Scoped implements InterceptionPluginList
107107
* @param string|null $cacheId
108108
* @param SerializerInterface|null $serializer
109109
* @param ConfigLoaderInterface|null $configLoader
110-
* @param ConfigWriterInterface|null $configWriter
110+
* @param PluginListGenerator|null $pluginListGenerator
111111
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
112112
*/
113113
public function __construct(
@@ -123,7 +123,7 @@ public function __construct(
123123
$cacheId = 'plugins',
124124
SerializerInterface $serializer = null,
125125
ConfigLoaderInterface $configLoader = null,
126-
ConfigWriterInterface $configWriter = null
126+
PluginListGenerator $pluginListGenerator = null
127127
) {
128128
$this->serializer = $serializer ?: $objectManager->get(Serialize::class);
129129
parent::__construct($reader, $configScope, $cache, $cacheId, $this->serializer);
@@ -134,7 +134,7 @@ public function __construct(
134134
$this->_scopePriorityScheme = $scopePriorityScheme;
135135
$this->_objectManager = $objectManager;
136136
$this->configLoader = $configLoader ?: $this->_objectManager->get(ConfigLoaderInterface::class);
137-
$this->configWriter = $configWriter ?: $this->_objectManager->get(ConfigWriterInterface::class);
137+
$this->pluginListGenerator = $pluginListGenerator ?: $this->_objectManager->get(PluginListGenerator::class);
138138
}
139139

140140
/**
@@ -145,7 +145,7 @@ public function __construct(
145145
*/
146146
protected function _inheritPlugins($type)
147147
{
148-
return $this->configWriter->inheritPlugins($type, $this->_data, $this->_inherited, $this->_processed);
148+
return $this->pluginListGenerator->inheritPlugins($type, $this->_data, $this->_inherited, $this->_processed);
149149
}
150150

151151
/**
@@ -157,7 +157,7 @@ protected function _inheritPlugins($type)
157157
*/
158158
protected function _sort($itemA, $itemB)
159159
{
160-
return $this->configWriter->sort($itemA, $itemB);
160+
return ($itemA['sortOrder'] ?? PHP_INT_MIN) - ($itemB['sortOrder'] ?? PHP_INT_MIN);
161161
}
162162

163163
/**
@@ -238,7 +238,7 @@ protected function _loadScopedData()
238238
$this->_data,
239239
$this->_inherited,
240240
$this->_processed
241-
] = $this->configWriter->loadScopedVirtualTypes(
241+
] = $this->pluginListGenerator->loadScopedVirtualTypes(
242242
$this->_scopePriorityScheme,
243243
$this->_loadedScopes,
244244
$this->_data,
@@ -269,7 +269,7 @@ protected function _loadScopedData()
269269
*/
270270
protected function isCurrentScope($scopeCode)
271271
{
272-
return $this->configWriter->isCurrentScope($scopeCode);
272+
return $this->_configScope->getCurrentScope() === $scopeCode;
273273
}
274274

275275
/**
@@ -279,7 +279,7 @@ protected function isCurrentScope($scopeCode)
279279
*/
280280
protected function getClassDefinitions()
281281
{
282-
return $this->configWriter->getClassDefinitions();
282+
return $this->_classDefinitions->getClasses();
283283
}
284284

285285
/**
@@ -290,6 +290,6 @@ protected function getClassDefinitions()
290290
*/
291291
public function merge(array $config)
292292
{
293-
$this->_data = $this->configWriter->merge($config, $this->_data);
293+
$this->_data = $this->pluginListGenerator->merge($config, $this->_data);
294294
}
295295
}

lib/internal/Magento/Framework/Interception/ConfigWriter.php renamed to lib/internal/Magento/Framework/Interception/PluginListGenerator.php

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\Framework\Interception;
77

8-
use Magento\Framework\App\ObjectManager\ConfigWriterInterface as ObjectManagerConfigWriterInterface;
8+
use Magento\Framework\App\Filesystem\DirectoryList;
99
use Magento\Framework\Config\ReaderInterface;
1010
use Magento\Framework\Config\ScopeInterface;
1111
use Magento\Framework\Interception\ObjectManager\ConfigInterface;
@@ -14,9 +14,9 @@
1414
use Psr\Log\LoggerInterface;
1515

1616
/**
17-
* Interception configuration writer for scopes.
17+
* Plugin list configuration writer and loader for scopes.
1818
*/
19-
class ConfigWriter implements ConfigWriterInterface
19+
class PluginListGenerator implements ConfigWriterInterface, ConfigLoaderInterface
2020
{
2121
/**
2222
* @var ScopeInterface
@@ -78,9 +78,9 @@ class ConfigWriter implements ConfigWriterInterface
7878
private $logger;
7979

8080
/**
81-
* @var ObjectManagerConfigWriterInterface
81+
* @var DirectoryList
8282
*/
83-
private $configWriter;
83+
private $directoryList;
8484

8585
/**
8686
* @var array
@@ -117,7 +117,7 @@ class ConfigWriter implements ConfigWriterInterface
117117
* @param DefinitionInterface $definitions
118118
* @param ClassDefinitions $classDefinitions
119119
* @param LoggerInterface $logger
120-
* @param ObjectManagerConfigWriterInterface $configWriter
120+
* @param DirectoryList $directoryList
121121
* @param array $scopePriorityScheme
122122
*/
123123
public function __construct(
@@ -128,7 +128,7 @@ public function __construct(
128128
DefinitionInterface $definitions,
129129
ClassDefinitions $classDefinitions,
130130
LoggerInterface $logger,
131-
ObjectManagerConfigWriterInterface $configWriter,
131+
DirectoryList $directoryList,
132132
array $scopePriorityScheme = ['global']
133133
) {
134134
$this->reader = $reader;
@@ -138,7 +138,7 @@ public function __construct(
138138
$this->definitions = $definitions;
139139
$this->classDefinitions = $classDefinitions;
140140
$this->logger = $logger;
141-
$this->configWriter = $configWriter;
141+
$this->directoryList = $directoryList;
142142
$this->scopePriorityScheme = $scopePriorityScheme;
143143
}
144144

@@ -180,7 +180,7 @@ public function write($scopes)
180180
foreach ($this->getClassDefinitions() as $class) {
181181
$this->inheritPlugins($class, $this->pluginData, $this->inherited, $this->processed);
182182
}
183-
$this->configWriter->write(
183+
$this->writeConfig(
184184
$cacheId,
185185
[$this->pluginData, $this->inherited, $this->processed]
186186
);
@@ -197,6 +197,23 @@ public function write($scopes)
197197
}
198198
}
199199

200+
/**
201+
* Load interception configuration data per scope.
202+
*
203+
* @param string $cacheId
204+
* @return array
205+
* @throws \Magento\Framework\Exception\FileSystemException
206+
*/
207+
public function load($cacheId)
208+
{
209+
$file = $this->directoryList->getPath(DirectoryList::GENERATED_METADATA) . '/' . $cacheId . '.' . 'php';
210+
if (file_exists($file)) {
211+
return include $file;
212+
}
213+
214+
return [];
215+
}
216+
200217
/**
201218
* Load virtual types for current scope
202219
*
@@ -238,7 +255,7 @@ public function loadScopedVirtualTypes($scopePriorityScheme, $loadedScopes, $plu
238255
*
239256
* @return array
240257
*/
241-
public function getClassDefinitions()
258+
private function getClassDefinitions()
242259
{
243260
return $this->classDefinitions->getClasses();
244261
}
@@ -249,7 +266,7 @@ public function getClassDefinitions()
249266
* @param string $scopeCode
250267
* @return bool
251268
*/
252-
public function isCurrentScope($scopeCode)
269+
private function isCurrentScope($scopeCode)
253270
{
254271
return $this->scopeConfig->getCurrentScope() === $scopeCode;
255272
}
@@ -297,7 +314,9 @@ public function inheritPlugins($type, &$pluginData, &$inherited, &$processed)
297314
$inherited[$type] = null;
298315
if (is_array($plugins) && count($plugins)) {
299316
$this->filterPlugins($plugins);
300-
uasort($plugins, [$this, 'sort']);
317+
uasort($plugins, function ($itemA, $itemB) {
318+
return ($itemA['sortOrder'] ?? PHP_INT_MIN) - ($itemB['sortOrder'] ?? PHP_INT_MIN);
319+
});
301320
$this->trimInstanceStartingBackslash($plugins);
302321
$inherited[$type] = $plugins;
303322
$lastPerMethod = [];
@@ -385,14 +404,33 @@ public function merge(array $config, $pluginData)
385404
}
386405

387406
/**
388-
* Sort items
407+
* Writes config in storage
389408
*
390-
* @param array $itemA
391-
* @param array $itemB
392-
* @return int
409+
* @param string $key
410+
* @param array $config
411+
* @return void
412+
* @throws \Magento\Framework\Exception\FileSystemException
393413
*/
394-
public function sort($itemA, $itemB)
414+
private function writeConfig(string $key, array $config)
395415
{
396-
return ($itemA['sortOrder'] ?? PHP_INT_MIN) - ($itemB['sortOrder'] ?? PHP_INT_MIN);
416+
$this->initialize();
417+
$configuration = sprintf('<?php return %s;', var_export($config, true));
418+
file_put_contents(
419+
$this->directoryList->getPath(DirectoryList::GENERATED_METADATA) . '/' . $key . '.php',
420+
$configuration
421+
);
422+
}
423+
424+
/**
425+
* Initializes writer
426+
*
427+
* @return void
428+
* @throws \Magento\Framework\Exception\FileSystemException
429+
*/
430+
private function initialize()
431+
{
432+
if (!file_exists($this->directoryList->getPath(DirectoryList::GENERATED_METADATA))) {
433+
mkdir($this->directoryList->getPath(DirectoryList::GENERATED_METADATA));
434+
}
397435
}
398436
}

0 commit comments

Comments
 (0)