Skip to content

Commit b3f2e06

Browse files
committed
Merge branch 'interceptor-as-traits-rc' of git://github.com/fooman/magento2 into MAGETWO-35920-interceptor-trait
Conflicts: dev/tests/integration/testsuite/Magento/Framework/Code/_expected/SourceClassWithNamespaceInterceptor.php.sample lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php
2 parents ea7b254 + d596078 commit b3f2e06

File tree

4 files changed

+121
-231
lines changed

4 files changed

+121
-231
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 $trait
62+
* @return $this
63+
*/
64+
public function addTrait($trait);
5765
}

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

Lines changed: 2 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,7 @@ protected function _generateCode()
288160
} else {
289161
$this->_classGenerator->setExtendedClass($typeName);
290162
}
163+
$this->_classGenerator->addTrait('\Magento\Framework\Interception\Interceptor');
291164
return parent::_generateCode();
292165
}
293166

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace Magento\Framework\Interception;
4+
5+
trait Interceptor
6+
{
7+
/**
8+
* Object Manager instance
9+
*
10+
* @var \Magento\Framework\ObjectManagerInterface
11+
*/
12+
protected $pluginLocator = null;
13+
14+
/**
15+
* List of plugins
16+
*
17+
* @var \Magento\Framework\Interception\PluginListInterface
18+
*/
19+
protected $pluginList = null;
20+
21+
/**
22+
* Invocation chain
23+
*
24+
* @var \Magento\Framework\Interception\ChainInterface
25+
*/
26+
protected $chain = null;
27+
28+
/**
29+
* Subject type name
30+
*
31+
* @var string
32+
*/
33+
protected $subjectType = null;
34+
35+
36+
public function ___init()
37+
{
38+
$this->pluginLocator = \Magento\Framework\App\ObjectManager::getInstance();
39+
$this->pluginList = $this->pluginLocator->get('Magento\Framework\Interception\PluginListInterface');
40+
$this->chain = $this->pluginLocator->get('Magento\Framework\Interception\ChainInterface');
41+
$this->subjectType = get_parent_class($this);
42+
if (method_exists($this->subjectType, '___init')) {
43+
parent::___init();
44+
}
45+
}
46+
47+
public function ___callParent($method, array $arguments)
48+
{
49+
return call_user_func_array(array('parent', $method), $arguments);
50+
}
51+
52+
public function __sleep()
53+
{
54+
if (method_exists(get_parent_class($this), '__sleep')) {
55+
return array_diff(parent::__sleep(), array('pluginLocator', 'pluginList', 'chain', 'subjectType'));
56+
} else {
57+
return array_keys(get_class_vars(get_parent_class($this)));
58+
}
59+
}
60+
61+
public function __wakeup()
62+
{
63+
if (method_exists(get_parent_class(\$this), '__wakeup')) {
64+
parent::__wakeup();
65+
}
66+
$this->___init();
67+
}
68+
69+
protected function ___callPlugins($method, array $arguments, array $pluginInfo)
70+
{
71+
$capMethod = ucfirst($method);
72+
$result = null;
73+
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_BEFORE])) {
74+
// Call 'before' listeners
75+
foreach ($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_BEFORE] as $code) {
76+
$beforeResult = call_user_func_array(
77+
array($this->pluginList->getPlugin($this->subjectType, $code), 'before'. $capMethod), array_merge(array($this), $arguments)
78+
);
79+
if ($beforeResult) {
80+
$arguments = $beforeResult;
81+
}
82+
}
83+
}
84+
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AROUND])) {
85+
// Call 'around' listener
86+
$chain = $this->chain;
87+
$type = $this->subjectType;
88+
$subject = $this;
89+
$code = $pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AROUND];
90+
$next = function () use ($chain, $type, $method, $subject, $code) {
91+
return $chain->invokeNext($type, $method, $subject, func_get_args(), $code);
92+
};
93+
$result = call_user_func_array(
94+
array($this->pluginList->getPlugin($this->subjectType, $code), 'around' . $capMethod),
95+
array_merge(array($this, $next), $arguments)
96+
);
97+
} else {
98+
// Call original method
99+
$result = call_user_func_array(array('parent', $method), $arguments);
100+
}
101+
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AFTER])) {
102+
// Call 'after' listeners
103+
foreach ($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AFTER] as $code) {
104+
$result = $this->pluginList->getPlugin($this->subjectType, $code)
105+
->{'after' . $capMethod}($this, $result);
106+
}
107+
}
108+
return $result;
109+
}
110+
}

0 commit comments

Comments
 (0)