aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/NodeVisitor/Scope.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/twig-bridge/Symfony/Bridge/Twig/NodeVisitor/Scope.php')
-rw-r--r--vendor/symfony/twig-bridge/Symfony/Bridge/Twig/NodeVisitor/Scope.php135
1 files changed, 135 insertions, 0 deletions
diff --git a/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/NodeVisitor/Scope.php b/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/NodeVisitor/Scope.php
new file mode 100644
index 00000000..ce27b6a6
--- /dev/null
+++ b/vendor/symfony/twig-bridge/Symfony/Bridge/Twig/NodeVisitor/Scope.php
@@ -0,0 +1,135 @@
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
12namespace Symfony\Bridge\Twig\NodeVisitor;
13
14/**
15 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
16 */
17class Scope
18{
19 /**
20 * @var Scope|null
21 */
22 private $parent;
23
24 /**
25 * @var Scope[]
26 */
27 private $children;
28
29 /**
30 * @var array
31 */
32 private $data;
33
34 /**
35 * @var boolean
36 */
37 private $left;
38
39 /**
40 * @param Scope $parent
41 */
42 public function __construct(Scope $parent = null)
43 {
44 $this->parent = $parent;
45 $this->left = false;
46 $this->data = array();
47 }
48
49 /**
50 * Opens a new child scope.
51 *
52 * @return Scope
53 */
54 public function enter()
55 {
56 $child = new self($this);
57 $this->children[] = $child;
58
59 return $child;
60 }
61
62 /**
63 * Closes current scope and returns parent one.
64 *
65 * @return Scope|null
66 */
67 public function leave()
68 {
69 $this->left = true;
70
71 return $this->parent;
72 }
73
74 /**
75 * Stores data into current scope.
76 *
77 * @param string $key
78 * @param mixed $value
79 *
80 * @throws \LogicException
81 *
82 * @return Scope Current scope
83 */
84 public function set($key, $value)
85 {
86 if ($this->left) {
87 throw new \LogicException('Left scope is not mutable.');
88 }
89
90 $this->data[$key] = $value;
91
92 return $this;
93 }
94
95 /**
96 * Tests if a data is visible from current scope.
97 *
98 * @param string $key
99 *
100 * @return boolean
101 */
102 public function has($key)
103 {
104 if (array_key_exists($key, $this->data)) {
105 return true;
106 }
107
108 if (null === $this->parent) {
109 return false;
110 }
111
112 return $this->parent->has($key);
113 }
114
115 /**
116 * Returns data visible from current scope.
117 *
118 * @param string $key
119 * @param mixed $default
120 *
121 * @return mixed
122 */
123 public function get($key, $default = null)
124 {
125 if (array_key_exists($key, $this->data)) {
126 return $this->data[$key];
127 }
128
129 if (null === $this->parent) {
130 return $default;
131 }
132
133 return $this->parent->get($key, $default);
134 }
135}