[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/ProxyGenerator/AccessInterceptorScopeLocalizer/MethodGenerator/Util/ -> InterceptorGenerator.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\Util;
   6  
   7  use ProxyManager\Generator\MethodGenerator;
   8  use ProxyManager\Generator\Util\ProxiedMethodReturnExpression;
   9  use Zend\Code\Generator\PropertyGenerator;
  10  
  11  /**
  12   * Utility to create pre- and post- method interceptors around a given method body
  13   *
  14   * @author Marco Pivetta <ocramius@gmail.com>
  15   * @license MIT
  16   *
  17   * @private - this class is just here as a small utility for this component, don't use it in your own code
  18   */
  19  class InterceptorGenerator
  20  {
  21      /**
  22       * @param string                                  $methodBody         the body of the previously generated code.
  23       *                                                                    It MUST assign the return value to a variable
  24       *                                                                    `$returnValue` instead of directly returning
  25       * @param \ProxyManager\Generator\MethodGenerator $method
  26       * @param \Zend\Code\Generator\PropertyGenerator  $prefixInterceptors
  27       * @param \Zend\Code\Generator\PropertyGenerator  $suffixInterceptors
  28       * @param \ReflectionMethod|null                  $originalMethod
  29       */
  30      public static function createInterceptedMethodBody(
  31          string $methodBody,
  32          MethodGenerator $method,
  33          PropertyGenerator $prefixInterceptors,
  34          PropertyGenerator $suffixInterceptors,
  35          ?\ReflectionMethod $originalMethod
  36      ) : string {
  37          $name                   = var_export($method->getName(), true);
  38          $prefixInterceptorsName = $prefixInterceptors->getName();
  39          $suffixInterceptorsName = $suffixInterceptors->getName();
  40          $params                 = [];
  41  
  42          foreach ($method->getParameters() as $parameter) {
  43              $parameterName = $parameter->getName();
  44              $params[]      = var_export($parameterName, true) . ' => $' . $parameter->getName();
  45          }
  46  
  47          $paramsString = 'array(' . implode(', ', $params) . ')';
  48  
  49          return "if (isset(\$this->$prefixInterceptorsName" . "[$name])) {\n"
  50              . "    \$returnEarly       = false;\n"
  51              . "    \$prefixReturnValue = \$this->$prefixInterceptorsName" . "[$name]->__invoke("
  52              . "\$this, \$this, $name, $paramsString, \$returnEarly);\n\n"
  53              . "    if (\$returnEarly) {\n"
  54              . '        ' . ProxiedMethodReturnExpression::generate('$prefixReturnValue', $originalMethod) . "\n"
  55              . "    }\n"
  56              . "}\n\n"
  57              . $methodBody . "\n\n"
  58              . "if (isset(\$this->$suffixInterceptorsName" . "[$name])) {\n"
  59              . "    \$returnEarly       = false;\n"
  60              . "    \$suffixReturnValue = \$this->$suffixInterceptorsName" . "[$name]->__invoke("
  61              . "\$this, \$this, $name, $paramsString, \$returnValue, \$returnEarly);\n\n"
  62              . "    if (\$returnEarly) {\n"
  63              . '        ' . ProxiedMethodReturnExpression::generate('$suffixReturnValue', $originalMethod) . "\n"
  64              . "    }\n"
  65              . "}\n\n"
  66              . ProxiedMethodReturnExpression::generate('$returnValue', $originalMethod);
  67      }
  68  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1