[ Index ]

PHP Cross Reference of phpBB-3.3.14-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 ProxyManager\Version;
  17  use Symfony\Component\DependencyInjection\Container;
  18  use Symfony\Component\DependencyInjection\ContainerBuilder;
  19  use Symfony\Component\DependencyInjection\Definition;
  20  use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface;
  21  
  22  /**
  23   * Generates dumped PHP code of proxies via reflection.
  24   *
  25   * @author Marco Pivetta <ocramius@gmail.com>
  26   *
  27   * @final since version 3.3
  28   */
  29  class ProxyDumper implements DumperInterface
  30  {
  31      private $salt;
  32      private $proxyGenerator;
  33      private $classGenerator;
  34  
  35      /**
  36       * @param string $salt
  37       */
  38      public function __construct($salt = '')
  39      {
  40          $this->salt = $salt;
  41          $this->proxyGenerator = new LazyLoadingValueHolderGenerator();
  42          $this->classGenerator = new BaseGeneratorStrategy();
  43      }
  44  
  45      /**
  46       * {@inheritdoc}
  47       */
  48      public function isProxyCandidate(Definition $definition)
  49      {
  50          return $definition->isLazy() && ($class = $definition->getClass()) && (class_exists($class) || interface_exists($class));
  51      }
  52  
  53      /**
  54       * {@inheritdoc}
  55       */
  56      public function getProxyFactoryCode(Definition $definition, $id, $factoryCode = null)
  57      {
  58          $instantiation = 'return';
  59  
  60          if ($definition->isShared()) {
  61              $instantiation .= sprintf(' $this->%s[%s] =', method_exists(ContainerBuilder::class, 'addClassResource') || ($definition->isPublic() && !$definition->isPrivate()) ? 'services' : 'privates', var_export($id, true));
  62          }
  63  
  64          if (null === $factoryCode) {
  65              @trigger_error(sprintf('The "%s()" method expects a third argument defining the code to execute to construct your service since Symfony 3.4, providing it will be required in 4.0.', __METHOD__), \E_USER_DEPRECATED);
  66              $factoryCode = '$this->get'.Container::camelize($id).'Service(false)';
  67          } elseif (false === strpos($factoryCode, '(')) {
  68              @trigger_error(sprintf('The "%s()" method expects its third argument to define the code to execute to construct your service since Symfony 3.4, providing it will be required in 4.0.', __METHOD__), \E_USER_DEPRECATED);
  69              $factoryCode = "\$this->$factoryCode(false)";
  70          }
  71          $proxyClass = $this->getProxyClassName($definition);
  72  
  73          $hasStaticConstructor = $this->generateProxyClass($definition)->hasMethod('staticProxyConstructor');
  74  
  75          $constructorCall = sprintf($hasStaticConstructor ? '%s::staticProxyConstructor' : 'new %s', '\\'.$proxyClass);
  76  
  77          return <<<EOF
  78          if (\$lazyLoad) {
  79              $instantiation \$this->createProxy('$proxyClass', function () {
  80                  return $constructorCall(function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) {
  81                      \$wrappedInstance = $factoryCode;
  82  
  83                      \$proxy->setProxyInitializer(null);
  84  
  85                      return true;
  86                  });
  87              });
  88          }
  89  
  90  
  91  EOF;
  92      }
  93  
  94      /**
  95       * {@inheritdoc}
  96       */
  97      public function getProxyCode(Definition $definition)
  98      {
  99          $code = $this->classGenerator->generate($this->generateProxyClass($definition));
 100          $code = preg_replace('/^(class [^ ]++ extends )([^\\\\])/', '$1\\\\$2', $code);
 101  
 102          $code = preg_replace(
 103              '/(\$this->initializer[0-9a-f]++) && \1->__invoke\(\$this->(valueHolder[0-9a-f]++), (.*?), \1\);/',
 104              '$1 && ($1->__invoke(\$$2, $3, $1) || 1) && $this->$2 = \$$2;',
 105              $code
 106          );
 107  
 108          if (version_compare(self::getProxyManagerVersion(), '2.2', '<')) {
 109              $code = preg_replace(
 110                  '/((?:\$(?:this|initializer|instance)->)?(?:publicProperties|initializer|valueHolder))[0-9a-f]++/',
 111                  '$1}'.$this->getIdentifierSuffix($definition),
 112                  $code
 113              );
 114          }
 115  
 116          if (version_compare(self::getProxyManagerVersion(), '2.5', '<')) {
 117              $code = preg_replace('/ \\\\Closure::bind\(function ((?:& )?\(\$instance(?:, \$value)?\))/', ' \Closure::bind(static function \1', $code);
 118          }
 119  
 120          return $code;
 121      }
 122  
 123      /**
 124       * @return string
 125       */
 126      private static function getProxyManagerVersion()
 127      {
 128          if (!class_exists(Version::class)) {
 129              return '0.0.1';
 130          }
 131  
 132          return \defined(Version::class.'::VERSION') ? Version::VERSION : Version::getVersion();
 133      }
 134  
 135      /**
 136       * Produces the proxy class name for the given definition.
 137       *
 138       * @return string
 139       */
 140      private function getProxyClassName(Definition $definition)
 141      {
 142          return preg_replace('/^.*\\\\/', '', $definition->getClass()).'_'.$this->getIdentifierSuffix($definition);
 143      }
 144  
 145      /**
 146       * @return ClassGenerator
 147       */
 148      private function generateProxyClass(Definition $definition)
 149      {
 150          $generatedClass = new ClassGenerator($this->getProxyClassName($definition));
 151  
 152          $this->proxyGenerator->generate(new \ReflectionClass($definition->getClass()), $generatedClass);
 153  
 154          return $generatedClass;
 155      }
 156  
 157      /**
 158       * @return string
 159       */
 160      private function getIdentifierSuffix(Definition $definition)
 161      {
 162          return substr(hash('sha256', $definition->getClass().$this->salt), -7);
 163      }
 164  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1