[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

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


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