Skip to content

Commit

Permalink
MAGETWO-35920: [GITHUB] Moves common code to all auto-generated Inter…
Browse files Browse the repository at this point in the history
…ceptor classes into a trait #1156

Add interface for Interceptor so Chain can reference it.
  • Loading branch information
otoolec committed Jun 9, 2015
1 parent d0db358 commit 75668d8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
6 changes: 3 additions & 3 deletions lib/internal/Magento/Framework/Interception/Chain/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
namespace Magento\Framework\Interception\Chain;

use Magento\Framework\Interception\Interceptor;
use Magento\Framework\Interception\InterceptorInterface;
use Magento\Framework\Interception\DefinitionInterface;
use Magento\Framework\Interception\PluginListInterface;

Expand All @@ -31,11 +31,11 @@ public function __construct(PluginListInterface $pluginList)
* @param string $type
* @param string $method
* @param string $previousPluginCode
* @param Interceptor $subject
* @param InterceptorInterface $subject
* @param array $arguments
* @return mixed|void
*/
public function invokeNext($type, $method, $subject, array $arguments, $previousPluginCode = null)
public function invokeNext($type, $method, InterceptorInterface $subject, array $arguments, $previousPluginCode = null)
{
$pluginInfo = $this->pluginList->getNext($type, $method, $previousPluginCode);
$capMethod = ucfirst($method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ interface ChainInterface
/**
* @param string $type
* @param string $method
* @param string $subject
* @param InterceptorInterface $subject
* @param array $arguments
* @param string $previousPluginCode
* @return mixed
*/
public function invokeNext($type, $method, $subject, array $arguments, $previousPluginCode = null);
public function invokeNext($type, $method, InterceptorInterface $subject, array $arguments, $previousPluginCode = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ protected function _generateCode()
$this->_classGenerator->setExtendedClass($typeName);
}
$this->_classGenerator->addTrait('\Magento\Framework\Interception\Interceptor');
$interfaces = $this->_classGenerator->getImplementedInterfaces();
$interfaces[] = '\Magento\Framework\Interception\InterceptorInterface';
$this->_classGenerator->setImplementedInterfaces($interfaces);
return parent::_generateCode();
}

Expand Down
21 changes: 14 additions & 7 deletions lib/internal/Magento/Framework/Interception/Interceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
*/
namespace Magento\Framework\Interception;

use Magento\Framework\App\ObjectManager;

/**
* Interceptor trait that contains the common logic for all interceptor classes.
*
* A trait is used because our interceptor classes need to extend the class that they are intercepting.
*
* Any class using this trait is required to implement \Magento\Framework\Interception\InterceptorInterface
*
* @see \Magento\Framework\Interception\InterceptorInterface
*/
trait Interceptor
{
Expand Down Expand Up @@ -47,7 +53,7 @@ trait Interceptor
*/
public function ___init()
{
$this->pluginLocator = \Magento\Framework\App\ObjectManager::getInstance();
$this->pluginLocator = ObjectManager::getInstance();
$this->pluginList = $this->pluginLocator->get('Magento\Framework\Interception\PluginListInterface');
$this->chain = $this->pluginLocator->get('Magento\Framework\Interception\ChainInterface');
$this->subjectType = get_parent_class($this);
Expand Down Expand Up @@ -107,9 +113,9 @@ protected function ___callPlugins($method, array $arguments, array $pluginInfo)
{
$capMethod = ucfirst($method);
$result = null;
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_BEFORE])) {
if (isset($pluginInfo[DefinitionInterface::LISTENER_BEFORE])) {
// Call 'before' listeners
foreach ($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_BEFORE] as $code) {
foreach ($pluginInfo[DefinitionInterface::LISTENER_BEFORE] as $code) {
$beforeResult = call_user_func_array(
[$this->pluginList->getPlugin($this->subjectType, $code), 'before'. $capMethod],
array_merge([$this], $arguments)
Expand All @@ -119,12 +125,13 @@ protected function ___callPlugins($method, array $arguments, array $pluginInfo)
}
}
}
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AROUND])) {
if (isset($pluginInfo[DefinitionInterface::LISTENER_AROUND])) {
// Call 'around' listener
$chain = $this->chain;
$type = $this->subjectType;
/** @var \Magento\Framework\Interception\InterceptorInterface $subject */
$subject = $this;
$code = $pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AROUND];
$code = $pluginInfo[DefinitionInterface::LISTENER_AROUND];
$next = function () use ($chain, $type, $method, $subject, $code) {
return $chain->invokeNext($type, $method, $subject, func_get_args(), $code);
};
Expand All @@ -136,9 +143,9 @@ protected function ___callPlugins($method, array $arguments, array $pluginInfo)
// Call original method
$result = call_user_func_array(['parent', $method], $arguments);
}
if (isset($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AFTER])) {
if (isset($pluginInfo[DefinitionInterface::LISTENER_AFTER])) {
// Call 'after' listeners
foreach ($pluginInfo[\Magento\Framework\Interception\DefinitionInterface::LISTENER_AFTER] as $code) {
foreach ($pluginInfo[DefinitionInterface::LISTENER_AFTER] as $code) {
$result = $this->pluginList->getPlugin($this->subjectType, $code)
->{'after' . $capMethod}($this, $result);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Interception;

/**
* Interface for any class that is intercepting another Magento class.
*
* This interface exposes the parent method of the interception class, which allows the caller to bypass
* the interception logic.
*/
interface InterceptorInterface
{
/**
* Calls parent class method
*
* @param string $method
* @param array $arguments
* @return mixed
*/
public function ___callParent($method, array $arguments);
}

0 comments on commit 75668d8

Please sign in to comment.