[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/ProxyGenerator/LazyLoadingGhost/MethodGenerator/ -> MagicGet.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\InitializationTracker;
  26  use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\PrivatePropertiesMap;
  27  use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\ProtectedPropertiesMap;
  28  use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
  29  use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator;
  30  use ReflectionClass;
  31  use Zend\Code\Generator\MethodGenerator;
  32  use Zend\Code\Generator\PropertyGenerator;
  33  
  34  /**
  35   * Magic `__get` for lazy loading ghost objects
  36   *
  37   * @author Marco Pivetta <ocramius@gmail.com>
  38   * @license MIT
  39   */
  40  class MagicGet extends MagicMethodGenerator
  41  {
  42      /**
  43       * @var string
  44       */
  45      private $callParentTemplate =  <<<'PHP'
  46  $this->%s && ! $this->%s && $this->%s('__get', array('name' => $name));
  47  
  48  if (isset(self::$%s[$name])) {
  49      return $this->$name;
  50  }
  51  
  52  if (isset(self::$%s[$name])) {
  53      if ($this->%s) {
  54          return $this->$name;
  55      }
  56  
  57      // check protected property access via compatible class
  58      $callers      = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
  59      $caller       = isset($callers[1]) ? $callers[1] : [];
  60      $object       = isset($caller['object']) ? $caller['object'] : '';
  61      $expectedType = self::$%s[$name];
  62  
  63      if ($object instanceof $expectedType) {
  64          return $this->$name;
  65      }
  66  
  67      $class = isset($caller['class']) ? $caller['class'] : '';
  68  
  69      if ($class === $expectedType || is_subclass_of($class, $expectedType) || $class === 'ReflectionProperty') {
  70          return $this->$name;
  71      }
  72  } elseif (isset(self::$%s[$name])) {
  73      // check private property access via same class
  74      $callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
  75      $caller  = isset($callers[1]) ? $callers[1] : [];
  76      $class   = isset($caller['class']) ? $caller['class'] : '';
  77  
  78      static $accessorCache = [];
  79  
  80      if (isset(self::$%s[$name][$class])) {
  81          $cacheKey = $class . '#' . $name;
  82          $accessor = isset($accessorCache[$cacheKey])
  83              ? $accessorCache[$cacheKey]
  84              : $accessorCache[$cacheKey] = \Closure::bind(function & ($instance) use ($name) {
  85                  return $instance->$name;
  86              }, null, $class);
  87  
  88          return $accessor($this);
  89      }
  90  
  91      if ($this->%s || 'ReflectionProperty' === $class) {
  92          $tmpClass = key(self::$%s[$name]);
  93          $cacheKey = $tmpClass . '#' . $name;
  94          $accessor = isset($accessorCache[$cacheKey])
  95              ? $accessorCache[$cacheKey]
  96              : $accessorCache[$cacheKey] = \Closure::bind(function & ($instance) use ($name) {
  97                  return $instance->$name;
  98              }, null, $tmpClass);
  99  
 100          return $accessor($this);
 101      }
 102  }
 103  
 104  %s
 105  PHP;
 106  
 107      /**
 108       * @param ReflectionClass        $originalClass
 109       * @param PropertyGenerator      $initializerProperty
 110       * @param MethodGenerator        $callInitializer
 111       * @param PublicPropertiesMap    $publicProperties
 112       * @param ProtectedPropertiesMap $protectedProperties
 113       * @param PrivatePropertiesMap   $privateProperties
 114       * @param InitializationTracker  $initializationTracker
 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          InitializationTracker $initializationTracker
 127      ) {
 128          parent::__construct($originalClass, '__get', [new ParameterGenerator('name')]);
 129  
 130          $override = $originalClass->hasMethod('__get');
 131  
 132          $this->setDocBlock(($override ? "{@inheritDoc}\n" : '') . '@param string $name');
 133  
 134          $parentAccess = 'return parent::__get($name);';
 135  
 136          if (! $override) {
 137              $parentAccess = PublicScopeSimulator::getPublicAccessSimulationCode(
 138                  PublicScopeSimulator::OPERATION_GET,
 139                  'name'
 140              );
 141          }
 142  
 143          $this->setBody(sprintf(
 144              $this->callParentTemplate,
 145              $initializerProperty->getName(),
 146              $initializationTracker->getName(),
 147              $callInitializer->getName(),
 148              $publicProperties->getName(),
 149              $protectedProperties->getName(),
 150              $initializationTracker->getName(),
 151              $protectedProperties->getName(),
 152              $privateProperties->getName(),
 153              $privateProperties->getName(),
 154              $initializationTracker->getName(),
 155              $privateProperties->getName(),
 156              $parentAccess
 157          ));
 158      }
 159  }


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