[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/ProxyGenerator/ -> AccessInterceptorScopeLocalizerGenerator.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\AccessInterceptorInterface;
  26  use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodPrefixInterceptor;
  27  use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\SetMethodSuffixInterceptor;
  28  use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodPrefixInterceptors;
  29  use ProxyManager\ProxyGenerator\AccessInterceptor\PropertyGenerator\MethodSuffixInterceptors;
  30  use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\BindProxyProperties;
  31  use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\InterceptedMethod;
  32  use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicClone;
  33  use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicGet;
  34  use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicIsset;
  35  use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicSet;
  36  use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicSleep;
  37  use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\MagicUnset;
  38  use ProxyManager\ProxyGenerator\AccessInterceptorScopeLocalizer\MethodGenerator\StaticProxyConstructor;
  39  use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion;
  40  use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
  41  use ReflectionClass;
  42  use ReflectionMethod;
  43  use Zend\Code\Generator\ClassGenerator;
  44  use Zend\Code\Generator\MethodGenerator;
  45  use Zend\Code\Reflection\MethodReflection;
  46  
  47  /**
  48   * Generator for proxies implementing {@see \ProxyManager\Proxy\ValueHolderInterface}
  49   * and localizing scope of the proxied object at instantiation
  50   *
  51   * {@inheritDoc}
  52   *
  53   * @author Marco Pivetta <ocramius@gmail.com>
  54   * @license MIT
  55   */
  56  class AccessInterceptorScopeLocalizerGenerator implements ProxyGeneratorInterface
  57  {
  58      /**
  59       * {@inheritDoc}
  60       *
  61       * @throws \InvalidArgumentException
  62       * @throws InvalidProxiedClassException
  63       * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
  64       */
  65      public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator)
  66      {
  67          CanProxyAssertion::assertClassCanBeProxied($originalClass, false);
  68  
  69          $classGenerator->setExtendedClass($originalClass->getName());
  70          $classGenerator->setImplementedInterfaces([AccessInterceptorInterface::class]);
  71          $classGenerator->addPropertyFromGenerator($prefixInterceptors = new MethodPrefixInterceptors());
  72          $classGenerator->addPropertyFromGenerator($suffixInterceptors = new MethodSuffixInterceptors());
  73  
  74          array_map(
  75              function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) {
  76                  ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
  77              },
  78              array_merge(
  79                  array_map(
  80                      $this->buildMethodInterceptor($prefixInterceptors, $suffixInterceptors),
  81                      ProxiedMethodsFilter::getProxiedMethods(
  82                          $originalClass,
  83                          ['__get', '__set', '__isset', '__unset', '__clone', '__sleep']
  84                      )
  85                  ),
  86                  [
  87                      new StaticProxyConstructor($originalClass),
  88                      new BindProxyProperties($originalClass, $prefixInterceptors, $suffixInterceptors),
  89                      new SetMethodPrefixInterceptor($prefixInterceptors),
  90                      new SetMethodSuffixInterceptor($suffixInterceptors),
  91                      new MagicGet($originalClass, $prefixInterceptors, $suffixInterceptors),
  92                      new MagicSet($originalClass, $prefixInterceptors, $suffixInterceptors),
  93                      new MagicIsset($originalClass, $prefixInterceptors, $suffixInterceptors),
  94                      new MagicUnset($originalClass, $prefixInterceptors, $suffixInterceptors),
  95                      new MagicSleep($originalClass, $prefixInterceptors, $suffixInterceptors),
  96                      new MagicClone($originalClass, $prefixInterceptors, $suffixInterceptors),
  97                  ]
  98              )
  99          );
 100      }
 101  
 102      private function buildMethodInterceptor(
 103          MethodPrefixInterceptors $prefixInterceptors,
 104          MethodSuffixInterceptors $suffixInterceptors
 105      ) : callable {
 106          return function (ReflectionMethod $method) use ($prefixInterceptors, $suffixInterceptors) : InterceptedMethod {
 107              return InterceptedMethod::generateMethod(
 108                  new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
 109                  $prefixInterceptors,
 110                  $suffixInterceptors
 111              );
 112          };
 113      }
 114  }


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