[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/NodeVisitor/ -> Scope.php (source)

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1