Skip to content

Commit 4d98fd6

Browse files
Moves common code to all auto-generated Interceptor classes into a trait
1 parent 8e9035c commit 4d98fd6

File tree

5 files changed

+212
-225
lines changed

5 files changed

+212
-225
lines changed

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

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -7,112 +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-
$this->___init();
72-
}
73-
74-
protected function ___callPlugins($method, array $arguments, array $pluginInfo)
75-
{
76-
$capMethod = ucfirst($method);
77-
$result = null;
78-
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_BEFORE])) {
79-
// Call 'before' listeners
80-
foreach ($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_BEFORE] as $code) {
81-
$beforeResult = call_user_func_array(
82-
array($this->pluginList->getPlugin($this->subjectType, $code), 'before'. $capMethod), array_merge(array($this), $arguments)
83-
);
84-
if ($beforeResult) {
85-
$arguments = $beforeResult;
86-
}
87-
}
88-
}
89-
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AROUND])) {
90-
// Call 'around' listener
91-
$chain = $this->chain;
92-
$type = $this->subjectType;
93-
$subject = $this;
94-
$code = $pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AROUND];
95-
$next = function () use ($chain, $type, $method, $subject, $code) {
96-
return $chain->invokeNext($type, $method, $subject, func_get_args(), $code);
97-
};
98-
$result = call_user_func_array(
99-
array($this->pluginList->getPlugin($this->subjectType, $code), 'around' . $capMethod),
100-
array_merge(array($this, $next), $arguments)
101-
);
102-
} else {
103-
// Call original method
104-
$result = call_user_func_array(array('parent', $method), $arguments);
105-
}
106-
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AFTER])) {
107-
// Call 'after' listeners
108-
foreach ($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AFTER] as $code) {
109-
$result = $this->pluginList->getPlugin($this->subjectType, $code)
110-
->{'after' . $capMethod}($this, $result);
111-
}
112-
}
113-
return $result;
114-
}
115-
11618
/**
11719
* {@inheritdoc}
11820
*/

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ class ClassGenerator extends \Zend\Code\Generator\ClassGenerator implements
6161
'passedByReference' => 'setPassedByReference',
6262
];
6363

64+
/**
65+
* @var array Array of string names
66+
*/
67+
protected $traits = array();
68+
69+
public function setTraits(array $traits)
70+
{
71+
$this->traits = $traits;
72+
return $this;
73+
}
74+
75+
/**
76+
* Returns the "traits" classes
77+
*
78+
* @return array
79+
*/
80+
public function getTraits()
81+
{
82+
return $this->traits;
83+
}
84+
6485
/**
6586
* @param object $object
6687
* @param array $data
@@ -209,4 +230,77 @@ public function getNamespaceName()
209230
{
210231
return ltrim(parent::getNamespaceName(), '\\');
211232
}
233+
234+
/**
235+
* @return string
236+
*/
237+
public function generate()
238+
{
239+
if (!$this->isSourceDirty()) {
240+
$output = $this->getSourceContent();
241+
if (!empty($output)) {
242+
return $output;
243+
}
244+
}
245+
246+
$output = '';
247+
248+
if (null !== ($namespace = $this->getNamespaceName())) {
249+
$output .= 'namespace ' . $namespace . ';' . self::LINE_FEED . self::LINE_FEED;
250+
}
251+
252+
$uses = $this->getUses();
253+
if (!empty($uses)) {
254+
foreach ($uses as $use) {
255+
$output .= 'use ' . $use . ';' . self::LINE_FEED;
256+
}
257+
$output .= self::LINE_FEED;
258+
}
259+
260+
if (null !== ($docBlock = $this->getDocBlock())) {
261+
$docBlock->setIndentation('');
262+
$output .= $docBlock->generate();
263+
}
264+
265+
if ($this->isAbstract()) {
266+
$output .= 'abstract ';
267+
}
268+
269+
$output .= 'class ' . $this->getName();
270+
271+
if (!empty($this->extendedClass)) {
272+
$output .= ' extends ' . $this->extendedClass;
273+
}
274+
275+
$implemented = $this->getImplementedInterfaces();
276+
if (!empty($implemented)) {
277+
$output .= ' implements ' . implode(', ', $implemented);
278+
}
279+
280+
$output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED;
281+
282+
$traits = $this->getTraits();
283+
if (!empty($traits)) {
284+
$output .= self::LINE_FEED . $this->indentation
285+
. 'use ' . (implode(', ', $traits)) . ';' . self::LINE_FEED . self::LINE_FEED;
286+
}
287+
288+
$properties = $this->getProperties();
289+
if (!empty($properties)) {
290+
foreach ($properties as $property) {
291+
$output .= $property->generate() . self::LINE_FEED . self::LINE_FEED;
292+
}
293+
}
294+
295+
$methods = $this->getMethods();
296+
if (!empty($methods)) {
297+
foreach ($methods as $method) {
298+
$output .= $method->generate() . self::LINE_FEED;
299+
}
300+
}
301+
302+
$output .= self::LINE_FEED . '}' . self::LINE_FEED;
303+
304+
return $output;
305+
}
212306
}

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+
* Set a list of traits.
60+
*
61+
* @param array $traits
62+
* @return $this
63+
*/
64+
public function setTraits(array $traits);
5765
}

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

