[ Index ]

PHP Cross Reference of phpBB-3.3.5-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/ProxyGenerator/ -> LazyLoadingValueHolderGenerator.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\Util\ClassGeneratorUtils;
  25  use ProxyManager\Proxy\VirtualProxyInterface;
  26  use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup;
  27  use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion;
  28  use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\StaticProxyConstructor;
  29  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\GetProxyInitializer;
  30  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\InitializeProxy;
  31  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\IsProxyInitialized;
  32  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\LazyLoadingMethodInterceptor;
  33  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicClone;
  34  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicGet;
  35  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicIsset;
  36  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicSet;
  37  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicSleep;
  38  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicUnset;
  39  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\SetProxyInitializer;
  40  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\PropertyGenerator\InitializerProperty;
  41  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\PropertyGenerator\ValueHolderProperty;
  42  use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
  43  use ProxyManager\ProxyGenerator\Util\Properties;
  44  use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
  45  use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor;
  46  use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\GetWrappedValueHolderValue;
  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\VirtualProxyInterface}
  55   *
  56   * {@inheritDoc}
  57   *
  58   * @author Marco Pivetta <ocramius@gmail.com>
  59   * @license MIT
  60   */
  61  class LazyLoadingValueHolderGenerator 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)
  71      {
  72          CanProxyAssertion::assertClassCanBeProxied($originalClass);
  73  
  74          $interfaces          = [VirtualProxyInterface::class];
  75          $publicProperties    = new PublicPropertiesMap(Properties::fromReflectionClass($originalClass));
  76  
  77          if ($originalClass->isInterface()) {
  78              $interfaces[] = $originalClass->getName();
  79          } else {
  80              $classGenerator->setExtendedClass($originalClass->getName());
  81          }
  82  
  83          $classGenerator->setImplementedInterfaces($interfaces);
  84          $classGenerator->addPropertyFromGenerator($valueHolder = new ValueHolderProperty());
  85          $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty());
  86          $classGenerator->addPropertyFromGenerator($publicProperties);
  87  
  88          array_map(
  89              function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) {
  90                  ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
  91              },
  92              array_merge(
  93                  array_map(
  94                      $this->buildLazyLoadingMethodInterceptor($initializer, $valueHolder),
  95                      ProxiedMethodsFilter::getProxiedMethods($originalClass)
  96                  ),
  97                  [
  98                      new StaticProxyConstructor($initializer, Properties::fromReflectionClass($originalClass)),
  99                      Constructor::generateMethod($originalClass, $valueHolder),
 100                      new MagicGet($originalClass, $initializer, $valueHolder, $publicProperties),
 101                      new MagicSet($originalClass, $initializer, $valueHolder, $publicProperties),
 102                      new MagicIsset($originalClass, $initializer, $valueHolder, $publicProperties),
 103                      new MagicUnset($originalClass, $initializer, $valueHolder, $publicProperties),
 104                      new MagicClone($originalClass, $initializer, $valueHolder),
 105                      new MagicSleep($originalClass, $initializer, $valueHolder),
 106                      new MagicWakeup($originalClass),
 107                      new SetProxyInitializer($initializer),
 108                      new GetProxyInitializer($initializer),
 109                      new InitializeProxy($initializer, $valueHolder),
 110                      new IsProxyInitialized($valueHolder),
 111                      new GetWrappedValueHolderValue($valueHolder),
 112                  ]
 113              )
 114          );
 115      }
 116  
 117      private function buildLazyLoadingMethodInterceptor(
 118          InitializerProperty $initializer,
 119          ValueHolderProperty $valueHolder
 120      ) : callable {
 121          return function (ReflectionMethod $method) use ($initializer, $valueHolder) : LazyLoadingMethodInterceptor {
 122              return LazyLoadingMethodInterceptor::generateMethod(
 123                  new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
 124                  $initializer,
 125                  $valueHolder
 126              );
 127          };
 128      }
 129  }


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