[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

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


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