Skip to content

Commit f853354

Browse files
committed
MAGETWO-35920: [GITHUB] Moves common code to all auto-generated Interceptor classes into a trait #1156
Add some doc-blocks and fix some static test failures
1 parent b3f2e06 commit f853354

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function setImplementedInterfaces(array $interfaces);
5858
/**
5959
* Add a trait to the class.
6060
*
61-
* @param $trait
61+
* @param string $trait
6262
* @return $this
6363
*/
6464
public function addTrait($trait);

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

Lines changed: 1 addition & 1 deletion
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\Interceptor;
1010
use Magento\Framework\Interception\DefinitionInterface;
1111
use Magento\Framework\Interception\PluginListInterface;
1212

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

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
<?php
2-
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
36
namespace Magento\Framework\Interception;
47

8+
/**
9+
* Interceptor trait that contains the common logic for all interceptor classes.
10+
*
11+
* A trait is used because our interceptor classes need to extend the class that they are intercepting.
12+
*/
513
trait Interceptor
614
{
715
/**
@@ -32,7 +40,11 @@ trait Interceptor
3240
*/
3341
protected $subjectType = null;
3442

35-
43+
/**
44+
* Initialize the Interceptor
45+
*
46+
* @return void
47+
*/
3648
public function ___init()
3749
{
3850
$this->pluginLocator = \Magento\Framework\App\ObjectManager::getInstance();
@@ -44,28 +56,53 @@ public function ___init()
4456
}
4557
}
4658

59+
/**
60+
* Calls parent class method
61+
*
62+
* @param string $method
63+
* @param array $arguments
64+
* @return mixed
65+
*/
4766
public function ___callParent($method, array $arguments)
4867
{
49-
return call_user_func_array(array('parent', $method), $arguments);
68+
return call_user_func_array(['parent', $method], $arguments);
5069
}
5170

71+
/**
72+
* Calls parent class sleep if defined, otherwise provides own implementation
73+
*
74+
* @return array
75+
*/
5276
public function __sleep()
5377
{
5478
if (method_exists(get_parent_class($this), '__sleep')) {
55-
return array_diff(parent::__sleep(), array('pluginLocator', 'pluginList', 'chain', 'subjectType'));
79+
return array_diff(parent::__sleep(), ['pluginLocator', 'pluginList', 'chain', 'subjectType']);
5680
} else {
5781
return array_keys(get_class_vars(get_parent_class($this)));
5882
}
5983
}
6084

85+
/**
86+
* Causes Interceptor to be initialized
87+
*
88+
* @return void
89+
*/
6190
public function __wakeup()
6291
{
63-
if (method_exists(get_parent_class(\$this), '__wakeup')) {
92+
if (method_exists(get_parent_class($this), '__wakeup')) {
6493
parent::__wakeup();
6594
}
6695
$this->___init();
6796
}
6897

98+
/**
99+
* Calls plugins for a given method.
100+
*
101+
* @param string $method
102+
* @param array $arguments
103+
* @param array $pluginInfo
104+
* @return mixed|null
105+
*/
69106
protected function ___callPlugins($method, array $arguments, array $pluginInfo)
70107
{
71108
$capMethod = ucfirst($method);
@@ -74,7 +111,8 @@ protected function ___callPlugins($method, array $arguments, array $pluginInfo)
74111
// Call 'before' listeners
75112
foreach ($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_BEFORE] as $code) {
76113
$beforeResult = call_user_func_array(
77-
array($this->pluginList->getPlugin($this->subjectType, $code), 'before'. $capMethod), array_merge(array($this), $arguments)
114+
[$this->pluginList->getPlugin($this->subjectType, $code), 'before'. $capMethod],
115+
array_merge([$this], $arguments)
78116
);
79117
if ($beforeResult) {
80118
$arguments = $beforeResult;
@@ -91,12 +129,12 @@ protected function ___callPlugins($method, array $arguments, array $pluginInfo)
91129
return $chain->invokeNext($type, $method, $subject, func_get_args(), $code);
92130
};
93131
$result = call_user_func_array(
94-
array($this->pluginList->getPlugin($this->subjectType, $code), 'around' . $capMethod),
95-
array_merge(array($this, $next), $arguments)
132+
[$this->pluginList->getPlugin($this->subjectType, $code), 'around' . $capMethod],
133+
array_merge([$this, $next], $arguments)
96134
);
97135
} else {
98136
// Call original method
99-
$result = call_user_func_array(array('parent', $method), $arguments);
137+
$result = call_user_func_array(['parent', $method], $arguments);
100138
}
101139
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AFTER])) {
102140
// Call 'after' listeners

0 commit comments

Comments
 (0)