[ Index ]

PHP Cross Reference of phpBB-3.3.5-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/ProxyGenerator/ValueHolder/MethodGenerator/ -> Constructor.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\ValueHolder\MethodGenerator;
  22  
  23  use ProxyManager\Generator\MethodGenerator;
  24  use ProxyManager\ProxyGenerator\Util\Properties;
  25  use ProxyManager\ProxyGenerator\Util\UnsetPropertiesGenerator;
  26  use ReflectionClass;
  27  use Zend\Code\Generator\ParameterGenerator;
  28  use Zend\Code\Generator\PropertyGenerator;
  29  use Zend\Code\Reflection\MethodReflection;
  30  
  31  /**
  32   * The `__construct` implementation for lazy loading proxies
  33   *
  34   * @author Marco Pivetta <ocramius@gmail.com>
  35   * @license MIT
  36   */
  37  class Constructor extends MethodGenerator
  38  {
  39      /**
  40       * Constructor
  41       *
  42       * @param ReflectionClass   $originalClass
  43       * @param PropertyGenerator $valueHolder
  44       *
  45       * @return self
  46       *
  47       * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
  48       */
  49      public static function generateMethod(ReflectionClass $originalClass, PropertyGenerator $valueHolder) : self
  50      {
  51          $originalConstructor = self::getConstructor($originalClass);
  52  
  53          $constructor = $originalConstructor
  54              ? self::fromReflection($originalConstructor)
  55              : new self('__construct');
  56  
  57          $constructor->setDocBlock('{@inheritDoc}');
  58          $constructor->setBody(
  59              'static $reflection;' . "\n\n"
  60              . 'if (! $this->' . $valueHolder->getName() . ') {' . "\n"
  61              . '    $reflection = $reflection ?: new \ReflectionClass('
  62              . var_export($originalClass->getName(), true)
  63              . ");\n"
  64              . '    $this->' . $valueHolder->getName() . ' = $reflection->newInstanceWithoutConstructor();' . "\n"
  65              . UnsetPropertiesGenerator::generateSnippet(Properties::fromReflectionClass($originalClass), 'this')
  66              . '}'
  67              . self::generateOriginalConstructorCall($originalClass, $valueHolder)
  68          );
  69  
  70          return $constructor;
  71      }
  72  
  73      private static function generateOriginalConstructorCall(
  74          ReflectionClass $class,
  75          PropertyGenerator $valueHolder
  76      ) : string {
  77          $originalConstructor = self::getConstructor($class);
  78  
  79          if (! $originalConstructor) {
  80              return '';
  81          }
  82  
  83          $constructor = self::fromReflection($originalConstructor);
  84  
  85          return "\n\n"
  86              . '$this->' . $valueHolder->getName() . '->' . $constructor->getName() . '('
  87              . implode(
  88                  ', ',
  89                  array_map(
  90                      function (ParameterGenerator $parameter) : string {
  91                          return ($parameter->getVariadic() ? '...' : '') . '$' . $parameter->getName();
  92                      },
  93                      $constructor->getParameters()
  94                  )
  95              )
  96              . ');';
  97      }
  98  
  99      /**
 100       * @param ReflectionClass $class
 101       *
 102       * @return MethodReflection|null
 103       */
 104      private static function getConstructor(ReflectionClass $class)
 105      {
 106          $constructors = array_map(
 107              function (\ReflectionMethod $method) : MethodReflection {
 108                  return new MethodReflection(
 109                      $method->getDeclaringClass()->getName(),
 110                      $method->getName()
 111                  );
 112              },
 113              array_filter(
 114                  $class->getMethods(),
 115                  function (\ReflectionMethod $method) : bool {
 116                      return $method->isConstructor();
 117                  }
 118              )
 119          );
 120  
 121          return reset($constructors) ?: null;
 122      }
 123  }


Generated: Mon Oct 4 17:42:11 2021 Cross-referenced by PHPXref 0.7.1