[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

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

   1  <?php
   2  /*
   3   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   4   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   5   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   6   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   7   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   8   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   9   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14   *
  15   * This software consists of voluntary contributions made by many individuals
  16   * and is licensed under the MIT license.
  17   */
  18  
  19  declare(strict_types=1);
  20  
  21  namespace ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator;
  22  
  23  use ProxyManager\Generator\MagicMethodGenerator;
  24  use Zend\Code\Generator\ParameterGenerator;
  25  use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\PrivatePropertiesMap;
  26  use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\ProtectedPropertiesMap;
  27  use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
  28  use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator;
  29  use ReflectionClass;
  30  use Zend\Code\Generator\MethodGenerator;
  31  use Zend\Code\Generator\PropertyGenerator;
  32  
  33  /**
  34   * Magic `__unset` method for lazy loading ghost objects
  35   *
  36   * @author Marco Pivetta <ocramius@gmail.com>
  37   * @license MIT
  38   */
  39  class MagicUnset extends MagicMethodGenerator
  40  {
  41      /**
  42       * @var string
  43       */
  44      private $callParentTemplate = <<<'PHP'
  45  %s
  46  
  47  if (isset(self::$%s[$name])) {
  48      unset($this->$name);
  49  
  50      return;
  51  }
  52  
  53  if (isset(self::$%s[$name])) {
  54      // check protected property access via compatible class
  55      $callers      = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
  56      $caller       = isset($callers[1]) ? $callers[1] : [];
  57      $object       = isset($caller['object']) ? $caller['object'] : '';
  58      $expectedType = self::$%s[$name];
  59  
  60      if ($object instanceof $expectedType) {
  61          unset($this->$name);
  62  
  63          return;
  64      }
  65  
  66      $class = isset($caller['class']) ? $caller['class'] : '';
  67  
  68      if ($class === $expectedType || is_subclass_of($class, $expectedType) || $class === 'ReflectionProperty') {
  69          unset($this->$name);
  70  
  71          return;
  72      }
  73  } elseif (isset(self::$%s[$name])) {
  74      // check private property access via same class
  75      $callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
  76      $caller  = isset($callers[1]) ? $callers[1] : [];
  77      $class   = isset($caller['class']) ? $caller['class'] : '';
  78  
  79      static $accessorCache = [];
  80  
  81      if (isset(self::$%s[$name][$class])) {
  82          $cacheKey = $class . '#' . $name;
  83          $accessor = isset($accessorCache[$cacheKey])
  84              ? $accessorCache[$cacheKey]
  85              : $accessorCache[$cacheKey] = \Closure::bind(function ($instance) use ($name) {
  86                  unset($instance->$name);
  87              }, null, $class);
  88  
  89          return $accessor($this);
  90      }
  91  
  92      if ('ReflectionProperty' === $class) {
  93          $tmpClass = key(self::$%s[$name]);
  94          $cacheKey = $tmpClass . '#' . $name;
  95          $accessor = isset($accessorCache[$cacheKey])
  96              ? $accessorCache[$cacheKey]
  97              : $accessorCache[$cacheKey] = \Closure::bind(function ($instance) use ($name) {
  98                  unset($instance->$name);
  99              }, null, $tmpClass);
 100  
 101          return $accessor($this);
 102      }
 103  }
 104  
 105  %s
 106  PHP;
 107  
 108      /**
 109       * @param ReflectionClass        $originalClass
 110       * @param PropertyGenerator      $initializerProperty
 111       * @param MethodGenerator        $callInitializer
 112       * @param PublicPropertiesMap    $publicProperties
 113       * @param ProtectedPropertiesMap $protectedProperties
 114       * @param PrivatePropertiesMap   $privateProperties
 115       *
 116       * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
 117       * @throws \InvalidArgumentException
 118       */
 119      public function __construct(
 120          ReflectionClass $originalClass,
 121          PropertyGenerator $initializerProperty,
 122          MethodGenerator $callInitializer,
 123          PublicPropertiesMap $publicProperties,
 124          ProtectedPropertiesMap $protectedProperties,
 125          PrivatePropertiesMap $privateProperties
 126      ) {
 127          parent::__construct($originalClass, '__unset', [new ParameterGenerator('name')]);
 128  
 129          $override = $originalClass->hasMethod('__unset');
 130  
 131          $this->setDocBlock(($override ? "{@inheritDoc}\n" : '') . '@param string $name');
 132  
 133          $parentAccess = 'return parent::__unset($name);';
 134  
 135          if (! $override) {
 136              $parentAccess = PublicScopeSimulator::getPublicAccessSimulationCode(
 137                  PublicScopeSimulator::OPERATION_UNSET,
 138                  'name'
 139              );
 140          }
 141  
 142          $this->setBody(sprintf(
 143              $this->callParentTemplate,
 144              '$this->' . $initializerProperty->getName() . ' && $this->' . $callInitializer->getName()
 145              . '(\'__unset\', array(\'name\' => $name));',
 146              $publicProperties->getName(),
 147              $protectedProperties->getName(),
 148              $protectedProperties->getName(),
 149              $privateProperties->getName(),
 150              $privateProperties->getName(),
 151              $privateProperties->getName(),
 152              $parentAccess
 153          ));
 154      }
 155  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1