[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/ProxyGenerator/ValueHolder/MethodGenerator/ -> Constructor.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator;
   6  
   7  use ProxyManager\Generator\MethodGenerator;
   8  use ProxyManager\ProxyGenerator\Util\Properties;
   9  use ProxyManager\ProxyGenerator\Util\UnsetPropertiesGenerator;
  10  use ReflectionClass;
  11  use Zend\Code\Generator\PropertyGenerator;
  12  use Zend\Code\Reflection\MethodReflection;
  13  use Zend\Code\Reflection\ParameterReflection;
  14  
  15  /**
  16   * The `__construct` implementation for lazy loading proxies
  17   *
  18   * @author Marco Pivetta <ocramius@gmail.com>
  19   * @license MIT
  20   */
  21  class Constructor extends MethodGenerator
  22  {
  23      /**
  24       * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
  25       */
  26      public static function generateMethod(ReflectionClass $originalClass, PropertyGenerator $valueHolder) : self
  27      {
  28          $originalConstructor = self::getConstructor($originalClass);
  29  
  30          /* @var $constructor self */
  31          $constructor = $originalConstructor
  32              ? self::fromReflectionWithoutBodyAndDocBlock($originalConstructor)
  33              : new self('__construct');
  34  
  35          $constructor->setBody(
  36              'static $reflection;' . "\n\n"
  37              . 'if (! $this->' . $valueHolder->getName() . ') {' . "\n"
  38              . '    $reflection = $reflection ?: new \ReflectionClass('
  39              . var_export($originalClass->getName(), true)
  40              . ");\n"
  41              . '    $this->' . $valueHolder->getName() . ' = $reflection->newInstanceWithoutConstructor();' . "\n"
  42              . UnsetPropertiesGenerator::generateSnippet(Properties::fromReflectionClass($originalClass), 'this')
  43              . '}'
  44              . ($originalConstructor ? self::generateOriginalConstructorCall($originalConstructor, $valueHolder) : '')
  45          );
  46  
  47          return $constructor;
  48      }
  49  
  50      private static function generateOriginalConstructorCall(
  51          MethodReflection $originalConstructor,
  52          PropertyGenerator $valueHolder
  53      ) : string {
  54          return "\n\n"
  55              . '$this->' . $valueHolder->getName() . '->' . $originalConstructor->getName() . '('
  56              . implode(
  57                  ', ',
  58                  array_map(
  59                      function (ParameterReflection $parameter) : string {
  60                          return ($parameter->isVariadic() ? '...' : '') . '$' . $parameter->getName();
  61                      },
  62                      $originalConstructor->getParameters()
  63                  )
  64              )
  65              . ');';
  66      }
  67  
  68      /**
  69       * @param ReflectionClass $class
  70       *
  71       * @return MethodReflection|null
  72       */
  73      private static function getConstructor(ReflectionClass $class)
  74      {
  75          $constructors = array_map(
  76              function (\ReflectionMethod $method) : MethodReflection {
  77                  return new MethodReflection(
  78                      $method->getDeclaringClass()->getName(),
  79                      $method->getName()
  80                  );
  81              },
  82              array_filter(
  83                  $class->getMethods(),
  84                  function (\ReflectionMethod $method) : bool {
  85                      return $method->isConstructor();
  86                  }
  87              )
  88          );
  89  
  90          return reset($constructors) ?: null;
  91      }
  92  }


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