[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/proxy-manager-bridge/LazyProxy/PhpDumper/ -> ProxyDumper.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper;
  13  
  14  use ProxyManager\Generator\ClassGenerator;
  15  use ProxyManager\GeneratorStrategy\BaseGeneratorStrategy;
  16  use Symfony\Component\DependencyInjection\Container;
  17  use Symfony\Component\DependencyInjection\ContainerInterface;
  18  use Symfony\Component\DependencyInjection\Definition;
  19  use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface;
  20  
  21  /**
  22   * Generates dumped PHP code of proxies via reflection.
  23   *
  24   * @author Marco Pivetta <ocramius@gmail.com>
  25   */
  26  class ProxyDumper implements DumperInterface
  27  {
  28      private $salt;
  29      private $proxyGenerator;
  30      private $classGenerator;
  31  
  32      /**
  33       * @param string $salt
  34       */
  35      public function __construct($salt = '')
  36      {
  37          $this->salt = $salt;
  38          $this->proxyGenerator = new LazyLoadingValueHolderGenerator();
  39          $this->classGenerator = new BaseGeneratorStrategy();
  40      }
  41  
  42      /**
  43       * {@inheritdoc}
  44       */
  45      public function isProxyCandidate(Definition $definition)
  46      {
  47          return $definition->isLazy() && ($class = $definition->getClass()) && class_exists($class);
  48      }
  49  
  50      /**
  51       * {@inheritdoc}
  52       */
  53      public function getProxyFactoryCode(Definition $definition, $id)
  54      {
  55          $instantiation = 'return';
  56          $scope = '';
  57  
  58          if ($definition->isShared()) {
  59              $instantiation .= ' $this->services[%s] =';
  60  
  61              if (\defined('Symfony\Component\DependencyInjection\ContainerInterface::SCOPE_CONTAINER') && ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope(false)) {
  62                  $instantiation .= ' $this->scopedServices[%s][%1$s] =';
  63              }
  64          }
  65  
  66          $instantiation = sprintf($instantiation, var_export($id, true), var_export($scope, true));
  67          $methodName = 'get'.Container::camelize($id).'Service';
  68          $proxyClass = $this->getProxyClassName($definition);
  69  
  70          $generatedClass = $this->generateProxyClass($definition);
  71  
  72          $constructorCall = $generatedClass->hasMethod('staticProxyConstructor')
  73              ? $proxyClass.'::staticProxyConstructor'
  74              : 'new '.$proxyClass;
  75  
  76          return <<<EOF
  77          if (\$lazyLoad) {
  78              \$container = \$this;
  79  
  80              $instantiation $constructorCall(
  81                  function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) use (\$container) {
  82                      \$wrappedInstance = \$container->$methodName(false);
  83  
  84                      \$proxy->setProxyInitializer(null);
  85  
  86                      return true;
  87                  }
  88              );
  89          }
  90  
  91  
  92  EOF;
  93      }
  94  
  95      /**
  96       * {@inheritdoc}
  97       */
  98      public function getProxyCode(Definition $definition)
  99      {
 100          return preg_replace(
 101              '/(\$this->initializer[0-9a-f]++) && \1->__invoke\(\$this->(valueHolder[0-9a-f]++), (.*?), \1\);/',
 102              '$1 && ($1->__invoke(\$$2, $3, $1) || 1) && $this->$2 = \$$2;',
 103              $this->classGenerator->generate($this->generateProxyClass($definition))
 104          );
 105      }
 106  
 107      /**
 108       * Produces the proxy class name for the given definition.
 109       *
 110       * @return string
 111       */
 112      private function getProxyClassName(Definition $definition)
 113      {
 114          return str_replace('\\', '', $definition->getClass()).'_'.spl_object_hash($definition).$this->salt;
 115      }
 116  
 117      /**
 118       * @return ClassGenerator
 119       */
 120      private function generateProxyClass(Definition $definition)
 121      {
 122          $generatedClass = new ClassGenerator($this->getProxyClassName($definition));
 123  
 124          $this->proxyGenerator->generate(new \ReflectionClass($definition->getClass()), $generatedClass);
 125  
 126          return $generatedClass;
 127      }
 128  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1