Skip to content

Commit 13eca91

Browse files
author
Dale Sikkema
committed
Merge remote-tracking branch 'origin/MAGETWO-35920-interceptor-trait' into develop
2 parents 81aca8e + 75668d8 commit 13eca91

File tree

7 files changed

+198
-236
lines changed

7 files changed

+198
-236
lines changed

dev/tests/integration/testsuite/Magento/Framework/Code/_expected/SourceClassWithNamespaceInterceptor.php.sample

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -7,115 +7,14 @@ namespace Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace;
77
*/
88
class Interceptor extends \Magento\Framework\Code\GeneratorTest\SourceClassWithNamespace
99
{
10-
/**
11-
* Object Manager instance
12-
*
13-
* @var \Magento\Framework\ObjectManagerInterface
14-
*/
15-
protected $pluginLocator = null;
16-
17-
/**
18-
* List of plugins
19-
*
20-
* @var \Magento\Framework\Interception\PluginListInterface
21-
*/
22-
protected $pluginList = null;
23-
24-
/**
25-
* Invocation chain
26-
*
27-
* @var \Magento\Framework\Interception\ChainInterface
28-
*/
29-
protected $chain = null;
30-
31-
/**
32-
* Subject type name
33-
*
34-
* @var string
35-
*/
36-
protected $subjectType = null;
10+
use \Magento\Framework\Interception\Interceptor;
3711

3812
public function __construct($param1 = '', $param2 = '\\', $param3 = '\'')
3913
{
4014
$this->___init();
4115
parent::__construct($param1, $param2, $param3);
4216
}
4317

44-
public function ___init()
45-
{
46-
$this->pluginLocator = \Magento\Framework\App\ObjectManager::getInstance();
47-
$this->pluginList = $this->pluginLocator->get('Magento\Framework\Interception\PluginListInterface');
48-
$this->chain = $this->pluginLocator->get('Magento\Framework\Interception\ChainInterface');
49-
$this->subjectType = get_parent_class($this);
50-
if (method_exists($this->subjectType, '___init')) {
51-
parent::___init();
52-
}
53-
}
54-
55-
public function ___callParent($method, array $arguments)
56-
{
57-
return call_user_func_array(array('parent', $method), $arguments);
58-
}
59-
60-
public function __sleep()
61-
{
62-
if (method_exists(get_parent_class($this), '__sleep')) {
63-
return array_diff(parent::__sleep(), array('pluginLocator', 'pluginList', 'chain', 'subjectType'));
64-
} else {
65-
return array_keys(get_class_vars(get_parent_class($this)));
66-
}
67-
}
68-
69-
public function __wakeup()
70-
{
71-
if (method_exists(get_parent_class($this), '__wakeup')) {
72-
parent::__wakeup();
73-
}
74-
$this->___init();
75-
}
76-
77-
protected function ___callPlugins($method, array $arguments, array $pluginInfo)
78-
{
79-
$capMethod = ucfirst($method);
80-
$result = null;
81-
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_BEFORE])) {
82-
// Call 'before' listeners
83-
foreach ($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_BEFORE] as $code) {
84-
$beforeResult = call_user_func_array(
85-
array($this->pluginList->getPlugin($this->subjectType, $code), 'before'. $capMethod), array_merge(array($this), $arguments)
86-
);
87-
if ($beforeResult) {
88-
$arguments = $beforeResult;
89-
}
90-
}
91-
}
92-
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AROUND])) {
93-
// Call 'around' listener
94-
$chain = $this->chain;
95-
$type = $this->subjectType;
96-
$subject = $this;
97-
$code = $pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AROUND];
98-
$next = function () use ($chain, $type, $method, $subject, $code) {
99-
return $chain->invokeNext($type, $method, $subject, func_get_args(), $code);
100-
};
101-
$result = call_user_func_array(
102-
array($this->pluginList->getPlugin($this->subjectType, $code), 'around' . $capMethod),
103-
array_merge(array($this, $next), $arguments)
104-
);
105-
} else {
106-
// Call original method
107-
$result = call_user_func_array(array('parent', $method), $arguments);
108-
}
109-
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AFTER])) {
110-
// Call 'after' listeners
111-
foreach ($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AFTER] as $code) {
112-
$result = $this->pluginList->getPlugin($this->subjectType, $code)
113-
->{'after' . $capMethod}($this, $result);
114-
}
115-
}
116-
return $result;
117-
}
118-
11918
/**
12019
* {@inheritdoc}
12120
*/

