[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

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

   1  <?php
   2  /*
   3   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   4   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   5   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   6   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   7   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   8   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   9   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14   *
  15   * This software consists of voluntary contributions made by many individuals
  16   * and is licensed under the MIT license.
  17   */
  18  
  19  declare(strict_types=1);
  20  
  21  namespace ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\Util;
  22  
  23  use ProxyManager\Generator\MethodGenerator;
  24  use ProxyManager\Generator\Util\ProxiedMethodReturnExpression;
  25  use Zend\Code\Generator\PropertyGenerator;
  26  
  27  /**
  28   * Utility to create pre- and post- method interceptors around a given method body
  29   *
  30   * @author Marco Pivetta <ocramius@gmail.com>
  31   * @license MIT
  32   *
  33   * @private - this class is just here as a small utility for this component, don't use it in your own code
  34   */
  35  class InterceptorGenerator
  36  {
  37      /**
  38       * @param string                                  $methodBody         the body of the previously generated code.
  39       *                                                                    It MUST assign the return value to a variable
  40       *                                                                    `$returnValue` instead of directly returning
  41       * @param \ProxyManager\Generator\MethodGenerator $method
  42       * @param \Zend\Code\Generator\PropertyGenerator  $valueHolder
  43       * @param \Zend\Code\Generator\PropertyGenerator  $prefixInterceptors
  44       * @param \Zend\Code\Generator\PropertyGenerator  $suffixInterceptors
  45       * @param \ReflectionMethod|null                  $originalMethod
  46       *
  47       * @return string
  48       */
  49      public static function createInterceptedMethodBody(
  50          string $methodBody,
  51          MethodGenerator $method,
  52          PropertyGenerator $valueHolder,
  53          PropertyGenerator $prefixInterceptors,
  54          PropertyGenerator $suffixInterceptors,
  55          ?\ReflectionMethod $originalMethod
  56      ) : string {
  57          $name                   = var_export($method->getName(), true);
  58          $valueHolderName        = $valueHolder->getName();
  59          $prefixInterceptorsName = $prefixInterceptors->getName();
  60          $suffixInterceptorsName = $suffixInterceptors->getName();
  61          $params                 = [];
  62  
  63          foreach ($method->getParameters() as $parameter) {
  64              $parameterName = $parameter->getName();
  65              $params[]      = var_export($parameterName, true) . ' => $' . $parameter->getName();
  66          }
  67  
  68          $paramsString = 'array(' . implode(', ', $params) . ')';
  69  
  70          return "if (isset(\$this->$prefixInterceptorsName" . "[$name])) {\n"
  71              . "    \$returnEarly       = false;\n"
  72              . "    \$prefixReturnValue = \$this->$prefixInterceptorsName" . "[$name]->__invoke("
  73              . "\$this, \$this->$valueHolderName, $name, $paramsString, \$returnEarly);\n\n"
  74              . "    if (\$returnEarly) {\n"
  75              . '        ' . ProxiedMethodReturnExpression::generate('$prefixReturnValue', $originalMethod) . "\n"
  76              . "    }\n"
  77              . "}\n\n"
  78              . $methodBody . "\n\n"
  79              . "if (isset(\$this->$suffixInterceptorsName" . "[$name])) {\n"
  80              . "    \$returnEarly       = false;\n"
  81              . "    \$suffixReturnValue = \$this->$suffixInterceptorsName" . "[$name]->__invoke("
  82              . "\$this, \$this->$valueHolderName, $name, $paramsString, \$returnValue, \$returnEarly);\n\n"
  83              . "    if (\$returnEarly) {\n"
  84              . '        ' . ProxiedMethodReturnExpression::generate('$suffixReturnValue', $originalMethod) . "\n"
  85              . "    }\n"
  86              . "}\n\n"
  87              . ProxiedMethodReturnExpression::generate('$returnValue', $originalMethod);
  88      }
  89  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1