[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

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


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1