[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ -> MagicGet.php (source)

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator;
   6  
   7  use ProxyManager\Generator\MagicMethodGenerator;
   8  use Zend\Code\Generator\ParameterGenerator;
   9  use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\InitializationTracker;
  10  use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\PrivatePropertiesMap;
  11  use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\ProtectedPropertiesMap;
  12  use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
  13  use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator;
  14  use ReflectionClass;
  15  use Zend\Code\Generator\MethodGenerator;
  16  use Zend\Code\Generator\PropertyGenerator;
  17  
  18  /**
  19   * Magic `__get` for lazy loading ghost objects
  20   *
  21   * @author Marco Pivetta <ocramius@gmail.com>
  22   * @license MIT
  23   */
  24  class MagicGet extends MagicMethodGenerator
  25  {
  26      /**
  27       * @var string
  28       */
  29      private $callParentTemplate =  <<<'PHP'
  30  $this->%s && ! $this->%s && $this->%s('__get', array('name' => $name));
  31  
  32  if (isset(self::$%s[$name])) {
  33      return $this->$name;
  34  }
  35  
  36  if (isset(self::$%s[$name])) {
  37      if ($this->%s) {
  38          return $this->$name;
  39      }
  40  
  41      // check protected property access via compatible class
  42      $callers      = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
  43      $caller       = isset($callers[1]) ? $callers[1] : [];
  44      $object       = isset($caller['object']) ? $caller['object'] : '';
  45      $expectedType = self::$%s[$name];
  46  
  47      if ($object instanceof $expectedType) {
  48          return $this->$name;
  49      }
  50  
  51      $class = isset($caller['class']) ? $caller['class'] : '';
  52  
  53      if ($class === $expectedType || is_subclass_of($class, $expectedType) || $class === 'ReflectionProperty') {
  54          return $this->$name;
  55      }
  56  } elseif (isset(self::$%s[$name])) {
  57      // check private property access via same class
  58      $callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
  59      $caller  = isset($callers[1]) ? $callers[1] : [];
  60      $class   = isset($caller['class']) ? $caller['class'] : '';
  61  
  62      static $accessorCache = [];
  63  
  64      if (isset(self::$%s[$name][$class])) {
  65          $cacheKey = $class . '#' . $name;
  66          $accessor = isset($accessorCache[$cacheKey])
  67              ? $accessorCache[$cacheKey]
  68              : $accessorCache[$cacheKey] = \Closure::bind(function & ($instance) use ($name) {
  69                  return $instance->$name;
  70              }, null, $class);
  71  
  72          return $accessor($this);
  73      }
  74  
  75      if ($this->%s || 'ReflectionProperty' === $class) {
  76          $tmpClass = key(self::$%s[$name]);
  77          $cacheKey = $tmpClass . '#' . $name;
  78          $accessor = isset($accessorCache[$cacheKey])
  79              ? $accessorCache[$cacheKey]
  80              : $accessorCache[$cacheKey] = \Closure::bind(function & ($instance) use ($name) {
  81                  return $instance->$name;
  82              }, null, $tmpClass);
  83  
  84          return $accessor($this);
  85      }
  86  }
  87  
  88  %s
  89  PHP;
  90  
  91      /**
  92       * @param ReflectionClass        $originalClass
  93       * @param PropertyGenerator      $initializerProperty
  94       * @param MethodGenerator        $callInitializer
  95       * @param PublicPropertiesMap    $publicProperties
  96       * @param ProtectedPropertiesMap $protectedProperties
  97       * @param PrivatePropertiesMap   $privateProperties
  98       * @param InitializationTracker  $initializationTracker
  99       *
 100       * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
 101       * @throws \InvalidArgumentException
 102       */
 103      public function __construct(
 104          ReflectionClass $originalClass,
 105          PropertyGenerator $initializerProperty,
 106          MethodGenerator $callInitializer,
 107          PublicPropertiesMap $publicProperties,
 108          ProtectedPropertiesMap $protectedProperties,
 109          PrivatePropertiesMap $privateProperties,
 110          InitializationTracker $initializationTracker
 111      ) {
 112          parent::__construct($originalClass, '__get', [new ParameterGenerator('name')]);
 113  
 114          $override = $originalClass->hasMethod('__get');
 115  
 116          $parentAccess = 'return parent::__get($name);';
 117  
 118          if (! $override) {
 119              $parentAccess = PublicScopeSimulator::getPublicAccessSimulationCode(
 120                  PublicScopeSimulator::OPERATION_GET,
 121                  'name'
 122              );
 123          }
 124  
 125          $this->setBody(sprintf(
 126              $this->callParentTemplate,
 127              $initializerProperty->getName(),
 128              $initializationTracker->getName(),
 129              $callInitializer->getName(),
 130              $publicProperties->getName(),
 131              $protectedProperties->getName(),
 132              $initializationTracker->getName(),
 133              $protectedProperties->getName(),
 134              $privateProperties->getName(),
 135              $privateProperties->getName(),
 136              $initializationTracker->getName(),
 137              $privateProperties->getName(),
 138              $parentAccess
 139          ));
 140      }
 141  }


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