lib/internal/Magento/Framework/Code/Generator/CodeGeneratorInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,12 @@ public function setExtendedClass($extendedClass);
5454
* @return $this
5555
*/
5656
public function setImplementedInterfaces(array $interfaces);
57+
58+
/**
59+
* Add a trait to the class.
60+
*
61+
* @param string $trait
62+
* @return $this
63+
*/
64+
public function addTrait($trait);
5765
}

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\Code\GeneratorTest\SourceClassWithNamespace\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: 5 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,7 @@ protected function _getDefaultResultClassName($modelClassName)
3131
*/
3232
protected function _getClassProperties()
3333
{
34-
return [
35-
[
36-
'name' => 'pluginLocator',
37-
'visibility' => 'protected',
38-
'docblock' => [
39-
'shortDescription' => 'Object Manager instance',
40-
'tags' => [[
41-
'name' => 'var',
42-
'description' => '\Magento\Framework\ObjectManagerInterface',
43-
]],
44-
],
45-
],
46-
[
47-
'name' => 'pluginList',
48-
'visibility' => 'protected',
49-
'docblock' => [
50-
'shortDescription' => 'List of plugins',
51-
'tags' => [[
52-
'name' => 'var',
53-
'description' => '\Magento\Framework\Interception\PluginListInterface',
54-
]],
55-
]
56-
],
57-
[
58-
'name' => 'chain',
59-
'visibility' => 'protected',
60-
'docblock' => [
61-
'shortDescription' => 'Invocation chain',
62-
'tags' => [[
63-
'name' => 'var',
64-
'description' => '\Magento\Framework\Interception\ChainInterface',
65-
]],
66-
]
67-
],
68-
[
69-
'name' => 'subjectType',
70-
'visibility' => 'protected',
71-
'docblock' => [
72-
'shortDescription' => 'Subject type name',
73-
'tags' => [['name' => 'var', 'description' => 'string']],
74-
]
75-
]
76-
];
34+
return [];
7735
}
7836