Lines changed: 2 additions & 126 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,89 +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' => "\$this->___init();\n",
148-
];
149-
150-
$methods[] = [
151-
'name' => '___callPlugins',
152-
'visibility' => 'protected',
153-
'parameters' => [
154-
['name' => 'method', 'type' => 'string'],
155-
['name' => 'arguments', 'type' => 'array'],
156-
['name' => 'pluginInfo', 'type' => 'array'],
157-
],
158-
'body' => "\$capMethod = ucfirst(\$method);\n" .
159-
"\$result = null;\n" .
160-
"if (isset(\$pluginInfo[\\Magento\\Framework\\Interception\\DefinitionInterface::LISTENER_BEFORE])) {\n" .
161-
" // Call 'before' listeners\n" .
162-
" foreach (\$pluginInfo[\\Magento\\Framework\\Interception\\DefinitionInterface::LISTENER_BEFORE] as \$code) {\n" .
163-
" \$beforeResult = call_user_func_array(\n" .
164-
" array(\$this->pluginList->getPlugin(\$this->subjectType, \$code), 'before'" .
165-
". \$capMethod), array_merge(array(\$this), \$arguments)\n" .
166-
" );\n" .
167-
" if (\$beforeResult) {\n" .
168-
" \$arguments = \$beforeResult;\n" .
169-
" }\n" .
170-
" }\n" .
171-
"}\n" .
172-
"if (isset(\$pluginInfo[\\Magento\\Framework\\Interception\\DefinitionInterface::LISTENER_AROUND])) {\n" .
173-
" // Call 'around' listener\n" .
174-
" \$chain = \$this->chain;\n" .
175-
" \$type = \$this->subjectType;\n" .
176-
" \$subject = \$this;\n" .
177-
" \$code = \$pluginInfo[\\Magento\\Framework\\Interception\\DefinitionInterface::LISTENER_AROUND];\n" .
178-
" \$next = function () use (\$chain, \$type, \$method, \$subject, \$code) {\n" .
179-
" return \$chain->invokeNext(\$type, \$method, \$subject, func_get_args(), \$code);\n" .
180-
" };\n" .
181-
" \$result = call_user_func_array(\n" .
182-
" array(\$this->pluginList->getPlugin(\$this->subjectType, \$code), 'around' . \$capMethod),\n" .
183-
" array_merge(array(\$this, \$next), \$arguments)\n" .
184-
" );\n" .
185-
"} else {\n" .
186-
" // Call original method\n" .
187-
" \$result = call_user_func_array(array('parent', \$method), \$arguments);\n" .
188-
"}\n" .
189-
"if (isset(\$pluginInfo[\\Magento\\Framework\\Interception\\DefinitionInterface::LISTENER_AFTER])) {\n" .
190-
" // Call 'after' listeners\n" .
191-
" foreach (\$pluginInfo[\\Magento\\Framework\\Interception\\DefinitionInterface::LISTENER_AFTER] as \$code) {\n" .
192-
" \$result = \$this->pluginList->getPlugin(\$this->subjectType, \$code)\n" .
193-
" ->{'after' . \$capMethod}(\$this, \$result);\n" .
194-
" }\n" .
195-
"}\n" .
196-
"return \$result;\n",
197-
];
198-
19974
$reflectionClass = new \ReflectionClass($this->getSourceClassName());
20075
$publicMethods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
20176
foreach ($publicMethods as $method) {
@@ -286,6 +161,7 @@ protected function _generateCode()
286161
} else {
287162
$this->_classGenerator->setExtendedClass($typeName);
288163
}
164+
$this->_classGenerator->setTraits(['\Magento\Framework\Interception\Interceptor']);
289165
return parent::_generateCode();
290166
}
291167

0 commit comments

Comments
 (0)