Skip to content

Commit 75668d8

Browse files
committed
MAGETWO-35920: [GITHUB] Moves common code to all auto-generated Interceptor classes into a trait #1156
Add interface for Interceptor so Chain can reference it.
1 parent d0db358 commit 75668d8

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
namespace Magento\Framework\Interception\Chain;
88

9-
use Magento\Framework\Interception\Interceptor;
9+
use Magento\Framework\Interception\InterceptorInterface;
1010
use Magento\Framework\Interception\DefinitionInterface;
1111
use Magento\Framework\Interception\PluginListInterface;
1212

@@ -31,11 +31,11 @@ public function __construct(PluginListInterface $pluginList)
3131
* @param string $type
3232
* @param string $method
3333
* @param string $previousPluginCode
34-
* @param Interceptor $subject
34+
* @param InterceptorInterface $subject
3535
* @param array $arguments
3636
* @return mixed|void
3737
*/
38-
public function invokeNext($type, $method, $subject, array $arguments, $previousPluginCode = null)
38+
public function invokeNext($type, $method, InterceptorInterface $subject, array $arguments, $previousPluginCode = null)
3939
{
4040
$pluginInfo = $this->pluginList->getNext($type, $method, $previousPluginCode);
4141
$capMethod = ucfirst($method);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ interface ChainInterface
1111
/**
1212
* @param string $type
1313
* @param string $method
14-
* @param string $subject
14+
* @param InterceptorInterface $subject
1515
* @param array $arguments
1616
* @param string $previousPluginCode
1717
* @return mixed
1818
*/
19-
public function invokeNext($type, $method, $subject, array $arguments, $previousPluginCode = null);
19+
public function invokeNext($type, $method, InterceptorInterface $subject, array $arguments, $previousPluginCode = null);
2020
}

lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ protected function _generateCode()
161161
$this->_classGenerator->setExtendedClass($typeName);
162162
}
163163
$this->_classGenerator->addTrait('\Magento\Framework\Interception\Interceptor');
164+
$interfaces = $this->_classGenerator->getImplementedInterfaces();
165+
$interfaces[] = '\Magento\Framework\Interception\InterceptorInterface';
166+
$this->_classGenerator->setImplementedInterfaces($interfaces);
164167
return parent::_generateCode();
165168
}
166169

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

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

8+
use Magento\Framework\App\ObjectManager;
9+
810
/**
911
* Interceptor trait that contains the common logic for all interceptor classes.
1012
*
1113
* A trait is used because our interceptor classes need to extend the class that they are intercepting.
14+
*
15+
* Any class using this trait is required to implement \Magento\Framework\Interception\InterceptorInterface
16+
*
17+
* @see \Magento\Framework\Interception\InterceptorInterface
1218
*/
1319
trait Interceptor
1420
{
@@ -47,7 +53,7 @@ trait Interceptor
4753
*/
4854
public function ___init()
4955
{
50-
$this->pluginLocator = \Magento\Framework\App\ObjectManager::getInstance();
56+
$this->pluginLocator = ObjectManager::getInstance();
5157
$this->pluginList = $this->pluginLocator->get('Magento\Framework\Interception\PluginListInterface');
5258
$this->chain = $this->pluginLocator->get('Magento\Framework\Interception\ChainInterface');
5359
$this->subjectType = get_parent_class($this);
@@ -107,9 +113,9 @@ protected function ___callPlugins($method, array $arguments, array $pluginInfo)
107113
{
108114
$capMethod = ucfirst($method);
109115
$result = null;
110-
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_BEFORE])) {
116+
if (isset($pluginInfo[DefinitionInterface::LISTENER_BEFORE])) {
111117
// Call 'before' listeners
112-
foreach ($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_BEFORE] as $code) {
118+
foreach ($pluginInfo[DefinitionInterface::LISTENER_BEFORE] as $code) {
113119
$beforeResult = call_user_func_array(
114120
[$this->pluginList->getPlugin($this->subjectType, $code), 'before'. $capMethod],
115121
array_merge([$this], $arguments)
@@ -119,12 +125,13 @@ protected function ___callPlugins($method, array $arguments, array $pluginInfo)
119125
}
120126
}
121127
}
122-
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AROUND])) {
128+
if (isset($pluginInfo[DefinitionInterface::LISTENER_AROUND])) {
123129
// Call 'around' listener
124130
$chain = $this->chain;
125131
$type = $this->subjectType;
132+
/** @var \Magento\Framework\Interception\InterceptorInterface $subject */
126133
$subject = $this;
127-
$code = $pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AROUND];
134+
$code = $pluginInfo[DefinitionInterface::LISTENER_AROUND];
128135
$next = function () use ($chain, $type, $method, $subject, $code) {
129136
return $chain->invokeNext($type, $method, $subject, func_get_args(), $code);
130137
};
@@ -136,9 +143,9 @@ protected function ___callPlugins($method, array $arguments, array $pluginInfo)
136143
// Call original method
137144
$result = call_user_func_array(['parent', $method], $arguments);
138145
}
139-
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AFTER])) {
146+
if (isset($pluginInfo[DefinitionInterface::LISTENER_AFTER])) {
140147
// Call 'after' listeners
141-
foreach ($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AFTER] as $code) {
148+
foreach ($pluginInfo[DefinitionInterface::LISTENER_AFTER] as $code) {
142149
$result = $this->pluginList->getPlugin($this->subjectType, $code)
143150
->{'after' . $capMethod}($this, $result);
144151
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Interception;
7+
8+
/**
9+
* Interface for any class that is intercepting another Magento class.
10+
*
11+
* This interface exposes the parent method of the interception class, which allows the caller to bypass
12+
* the interception logic.
13+
*/
14+
interface InterceptorInterface
15+
{
16+
/**
17+
* Calls parent class method
18+
*
19+
* @param string $method
20+
* @param array $arguments
21+
* @return mixed
22+
*/
23+
public function ___callParent($method, array $arguments);
24+
}

0 commit comments

Comments
 (0)