vendor/symfony/twig-bridge/NodeVisitor/Scope.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Twig\NodeVisitor;
  11. /**
  12.  * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  13.  */
  14. class Scope
  15. {
  16.     private ?self $parent;
  17.     private array $data = [];
  18.     private bool $left false;
  19.     public function __construct(self $parent null)
  20.     {
  21.         $this->parent $parent;
  22.     }
  23.     /**
  24.      * Opens a new child scope.
  25.      */
  26.     public function enter(): self
  27.     {
  28.         return new self($this);
  29.     }
  30.     /**
  31.      * Closes current scope and returns parent one.
  32.      */
  33.     public function leave(): ?self
  34.     {
  35.         $this->left true;
  36.         return $this->parent;
  37.     }
  38.     /**
  39.      * Stores data into current scope.
  40.      *
  41.      * @return $this
  42.      *
  43.      * @throws \LogicException
  44.      */
  45.     public function set(string $keymixed $value): static
  46.     {
  47.         if ($this->left) {
  48.             throw new \LogicException('Left scope is not mutable.');
  49.         }
  50.         $this->data[$key] = $value;
  51.         return $this;
  52.     }
  53.     /**
  54.      * Tests if a data is visible from current scope.
  55.      */
  56.     public function has(string $key): bool
  57.     {
  58.         if (\array_key_exists($key$this->data)) {
  59.             return true;
  60.         }
  61.         if (null === $this->parent) {
  62.             return false;
  63.         }
  64.         return $this->parent->has($key);
  65.     }
  66.     /**
  67.      * Returns data visible from current scope.
  68.      */
  69.     public function get(string $keymixed $default null): mixed
  70.     {
  71.         if (\array_key_exists($key$this->data)) {
  72.             return $this->data[$key];
  73.         }
  74.         if (null === $this->parent) {
  75.             return $default;
  76.         }
  77.         return $this->parent->get($key$default);
  78.     }
  79. }