7937
/**
@@ -113,92 +71,6 @@ protected function _getClassMethods()
11371
{
11472
$methods = [$this->_getDefaultConstructorDefinition()];
11573

116-
$methods[] = [
117-
'name' => '___init',
118-
'body' => "\$this->pluginLocator = \\Magento\\Framework\\App\\ObjectManager::getInstance();\n" .
119-
"\$this->pluginList = \$this->pluginLocator->get('Magento\\Framework\\Interception\\PluginListInterface');\n" .
120-
"\$this->chain = \$this->pluginLocator->get('Magento\\Framework\\Interception\\ChainInterface');\n" .
121-
"\$this->subjectType = get_parent_class(\$this);\n" .
122-
"if (method_exists(\$this->subjectType, '___init')) {\n" .
123-
" parent::___init();\n" .
124-
"}\n",
125-
];
126-
127-
$methods[] = [
128-
'name' => '___callParent',
129-
'parameters' => [
130-
['name' => 'method', 'type' => 'string'],
131-
['name' => 'arguments', 'type' => 'array'],
132-
],
133-
'body' => 'return call_user_func_array(array(\'parent\', $method), $arguments);',
134-
];
135-
136-
$methods[] = [
137-
'name' => '__sleep',
138-
'body' => "if (method_exists(get_parent_class(\$this), '__sleep')) {\n" .
139-
" return array_diff(parent::__sleep(), array('pluginLocator', 'pluginList', 'chain', 'subjectType'));" .
140-
"\n} else {\n" .
141-
" return array_keys(get_class_vars(get_parent_class(\$this)));\n" .
142-
"}\n",
143-
];
144-
145-
$methods[] = [
146-
'name' => '__wakeup',
147-
'body' => "if (method_exists(get_parent_class(\$this), '__wakeup')) {\n"
148-
. " parent::__wakeup();\n"
149-
. "}\n"
150-
. "\$this->___init();\n",
151-
];
152-
153-
$methods[] = [
154-
'name' => '___callPlugins',
155-
'visibility' => 'protected',
156-
'parameters' => [
157-
['name' => 'method', 'type' => 'string'],
158-
['name' => 'arguments', 'type' => 'array'],
159-
['name' => 'pluginInfo', 'type' => 'array'],
160-
],
161-
'body' => "\$capMethod = ucfirst(\$method);\n" .
162-
"\$result = null;\n" .
163-
"if (isset(\$pluginInfo[\\Magento\\Framework\\Interception\\DefinitionInterface::LISTENER_BEFORE])) {\n" .
164-
" // Call 'before' listeners\n" .
165-
" foreach (\$pluginInfo[\\Magento\\Framework\\Interception\\DefinitionInterface::LISTENER_BEFORE] as \$code) {\n" .
166-
" \$beforeResult = call_user_func_array(\n" .
167-
" array(\$this->pluginList->getPlugin(\$this->subjectType, \$code), 'before'" .
168-
". \$capMethod), array_merge(array(\$this), \$arguments)\n" .
169-
" );\n" .
170-
" if (\$beforeResult) {\n" .
171-
" \$arguments = \$beforeResult;\n" .
172-
" }\n" .
173-
" }\n" .
174-
"}\n" .
175-
"if (isset(\$pluginInfo[\\Magento\\Framework\\Interception\\DefinitionInterface::LISTENER_AROUND])) {\n" .
176-
" // Call 'around' listener\n" .
177-
" \$chain = \$this->chain;\n" .
178-
" \$type = \$this->subjectType;\n" .
179-
" \$subject = \$this;\n" .
180-
" \$code = \$pluginInfo[\\Magento\\Framework\\Interception\\DefinitionInterface::LISTENER_AROUND];\n" .
181-
" \$next = function () use (\$chain, \$type, \$method, \$subject, \$code) {\n" .
182-
" return \$chain->invokeNext(\$type, \$method, \$subject, func_get_args(), \$code);\n" .
183-
" };\n" .
184-
" \$result = call_user_func_array(\n" .
185-
" array(\$this->pluginList->getPlugin(\$this->subjectType, \$code), 'around' . \$capMethod),\n" .
186-
" array_merge(array(\$this, \$next), \$arguments)\n" .
187-
" );\n" .
188-
"} else {\n" .
189-
" // Call original method\n" .
190-
" \$result = call_user_func_array(array('parent', \$method), \$arguments);\n" .
191-
"}\n" .
192-
"if (isset(\$pluginInfo[\\Magento\\Framework\\Interception\\DefinitionInterface::LISTENER_AFTER])) {\n" .
193-
" // Call 'after' listeners\n" .
194-
" foreach (\$pluginInfo[\\Magento\\Framework\\Interception\\DefinitionInterface::LISTENER_AFTER] as \$code) {\n" .
195-
" \$result = \$this->pluginList->getPlugin(\$this->subjectType, \$code)\n" .
196-
" ->{'after' . \$capMethod}(\$this, \$result);\n" .
197-
" }\n" .
198-
"}\n" .
199-
"return \$result;\n",
200-
];
201-
20274
$reflectionClass = new \ReflectionClass($this->getSourceClassName());
20375
$publicMethods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
20476
foreach ($publicMethods as $method) {
@@ -288,6 +160,10 @@ protected function _generateCode()
288160
} else {
289161
$this->_classGenerator->setExtendedClass($typeName);
290162
}
163+
$this->_classGenerator->addTrait('\Magento\Framework\Interception\Interceptor');
164+
$interfaces = $this->_classGenerator->getImplementedInterfaces();
165+
$interfaces[] = '\Magento\Framework\Interception\InterceptorInterface';
166+
$this->_classGenerator->setImplementedInterfaces($interfaces);
291167
return parent::_generateCode();
292168
}
293169

0 commit comments

Comments
 (0)