vendor/ezsystems/ezpublish-kernel/eZ/Publish/Core/Repository/Values/GeneratorProxyTrait.php line 152

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace eZ\Publish\Core\Repository\Values;
  7. use Generator;
  8. /**
  9.  * Trait for proxies, covers all relevant magic methods and exposes private method to load object.
  10.  *
  11.  * Uses generator internally to be able to cleanly execute object load async on-demand.
  12.  *
  13.  * @internal Meant for internal use by Repository!
  14.  */
  15. trait GeneratorProxyTrait
  16. {
  17.     /**
  18.      * Overload this for correct type hint on object type.
  19.      *
  20.      * @var object|null
  21.      */
  22.     protected $object;
  23.     /**
  24.      * Needs to be protected as value objects often define this as well.
  25.      *
  26.      * @var mixed
  27.      */
  28.     protected $id;
  29.     /** @var Generator|null */
  30.     private $generator;
  31.     /**
  32.      * GeneratorProxyTrait constructor.
  33.      *
  34.      * @param Generator $generator
  35.      * @param mixed $id Object id to use for loading the object on demand.
  36.      */
  37.     public function __construct(Generator $generator$id)
  38.     {
  39.         $this->generator $generator;
  40.         $this->id $id;
  41.     }
  42.     public function __call($name$arguments)
  43.     {
  44.         if ($this->object === null) {
  45.             $this->loadObject();
  46.         }
  47.         return $this->object->$name(...$arguments);
  48.     }
  49.     public function __invoke(...$args)
  50.     {
  51.         if ($this->object === null) {
  52.             $this->loadObject();
  53.         }
  54.         return ($this->object)(...$args);
  55.     }
  56.     public function __get($name)
  57.     {
  58.         if ($name === 'id') {
  59.             return $this->id;
  60.         }
  61.         if ($this->object === null) {
  62.             $this->loadObject();
  63.         }
  64.         return $this->object->$name;
  65.     }
  66.     public function __isset($name)
  67.     {
  68.         if ($name === 'id') {
  69.             return true;
  70.         }
  71.         if ($this->object === null) {
  72.             $this->loadObject();
  73.         }
  74.         return isset($this->object->$name);
  75.     }
  76.     public function __unset($name)
  77.     {
  78.         if ($this->object === null) {
  79.             $this->loadObject();
  80.         }
  81.         unset($this->object->$name);
  82.     }
  83.     public function __set($name$value)
  84.     {
  85.         if ($this->object === null) {
  86.             $this->loadObject();
  87.         }
  88.         $this->object->$name $value;
  89.     }
  90.     public function __toString()
  91.     {
  92.         if ($this->object === null) {
  93.             $this->loadObject();
  94.         }
  95.         return (string)$this->object;
  96.     }
  97.     public function __sleep()
  98.     {
  99.         if ($this->object === null) {
  100.             $this->loadObject();
  101.         }
  102.         return ['object''id'];
  103.     }
  104.     public function __debugInfo()
  105.     {
  106.         if ($this->object === null) {
  107.             $this->loadObject();
  108.         }
  109.         return [
  110.             'object' => $this->object,
  111.             'id' => $this->id,
  112.         ];
  113.     }
  114.     /**
  115.      * Loads the generator to object value and unset's generator.
  116.      *
  117.      * Can only be called once, so check presence of $this->object before using it.
  118.      *
  119.      * This assumes generator is made to support bulk loading of several objects, and thus takes object id as input
  120.      * in order to return right object at a time, and thus performs two yields per item.
  121.      * -> See PR that came with this trait for example generators.
  122.      */
  123.     protected function loadObject()
  124.     {
  125.         $this->object $this->generator->send($this->id);
  126.         $this->generator->next();
  127.         unset($this->generator);
  128.     }
  129. }