Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Interceptor.php
Go to the documentation of this file.
1 <?php
7 
9 
19 trait Interceptor
20 {
26  private $pluginList;
27 
33  private $subjectType;
34 
40  public function ___init()
41  {
42  $this->pluginList = ObjectManager::getInstance()->get(PluginListInterface::class);
43  $this->subjectType = get_parent_class($this);
44  if (method_exists($this->subjectType, '___init')) {
46  }
47  }
48 
56  public function ___callParent($method, array $arguments)
57  {
58  return parent::$method(...array_values($arguments));
59  }
60 
66  public function __sleep()
67  {
68  if (method_exists(get_parent_class($this), '__sleep')) {
70  } else {
71  $properties = array_keys(get_object_vars($this));
72  }
73  $properties = array_diff($properties, ['pluginList', 'subjectType']);
74  return $properties;
75  }
76 
82  public function __wakeup()
83  {
84  if (method_exists(get_parent_class($this), '__wakeup')) {
86  }
87  $this->___init();
88  }
89 
98  protected function ___callPlugins($method, array $arguments, array $pluginInfo)
99  {
100  $subject = $this;
101  $type = $this->subjectType;
102  $pluginList = $this->pluginList;
103 
104  $next = function (...$arguments) use (
105  $method,
106  &$pluginInfo,
107  $subject,
108  $type,
109  $pluginList,
110  &$next
111  ) {
112  $capMethod = ucfirst($method);
113  $currentPluginInfo = $pluginInfo;
114  $result = null;
115 
116  if (isset($currentPluginInfo[DefinitionInterface::LISTENER_BEFORE])) {
117  // Call 'before' listeners
118  foreach ($currentPluginInfo[DefinitionInterface::LISTENER_BEFORE] as $code) {
119  $pluginInstance = $pluginList->getPlugin($type, $code);
120  $pluginMethod = 'before' . $capMethod;
121  $beforeResult = $pluginInstance->$pluginMethod($this, ...array_values($arguments));
122 
123  if ($beforeResult !== null) {
124  $arguments = (array)$beforeResult;
125  }
126  }
127  }
128 
129  if (isset($currentPluginInfo[DefinitionInterface::LISTENER_AROUND])) {
130  // Call 'around' listener
131  $code = $currentPluginInfo[DefinitionInterface::LISTENER_AROUND];
132  $pluginInfo = $pluginList->getNext($type, $method, $code);
133  $pluginInstance = $pluginList->getPlugin($type, $code);
134  $pluginMethod = 'around' . $capMethod;
135  $result = $pluginInstance->$pluginMethod($subject, $next, ...array_values($arguments));
136  } else {
137  // Call original method
138  $result = $subject->___callParent($method, $arguments);
139  }
140 
141  if (isset($currentPluginInfo[DefinitionInterface::LISTENER_AFTER])) {
142  // Call 'after' listeners
143  foreach ($currentPluginInfo[DefinitionInterface::LISTENER_AFTER] as $code) {
144  $pluginInstance = $pluginList->getPlugin($type, $code);
145  $pluginMethod = 'after' . $capMethod;
146  $result = $pluginInstance->$pluginMethod($subject, $result, ...array_values($arguments));
147  }
148  }
149 
150  return $result;
151  };
152 
153  $result = $next(...array_values($arguments));
154  $next = null;
155 
156  return $result;
157  }
158 }
$type
Definition: item.phtml:13
$method
Definition: info.phtml:13
$arguments
___callPlugins($method, array $arguments, array $pluginInfo)
Definition: Interceptor.php:98
$properties
Definition: categories.php:26
___callParent($method, array $arguments)
Definition: Interceptor.php:56
$code
Definition: info.phtml:12