[ Index ]

PHP Cross Reference of phpBB-3.3.5-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/ProxyGenerator/ -> AccessInterceptorValueHolderGenerator.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\AccessInterceptorValueHolderInterface;
  26  use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup;
  27  use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor;
  28  use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor;
  29  use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodPrefixInterceptors;
  30  use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodSuffixInterceptors;
  31  use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\InterceptedMethod;
  32  use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicClone;
  33  use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicGet;
  34  use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicIsset;
  35  use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicSet;
  36  use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\MagicUnset;
  37  use ProxyManager\ProxyGenerator\AccessInterceptorValueHolder\MethodGenerator\StaticProxyConstructor;
  38  use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion;
  39  use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\PropertyGenerator\ValueHolderProperty;
  40  use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
  41  use ProxyManager\ProxyGenerator\Util\Properties;
  42  use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
  43  use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor;
  44  use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\GetWrappedValueHolderValue;
  45  use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\MagicSleep;
  46  use ReflectionClass;
  47  use ReflectionMethod;
  48  use Zend\Code\Generator\ClassGenerator;
  49  use Zend\Code\Generator\MethodGenerator;
  50  use Zend\Code\Reflection\MethodReflection;
  51  
  52  /**
  53   * Generator for proxies implementing {@see \ProxyManager\Proxy\ValueHolderInterface}
  54   * and {@see \ProxyManager\Proxy\AccessInterceptorInterface}
  55   *
  56   * {@inheritDoc}
  57   *
  58   * @author Marco Pivetta <ocramius@gmail.com>
  59   * @license MIT
  60   */
  61  class AccessInterceptorValueHolderGenerator implements ProxyGeneratorInterface
  62  {
  63      /**
  64       * {@inheritDoc}
  65       *
  66       * @throws \InvalidArgumentException
  67       * @throws InvalidProxiedClassException
  68       * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
  69       */
  70      public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator)
  71      {
  72          CanProxyAssertion::assertClassCanBeProxied($originalClass);
  73  
  74          $publicProperties = new PublicPropertiesMap(Properties::fromReflectionClass($originalClass));
  75          $interfaces       = [AccessInterceptorValueHolderInterface::class];
  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($prefixInterceptors = new MethodPrefixInterceptors());
  86          $classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodSuffixInterceptors());
  87          $classGenerator->addPropertyFromGenerator($publicProperties);
  88  
  89          array_map(
  90              function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) {
  91                  ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
  92              },
  93              array_merge(
  94                  array_map(
  95                      $this->buildMethodInterceptor($prefixInterceptors, $suffixInterceptors, $valueHolder),
  96                      ProxiedMethodsFilter::getProxiedMethods($originalClass)
  97                  ),
  98                  [
  99                      Constructor::generateMethod($originalClass, $valueHolder),
 100                      new StaticProxyConstructor($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors),
 101                      new GetWrappedValueHolderValue($valueHolder),
 102                      new SetMethodPrefixInterceptor($prefixInterceptors),
 103                      new SetMethodSuffixInterceptor($suffixInterceptors),
 104                      new MagicGet(
 105                          $originalClass,
 106                          $valueHolder,
 107                          $prefixInterceptors,
 108                          $suffixInterceptors,
 109                          $publicProperties
 110                      ),
 111                      new MagicSet(
 112                          $originalClass,
 113                          $valueHolder,
 114                          $prefixInterceptors,
 115                          $suffixInterceptors,
 116                          $publicProperties
 117                      ),
 118                      new MagicIsset(
 119                          $originalClass,
 120                          $valueHolder,
 121                          $prefixInterceptors,
 122                          $suffixInterceptors,
 123                          $publicProperties
 124                      ),
 125                      new MagicUnset(
 126                          $originalClass,
 127                          $valueHolder,
 128                          $prefixInterceptors,
 129                          $suffixInterceptors,
 130                          $publicProperties
 131                      ),
 132                      new MagicClone($originalClass, $valueHolder, $prefixInterceptors, $suffixInterceptors),
 133                      new MagicSleep($originalClass, $valueHolder),
 134                      new MagicWakeup($originalClass),
 135                  ]
 136              )
 137          );
 138      }
 139  
 140      private function buildMethodInterceptor(
 141          MethodPrefixInterceptors $prefixes,
 142          MethodSuffixInterceptors $suffixes,
 143          ValueHolderProperty $valueHolder
 144      ) : callable {
 145          return function (ReflectionMethod $method) use ($prefixes, $suffixes, $valueHolder) : InterceptedMethod {
 146              return InterceptedMethod::generateMethod(
 147                  new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
 148                  $valueHolder,
 149                  $prefixes,
 150                  $suffixes
 151              );
 152          };
 153      }
 154  }


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