[ Index ]

PHP Cross Reference of phpBB-3.3.11-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/ProxyGenerator/ -> LazyLoadingGhostGenerator.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace ProxyManager\ProxyGenerator;
   6  
   7  use ProxyManager\Exception\InvalidProxiedClassException;
   8  use ProxyManager\Generator\MethodGenerator as ProxyManagerMethodGenerator;
   9  use ProxyManager\Generator\Util\ClassGeneratorUtils;
  10  use ProxyManager\Proxy\GhostObjectInterface;
  11  use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion;
  12  use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\StaticProxyConstructor;
  13  use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\CallInitializer;
  14  use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\GetProxyInitializer;
  15  use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\InitializeProxy;
  16  use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\IsProxyInitialized;
  17  use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicClone;
  18  use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicGet;
  19  use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicIsset;
  20  use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicSet;
  21  use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicSleep;
  22  use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicUnset;
  23  use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\SetProxyInitializer;
  24  use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\InitializationTracker;
  25  use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\InitializerProperty;
  26  use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\PrivatePropertiesMap;
  27  use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\ProtectedPropertiesMap;
  28  use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
  29  use ProxyManager\ProxyGenerator\Util\Properties;
  30  use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
  31  use ReflectionClass;
  32  use ReflectionMethod;
  33  use Zend\Code\Generator\ClassGenerator;
  34  use Zend\Code\Generator\MethodGenerator;
  35  use Zend\Code\Reflection\MethodReflection;
  36  
  37  /**
  38   * Generator for proxies implementing {@see \ProxyManager\Proxy\GhostObjectInterface}
  39   *
  40   * {@inheritDoc}
  41   *
  42   * @author Marco Pivetta <ocramius@gmail.com>
  43   * @license MIT
  44   */
  45  class LazyLoadingGhostGenerator implements ProxyGeneratorInterface
  46  {
  47      /**
  48       * {@inheritDoc}
  49       *
  50       * @throws InvalidProxiedClassException
  51       * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
  52       * @throws \InvalidArgumentException
  53       */
  54      public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator, array $proxyOptions = [])
  55      {
  56          CanProxyAssertion::assertClassCanBeProxied($originalClass, false);
  57  
  58          $filteredProperties = Properties::fromReflectionClass($originalClass)
  59              ->filter($proxyOptions['skippedProperties'] ?? []);
  60  
  61          $publicProperties    = new PublicPropertiesMap($filteredProperties);
  62          $privateProperties   = new PrivatePropertiesMap($filteredProperties);
  63          $protectedProperties = new ProtectedPropertiesMap($filteredProperties);
  64  
  65          $classGenerator->setExtendedClass($originalClass->getName());
  66          $classGenerator->setImplementedInterfaces([GhostObjectInterface::class]);
  67          $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty());
  68          $classGenerator->addPropertyFromGenerator($initializationTracker = new InitializationTracker());
  69          $classGenerator->addPropertyFromGenerator($publicProperties);
  70          $classGenerator->addPropertyFromGenerator($privateProperties);
  71          $classGenerator->addPropertyFromGenerator($protectedProperties);
  72  
  73          $init = new CallInitializer($initializer, $initializationTracker, $filteredProperties);
  74  
  75          array_map(
  76              function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) {
  77                  ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
  78              },
  79              array_merge(
  80                  $this->getAbstractProxiedMethods($originalClass),
  81                  [
  82                      $init,
  83                      new StaticProxyConstructor($initializer, $filteredProperties),
  84                      new MagicGet(
  85                          $originalClass,
  86                          $initializer,
  87                          $init,
  88                          $publicProperties,
  89                          $protectedProperties,
  90                          $privateProperties,
  91                          $initializationTracker
  92                      ),
  93                      new MagicSet(
  94                          $originalClass,
  95                          $initializer,
  96                          $init,
  97                          $publicProperties,
  98                          $protectedProperties,
  99                          $privateProperties
 100                      ),
 101                      new MagicIsset(
 102                          $originalClass,
 103                          $initializer,
 104                          $init,
 105                          $publicProperties,
 106                          $protectedProperties,
 107                          $privateProperties
 108                      ),
 109                      new MagicUnset(
 110                          $originalClass,
 111                          $initializer,
 112                          $init,
 113                          $publicProperties,
 114                          $protectedProperties,
 115                          $privateProperties
 116                      ),
 117                      new MagicClone($originalClass, $initializer, $init),
 118                      new MagicSleep($originalClass, $initializer, $init),
 119                      new SetProxyInitializer($initializer),
 120                      new GetProxyInitializer($initializer),
 121                      new InitializeProxy($initializer, $init),
 122                      new IsProxyInitialized($initializer),
 123                  ]
 124              )
 125          );
 126      }
 127  
 128      /**
 129       * Retrieves all abstract methods to be proxied
 130       *
 131       * @param ReflectionClass $originalClass
 132       *
 133       * @return MethodGenerator[]
 134       */
 135      private function getAbstractProxiedMethods(ReflectionClass $originalClass) : array
 136      {
 137          return array_map(
 138              function (ReflectionMethod $method) : ProxyManagerMethodGenerator {
 139                  $generated = ProxyManagerMethodGenerator::fromReflectionWithoutBodyAndDocBlock(
 140                      new MethodReflection($method->getDeclaringClass()->getName(), $method->getName())
 141                  );
 142  
 143                  $generated->setAbstract(false);
 144  
 145                  return $generated;
 146              },
 147              ProxiedMethodsFilter::getAbstractProxiedMethods($originalClass)
 148          );
 149      }
 150  }


Generated: Sat Nov 4 14:26:03 2023 Cross-referenced by PHPXref 0.7.1