1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Interception\PluginList;
use Magento\Framework\Config\CacheInterface;
use Magento\Framework\Config\Data\Scoped;
use Magento\Framework\Config\ReaderInterface;
use Magento\Framework\Config\ScopeInterface;
use Magento\Framework\Interception\DefinitionInterface;
use Magento\Framework\Interception\PluginListInterface as InterceptionPluginList;
use Magento\Framework\Interception\ObjectManager\ConfigInterface;
use Magento\Framework\ObjectManager\RelationsInterface;
use Magento\Framework\ObjectManager\DefinitionInterface as ClassDefinitions;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\Serialize\Serializer\Serialize;
/**
* Plugin config, provides list of plugins for a type
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class PluginList extends Scoped implements InterceptionPluginList
{
/**
* Inherited plugin data
*
* @var array
*/
protected $_inherited = [];
/**
* Inherited plugin data, preprocessed for read
*
* @var array
*/
protected $_processed;
/**
* Type config
*
* @var ConfigInterface
*/
protected $_omConfig;
/**
* Class relations information provider
*
* @var RelationsInterface
*/
protected $_relations;
/**
* List of interception methods per plugin
*
* @var DefinitionInterface
*/
protected $_definitions;
/**
* List of interceptable application classes
*
* @var ClassDefinitions
*/
protected $_classDefinitions;
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $_objectManager;
/**
* @var array
*/
protected $_pluginInstances = [];
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
/**
* @var SerializerInterface
*/
private $serializer;
/**
* Constructor
*
* @param ReaderInterface $reader
* @param ScopeInterface $configScope
* @param CacheInterface $cache
* @param RelationsInterface $relations
* @param ConfigInterface $omConfig
* @param DefinitionInterface $definitions
* @param ObjectManagerInterface $objectManager
* @param ClassDefinitions $classDefinitions
* @param array $scopePriorityScheme
* @param string|null $cacheId
* @param SerializerInterface|null $serializer
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
ReaderInterface $reader,
ScopeInterface $configScope,
CacheInterface $cache,
RelationsInterface $relations,
ConfigInterface $omConfig,
DefinitionInterface $definitions,
ObjectManagerInterface $objectManager,
ClassDefinitions $classDefinitions,
array $scopePriorityScheme = ['global'],
$cacheId = 'plugins',
SerializerInterface $serializer = null
) {
$this->serializer = $serializer ?: $objectManager->get(Serialize::class);
parent::__construct($reader, $configScope, $cache, $cacheId, $this->serializer);
$this->_omConfig = $omConfig;
$this->_relations = $relations;
$this->_definitions = $definitions;
$this->_classDefinitions = $classDefinitions;
$this->_scopePriorityScheme = $scopePriorityScheme;
$this->_objectManager = $objectManager;
}
/**
* Collect parent types configuration for requested type
*
* @param string $type
* @return array
* @throws \InvalidArgumentException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function _inheritPlugins($type)
{
$type = ltrim($type, '\\');
if (!array_key_exists($type, $this->_inherited)) {
$realType = $this->_omConfig->getOriginalInstanceType($type);
if ($realType !== $type) {
$plugins = $this->_inheritPlugins($realType);
} elseif ($this->_relations->has($type)) {
$relations = $this->_relations->getParents($type);
$plugins = [];
foreach ($relations as $relation) {
if ($relation) {
$relationPlugins = $this->_inheritPlugins($relation);
if ($relationPlugins) {
$plugins = array_replace_recursive($plugins, $relationPlugins);
}
}
}
} else {
$plugins = [];
}
if (isset($this->_data[$type])) {
if (!$plugins) {
$plugins = $this->_data[$type];
} else {
$plugins = array_replace_recursive($plugins, $this->_data[$type]);
}
}
$this->_inherited[$type] = null;
if (is_array($plugins) && count($plugins)) {
$this->filterPlugins($plugins);
uasort($plugins, [$this, '_sort']);
$this->trimInstanceStartingBackslash($plugins);
$this->_inherited[$type] = $plugins;
$lastPerMethod = [];
foreach ($plugins as $key => $plugin) {
// skip disabled plugins
if (isset($plugin['disabled']) && $plugin['disabled']) {
unset($plugins[$key]);
continue;
}
$pluginType = $this->_omConfig->getOriginalInstanceType($plugin['instance']);
if (!class_exists($pluginType)) {
throw new \InvalidArgumentException('Plugin class ' . $pluginType . ' doesn\'t exist');
}
foreach ($this->_definitions->getMethodList($pluginType) as $pluginMethod => $methodTypes) {
$current = isset($lastPerMethod[$pluginMethod]) ? $lastPerMethod[$pluginMethod] : '__self';
$currentKey = $type . '_' . $pluginMethod . '_' . $current;
if ($methodTypes & DefinitionInterface::LISTENER_AROUND) {
$this->_processed[$currentKey][DefinitionInterface::LISTENER_AROUND] = $key;
$lastPerMethod[$pluginMethod] = $key;
}
if ($methodTypes & DefinitionInterface::LISTENER_BEFORE) {
$this->_processed[$currentKey][DefinitionInterface::LISTENER_BEFORE][] = $key;
}
if ($methodTypes & DefinitionInterface::LISTENER_AFTER) {
$this->_processed[$currentKey][DefinitionInterface::LISTENER_AFTER][] = $key;
}
}
}
}
return $plugins;
}
return $this->_inherited[$type];
}
/**
* Trims starting backslash from plugin instance name
*
* @param array $plugins
* @return void
*/
private function trimInstanceStartingBackslash(&$plugins)
{
foreach ($plugins as &$plugin) {
$plugin['instance'] = ltrim($plugin['instance'], '\\');
}
}
/**
* Sort items
*
* @param array $itemA
* @param array $itemB
* @return int
*/
protected function _sort($itemA, $itemB)
{
if (isset($itemA['sortOrder'])) {
if (isset($itemB['sortOrder'])) {
return $itemA['sortOrder'] - $itemB['sortOrder'];
}
return $itemA['sortOrder'];
} elseif (isset($itemB['sortOrder'])) {
return (0 - (int)$itemB['sortOrder']);
} else {
return 0;
}
}
/**
* Retrieve plugin Instance
*
* @param string $type
* @param string $code
* @return mixed
*/
public function getPlugin($type, $code)
{
if (!isset($this->_pluginInstances[$type][$code])) {
$this->_pluginInstances[$type][$code] = $this->_objectManager->get(
$this->_inherited[$type][$code]['instance']
);
}
return $this->_pluginInstances[$type][$code];
}
/**
* Retrieve next plugins in chain
*
* @param string $type
* @param string $method
* @param string $code
* @return array
*/
public function getNext($type, $method, $code = '__self')
{
$this->_loadScopedData();
if (!isset($this->_inherited[$type]) && !array_key_exists($type, $this->_inherited)) {
$this->_inheritPlugins($type);
}
$key = $type . '_' . lcfirst($method) . '_' . $code;
return $this->_processed[$key] ?? null;
}
/**
* Load configuration for current scope
*
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function _loadScopedData()
{
$scope = $this->_configScope->getCurrentScope();
if (false == isset($this->_loadedScopes[$scope])) {
if (false == in_array($scope, $this->_scopePriorityScheme)) {
$this->_scopePriorityScheme[] = $scope;
}
$cacheId = implode('|', $this->_scopePriorityScheme) . "|" . $this->_cacheId;
$data = $this->_cache->load($cacheId);
if ($data) {
list($this->_data, $this->_inherited, $this->_processed) = $this->serializer->unserialize($data);
foreach ($this->_scopePriorityScheme as $scopeCode) {
$this->_loadedScopes[$scopeCode] = true;
}
} else {
$virtualTypes = [];
foreach ($this->_scopePriorityScheme as $scopeCode) {
if (false == isset($this->_loadedScopes[$scopeCode])) {
$data = $this->_reader->read($scopeCode) ?: [];
unset($data['preferences']);
if (count($data) > 0) {
$this->_inherited = [];
$this->_processed = [];
$this->merge($data);
foreach ($data as $class => $config) {
if (isset($config['type'])) {
$virtualTypes[] = $class;
}
}
}
$this->_loadedScopes[$scopeCode] = true;
}
if ($this->isCurrentScope($scopeCode)) {
break;
}
}
foreach ($virtualTypes as $class) {
$this->_inheritPlugins($class);
}
foreach ($this->getClassDefinitions() as $class) {
$this->_inheritPlugins($class);
}
$this->_cache->save(
$this->serializer->serialize([$this->_data, $this->_inherited, $this->_processed]),
$cacheId
);
}
$this->_pluginInstances = [];
}
}
/**
* Whether scope code is current scope code
*
* @param string $scopeCode
* @return bool
*/
protected function isCurrentScope($scopeCode)
{
return $this->_configScope->getCurrentScope() == $scopeCode;
}
/**
* Returns class definitions
*
* @return array
*/
protected function getClassDefinitions()
{
return $this->_classDefinitions->getClasses();
}
/**
* Merge configuration
*
* @param array $config
* @return void
*/
public function merge(array $config)
{
foreach ($config as $type => $typeConfig) {
if (isset($typeConfig['plugins'])) {
$type = ltrim($type, '\\');
if (isset($this->_data[$type])) {
$this->_data[$type] = array_replace_recursive($this->_data[$type], $typeConfig['plugins']);
} else {
$this->_data[$type] = $typeConfig['plugins'];
}
}
}
}
/**
* Remove from list not existing plugins
*
* @param array $plugins
* @return void
*/
private function filterPlugins(array &$plugins)
{
foreach ($plugins as $name => $plugin) {
if (empty($plugin['instance'])) {
unset($plugins[$name]);
$this->getLogger()->info("Reference to undeclared plugin with name '{$name}'.");
}
}
}
/**
* Get logger
*
* @return \Psr\Log\LoggerInterface
* @deprecated 101.0.0
*/
private function getLogger()
{
if ($this->logger === null) {
$this->logger = $this->_objectManager->get(\Psr\Log\LoggerInterface::class);
}
return $this->logger;
}
}