Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Compiler.php
001 <?php
002
003 /*
004 * This file is part of Twig.
005 *
006 * (c) Fabien Potencier
007 * (c) Armin Ronacher
008 *
009 * For the full copyright and license information, please view the LICENSE
010 * file that was distributed with this source code.
011 */
012
013 namespace Twig;
014
015 use Twig\Node\Node;
016
017 /**
018 * Compiles a node to PHP code.
019 *
020 * @author Fabien Potencier <fabien@symfony.com>
021 */
022 class Compiler
023 {
024 private $lastLine;
025 private $source;
026 private $indentation;
027 private $env;
028 private $debugInfo = [];
029 private $sourceOffset;
030 private $sourceLine;
031 private $varNameSalt = 0;
032
033 public function __construct(Environment $env)
034 {
035 $this->env = $env;
036 }
037
038 /**
039 * Returns the environment instance related to this compiler.
040 *
041 * @return Environment
042 */
043 public function getEnvironment()
044 {
045 return $this->env;
046 }
047
048 /**
049 * Gets the current PHP code after compilation.
050 *
051 * @return string The PHP code
052 */
053 public function getSource()
054 {
055 return $this->source;
056 }
057
058 /**
059 * Compiles a node.
060 *
061 * @param int $indentation The current indentation
062 *
063 * @return $this
064 */
065 public function compile(Node $node, $indentation = 0)
066 {
067 $this->lastLine = null;
068 $this->source = '';
069 $this->debugInfo = [];
070 $this->sourceOffset = 0;
071 // source code starts at 1 (as we then increment it when we encounter new lines)
072 $this->sourceLine = 1;
073 $this->indentation = $indentation;
074 $this->varNameSalt = 0;
075
076 $node->compile($this);
077
078 return $this;
079 }
080
081 public function subcompile(Node $node, $raw = true)
082 {
083 if (false === $raw) {
084 $this->source .= str_repeat(' ', $this->indentation * 4);
085 }
086
087 $node->compile($this);
088
089 return $this;
090 }
091
092 /**
093 * Adds a raw string to the compiled code.
094 *
095 * @param string $string The string
096 *
097 * @return $this
098 */
099 public function raw($string)
100 {
101 $this->source .= $string;
102
103 return $this;
104 }
105
106 /**
107 * Writes a string to the compiled code by adding indentation.
108 *
109 * @return $this
110 */
111 public function write(...$strings)
112 {
113 foreach ($strings as $string) {
114 $this->source .= str_repeat(' ', $this->indentation * 4).$string;
115 }
116
117 return $this;
118 }
119
120 /**
121 * Adds a quoted string to the compiled code.
122 *
123 * @param string $value The string
124 *
125 * @return $this
126 */
127 public function string($value)
128 {
129 $this->source .= sprintf('"%s"', addcslashes($value, "\0\t\"\$\\"));
130
131 return $this;
132 }
133
134 /**
135 * Returns a PHP representation of a given value.
136 *
137 * @param mixed $value The value to convert
138 *
139 * @return $this
140 */
141 public function repr($value)
142 {
143 if (\is_int($value) || \is_float($value)) {
144 if (false !== $locale = setlocale(\LC_NUMERIC, '0')) {
145 setlocale(\LC_NUMERIC, 'C');
146 }
147
148 $this->raw(var_export($value, true));
149
150 if (false !== $locale) {
151 setlocale(\LC_NUMERIC, $locale);
152 }
153 } elseif (null === $value) {
154 $this->raw('null');
155 } elseif (\is_bool($value)) {
156 $this->raw($value ? 'true' : 'false');
157 } elseif (\is_array($value)) {
158 $this->raw('array(');
159 $first = true;
160 foreach ($value as $key => $v) {
161 if (!$first) {
162 $this->raw(', ');
163 }
164 $first = false;
165 $this->repr($key);
166 $this->raw(' => ');
167 $this->repr($v);
168 }
169 $this->raw(')');
170 } else {
171 $this->string($value);
172 }
173
174 return $this;
175 }
176
177 /**
178 * Adds debugging information.
179 *
180 * @return $this
181 */
182 public function addDebugInfo(Node $node)
183 {
184 if ($node->getTemplateLine() != $this->lastLine) {
185 $this->write(sprintf("// line %d\n", $node->getTemplateLine()));
186
187 $this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
188 $this->sourceOffset = \strlen($this->source);
189 $this->debugInfo[$this->sourceLine] = $node->getTemplateLine();
190
191 $this->lastLine = $node->getTemplateLine();
192 }
193
194 return $this;
195 }
196
197 public function getDebugInfo()
198 {
199 ksort($this->debugInfo);
200
201 return $this->debugInfo;
202 }
203
204 /**
205 * Indents the generated code.
206 *
207 * @param int $step The number of indentation to add
208 *
209 * @return $this
210 */
211 public function indent($step = 1)
212 {
213 $this->indentation += $step;
214
215 return $this;
216 }
217
218 /**
219 * Outdents the generated code.
220 *
221 * @param int $step The number of indentation to remove
222 *
223 * @return $this
224 *
225 * @throws \LogicException When trying to outdent too much so the indentation would become negative
226 */
227 public function outdent($step = 1)
228 {
229 // can't outdent by more steps than the current indentation level
230 if ($this->indentation < $step) {
231 throw new \LogicException('Unable to call outdent() as the indentation would become negative.');
232 }
233
234 $this->indentation -= $step;
235
236 return $this;
237 }
238
239 public function getVarName()
240 {
241 return sprintf('__internal_compile_%d', $this->varNameSalt++);
242 }
243 }
244
245 class_alias('Twig\Compiler', 'Twig_Compiler');
246