コード例 #1
0
ファイル: Chain.php プロジェクト: pradeep-wagento/magento2
 /**
  * Invoke next plugin in chain
  *
  * @param string $type
  * @param string $method
  * @param string $previousPluginCode
  * @param InterceptorInterface $subject
  * @param array $arguments
  * @return mixed|void
  */
 public function invokeNext($type, $method, InterceptorInterface $subject, array $arguments, $previousPluginCode = null)
 {
     $pluginInfo = $this->pluginList->getNext($type, $method, $previousPluginCode);
     $capMethod = ucfirst($method);
     $result = null;
     if (isset($pluginInfo[DefinitionInterface::LISTENER_BEFORE])) {
         foreach ($pluginInfo[DefinitionInterface::LISTENER_BEFORE] as $code) {
             $beforeResult = call_user_func_array([$this->pluginList->getPlugin($type, $code), 'before' . $capMethod], array_merge([$subject], $arguments));
             if ($beforeResult) {
                 $arguments = $beforeResult;
             }
         }
     }
     if (isset($pluginInfo[DefinitionInterface::LISTENER_AROUND])) {
         $chain = $this;
         $code = $pluginInfo[DefinitionInterface::LISTENER_AROUND];
         $next = function () use($chain, $type, $method, $subject, $code) {
             return $chain->invokeNext($type, $method, $subject, func_get_args(), $code);
         };
         $result = call_user_func_array([$this->pluginList->getPlugin($type, $code), 'around' . $capMethod], array_merge([$subject, $next], $arguments));
     } else {
         $result = $subject->___callParent($method, $arguments);
     }
     if (isset($pluginInfo[DefinitionInterface::LISTENER_AFTER])) {
         foreach ($pluginInfo[DefinitionInterface::LISTENER_AFTER] as $code) {
             $result = $this->pluginList->getPlugin($type, $code)->{'after' . $capMethod}($subject, $result);
         }
     }
     return $result;
 }
コード例 #2
0
 /**
  * Calls plugins for a given method.
  *
  * @param string $method
  * @param array $arguments
  * @param array $pluginInfo
  * @return mixed|null
  */
 protected function ___callPlugins($method, array $arguments, array $pluginInfo)
 {
     $capMethod = ucfirst($method);
     $result = null;
     if (isset($pluginInfo[DefinitionInterface::LISTENER_BEFORE])) {
         // Call 'before' listeners
         foreach ($pluginInfo[DefinitionInterface::LISTENER_BEFORE] as $code) {
             $beforeResult = call_user_func_array(
                 [$this->pluginList->getPlugin($this->subjectType, $code), 'before'. $capMethod],
                 array_merge([$this], $arguments)
             );
             if ($beforeResult) {
                 $arguments = $beforeResult;
             }
         }
     }
     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[DefinitionInterface::LISTENER_AROUND];
         $next = function () use ($chain, $type, $method, $subject, $code) {
             return $chain->invokeNext($type, $method, $subject, func_get_args(), $code);
         };
         $result = call_user_func_array(
             [$this->pluginList->getPlugin($this->subjectType, $code), 'around' . $capMethod],
             array_merge([$this, $next], $arguments)
         );
     } else {
         // Call original method
         $result = call_user_func_array(['parent', $method], $arguments);
     }
     if (isset($pluginInfo[DefinitionInterface::LISTENER_AFTER])) {
         // Call 'after' listeners
         foreach ($pluginInfo[DefinitionInterface::LISTENER_AFTER] as $code) {
             $result = $this->pluginList->getPlugin($this->subjectType, $code)
                 ->{'after' . $capMethod}($this, $result);
         }
     }
     return $result;
 }