[ Index ]

PHP Cross Reference of phpBB-3.3.9-deutsch

title

Body

[close]

/vendor/ocramius/proxy-manager/src/ProxyManager/ProxyGenerator/LazyLoadingValueHolder/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\LazyLoadingValueHolder\MethodGenerator;
  22  
  23  use ProxyManager\Generator\MagicMethodGenerator;
  24  use Zend\Code\Generator\ParameterGenerator;
  25  use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
  26  use ProxyManager\ProxyGenerator\Util\PublicScopeSimulator;
  27  use ReflectionClass;
  28  use Zend\Code\Generator\PropertyGenerator;
  29  
  30  /**
  31   * Magic `__get` for lazy loading value holder objects
  32   *
  33   * @author Marco Pivetta <ocramius@gmail.com>
  34   * @license MIT
  35   */
  36  class MagicGet extends MagicMethodGenerator
  37  {
  38      /**
  39       * Constructor
  40       *
  41       * @param ReflectionClass     $originalClass
  42       * @param PropertyGenerator   $initializerProperty
  43       * @param PropertyGenerator   $valueHolderProperty
  44       * @param PublicPropertiesMap $publicProperties
  45       *
  46       * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
  47       * @throws \InvalidArgumentException
  48       */
  49      public function __construct(
  50          ReflectionClass $originalClass,
  51          PropertyGenerator $initializerProperty,
  52          PropertyGenerator $valueHolderProperty,
  53          PublicPropertiesMap $publicProperties
  54      ) {
  55          parent::__construct($originalClass, '__get', [new ParameterGenerator('name')]);
  56  
  57          $hasParent = $originalClass->hasMethod('__get');
  58  
  59          $this->setDocBlock(($hasParent ? "{@inheritDoc}\n" : '') . '@param string $name');
  60  
  61          $initializer = $initializerProperty->getName();
  62          $valueHolder = $valueHolderProperty->getName();
  63          $callParent  = 'if (isset(self::$' . $publicProperties->getName() . "[\$name])) {\n"
  64              . '    return $this->' . $valueHolder . '->$name;'
  65              . "\n}\n\n";
  66  
  67          if ($hasParent) {
  68              $this->setInitializerBody(
  69                  $initializer,
  70                  $valueHolder,
  71                  $callParent . 'return $this->' . $valueHolder . '->__get($name);'
  72              );
  73  
  74              return;
  75          }
  76  
  77          $this->setInitializerBody(
  78              $initializer,
  79              $valueHolder,
  80              $callParent . PublicScopeSimulator::getPublicAccessSimulationCode(
  81                  PublicScopeSimulator::OPERATION_GET,
  82                  'name',
  83                  null,
  84                  $valueHolderProperty
  85              )
  86          );
  87      }
  88  
  89      private function setInitializerBody(string $initializer, string $valueHolder, string $callParent) : void
  90      {
  91          $this->setBody(
  92              '$this->' . $initializer . ' && $this->' . $initializer
  93              . '->__invoke($this->' . $valueHolder . ', $this, \'__get\', [\'name\' => $name], $this->'
  94              . $initializer . ');'
  95              . "\n\n" . $callParent
  96          );
  97      }
  98  }


Generated: Wed Dec 7 15:09:22 2022 Cross-referenced by PHPXref 0.7.1