ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Twig_Parser Class Reference

Default parser implementation. More...

+ Inheritance diagram for Twig_Parser:
+ Collaboration diagram for Twig_Parser:

Public Member Functions

 __construct (Twig_Environment $env)
 
 getEnvironment ()
 
 getVarName ()
 
 getFilename ()
 
 parse (Twig_TokenStream $stream, $test=null, $dropNeedle=false)
 
 subparse ($test, $dropNeedle=false)
 
 addHandler ($name, $class)
 
 addNodeVisitor (Twig_NodeVisitorInterface $visitor)
 
 getBlockStack ()
 
 peekBlockStack ()
 
 popBlockStack ()
 
 pushBlockStack ($name)
 
 hasBlock ($name)
 
 getBlock ($name)
 
 setBlock ($name, Twig_Node_Block $value)
 
 hasMacro ($name)
 
 setMacro ($name, Twig_Node_Macro $node)
 
 isReservedMacroName ($name)
 
 addTrait ($trait)
 
 hasTraits ()
 
 embedTemplate (Twig_Node_Module $template)
 
 addImportedSymbol ($type, $alias, $name=null, Twig_Node_Expression $node=null)
 
 getImportedSymbol ($type, $alias)
 
 isMainScope ()
 
 pushLocalScope ()
 
 popLocalScope ()
 
 getExpressionParser ()
 
 getParent ()
 
 setParent ($parent)
 
 getStream ()
 
 getCurrentToken ()
 
- Public Member Functions inherited from Twig_ParserInterface
 parse (Twig_TokenStream $stream)
 Converts a token stream to a node tree. More...
 

Protected Member Functions

 filterBodyNodes (Twig_NodeInterface $node)
 

Protected Attributes

 $stack = array()
 
 $stream
 
 $parent
 
 $handlers
 
 $visitors
 
 $expressionParser
 
 $blocks
 
 $blockStack
 
 $macros
 
 $env
 
 $reservedMacroNames
 
 $importedSymbols
 
 $traits
 
 $embeddedTemplates = array()
 

Private Attributes

 $varNameSalt = 0
 

Detailed Description

Default parser implementation.

Author
Fabien Potencier fabie.nosp@m.n@sy.nosp@m.mfony.nosp@m..com

Definition at line 18 of file Parser.php.

Constructor & Destructor Documentation

◆ __construct()

Twig_Parser::__construct ( Twig_Environment  $env)

Definition at line 36 of file Parser.php.

37 {
38 $this->env = $env;
39 }

References $env.

Member Function Documentation

◆ addHandler()

Twig_Parser::addHandler (   $name,
  $class 
)
Deprecated:
since 1.27 (to be removed in 2.0)

Definition at line 213 of file Parser.php.

214 {
215 @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0.', E_USER_DEPRECATED);
216
217 $this->handlers[$name] = $class;
218 }
if($format !==null) $name
Definition: metadata.php:146

References $name.

◆ addImportedSymbol()

Twig_Parser::addImportedSymbol (   $type,
  $alias,
  $name = null,
Twig_Node_Expression  $node = null 
)

Definition at line 313 of file Parser.php.

314 {
315 $this->importedSymbols[0][$type][$alias] = array('name' => $name, 'node' => $node);
316 }
$type

References $name, and $type.

◆ addNodeVisitor()

Twig_Parser::addNodeVisitor ( Twig_NodeVisitorInterface  $visitor)
Deprecated:
since 1.27 (to be removed in 2.0)

Definition at line 223 of file Parser.php.

224 {
225 @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0.', E_USER_DEPRECATED);
226
227 $this->visitors[] = $visitor;
228 }

◆ addTrait()

Twig_Parser::addTrait (   $trait)

Definition at line 296 of file Parser.php.

297 {
298 $this->traits[] = $trait;
299 }

◆ embedTemplate()

Twig_Parser::embedTemplate ( Twig_Node_Module  $template)

Definition at line 306 of file Parser.php.

307 {
308 $template->setIndex(mt_rand());
309
310 $this->embeddedTemplates[] = $template;
311 }
$template

References $template.

◆ filterBodyNodes()

Twig_Parser::filterBodyNodes ( Twig_NodeInterface  $node)
protected

Reimplemented in TestParser.

Definition at line 376 of file Parser.php.

377 {
378 // check that the body does not contain non-empty output nodes
379 if (
380 ($node instanceof Twig_Node_Text && !ctype_space($node->getAttribute('data')))
381 ||
382 (!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface)
383 ) {
384 if (false !== strpos((string) $node, chr(0xEF).chr(0xBB).chr(0xBF))) {
385 throw new Twig_Error_Syntax('A template that extends another one cannot start with a byte order mark (BOM); it must be removed.', $node->getTemplateLine(), $this->stream->getSourceContext());
386 }
387
388 throw new Twig_Error_Syntax('A template that extends another one cannot include content outside Twig blocks. Did you forget to put the content inside a {% block %} tag?', $node->getTemplateLine(), $this->stream->getSourceContext());
389 }
390
391 // bypass nodes that will "capture" the output
392 if ($node instanceof Twig_NodeCaptureInterface) {
393 return $node;
394 }
395
396 if ($node instanceof Twig_NodeOutputInterface) {
397 return;
398 }
399
400 foreach ($node as $k => $n) {
401 if (null !== $n && null === $this->filterBodyNodes($n)) {
402 $node->removeNode($k);
403 }
404 }
405
406 return $node;
407 }
$n
Definition: RandomTest.php:85
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:19
Represents a block call node.
Represents a text node.
Definition: Text.php:19
filterBodyNodes(Twig_NodeInterface $node)
Definition: Parser.php:376
Represents a node that captures any nested displayable nodes.
Represents a displayable node in the AST.

References $n, and filterBodyNodes().

Referenced by filterBodyNodes(), and parse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getBlock()

Twig_Parser::getBlock (   $name)

Definition at line 255 of file Parser.php.

256 {
257 return $this->blocks[$name];
258 }

References $name.

◆ getBlockStack()

Twig_Parser::getBlockStack ( )

Definition at line 230 of file Parser.php.

231 {
232 return $this->blockStack;
233 }

References $blockStack.

◆ getCurrentToken()

Twig_Parser::getCurrentToken ( )
Returns
Twig_Token

Definition at line 371 of file Parser.php.

372 {
373 return $this->stream->getCurrent();
374 }

Referenced by subparse().

+ Here is the caller graph for this function:

◆ getEnvironment()

Twig_Parser::getEnvironment ( )
Deprecated:
since 1.27 (to be removed in 2.0)

Definition at line 44 of file Parser.php.

45 {
46 @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0.', E_USER_DEPRECATED);
47
48 return $this->env;
49 }

References $env.

◆ getExpressionParser()

Twig_Parser::getExpressionParser ( )
Returns
Twig_ExpressionParser

Definition at line 345 of file Parser.php.

346 {
348 }
$expressionParser
Definition: Parser.php:25

References $expressionParser.

◆ getFilename()

Twig_Parser::getFilename ( )
Deprecated:
since 1.27 (to be removed in 2.0). Use $parser->getStream()->getSourceContext()->getPath() instead.

Definition at line 59 of file Parser.php.

60 {
61 @trigger_error(sprintf('The "%s" method is deprecated since version 1.27 and will be removed in 2.0. Use $parser->getStream()->getSourceContext()->getPath() instead.', __METHOD__), E_USER_DEPRECATED);
62
63 return $this->stream->getSourceContext()->getName();
64 }
sprintf('%.4f', $callTime)

References sprintf.

◆ getImportedSymbol()

Twig_Parser::getImportedSymbol (   $type,
  $alias 
)

Definition at line 318 of file Parser.php.

319 {
320 foreach ($this->importedSymbols as $functions) {
321 if (isset($functions[$type][$alias])) {
322 return $functions[$type][$alias];
323 }
324 }
325 }

References $type.

◆ getParent()

Twig_Parser::getParent ( )

Definition at line 350 of file Parser.php.

351 {
352 return $this->parent;
353 }

References $parent.

◆ getStream()

Twig_Parser::getStream ( )
Returns
Twig_TokenStream

Definition at line 363 of file Parser.php.

364 {
365 return $this->stream;
366 }

References $stream.

◆ getVarName()

Twig_Parser::getVarName ( )

Definition at line 51 of file Parser.php.

52 {
53 return sprintf('__internal_%s', hash('sha256', __METHOD__.$this->stream->getSourceContext()->getCode().$this->varNameSalt++));
54 }
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406

References GuzzleHttp\Psr7\hash(), and sprintf.

+ Here is the call graph for this function:

◆ hasBlock()

Twig_Parser::hasBlock (   $name)

Definition at line 250 of file Parser.php.

251 {
252 return isset($this->blocks[$name]);
253 }

References $name.

◆ hasMacro()

Twig_Parser::hasMacro (   $name)

Definition at line 265 of file Parser.php.

266 {
267 return isset($this->macros[$name]);
268 }

References $name.

◆ hasTraits()

Twig_Parser::hasTraits ( )

Definition at line 301 of file Parser.php.

302 {
303 return count($this->traits) > 0;
304 }

◆ isMainScope()

Twig_Parser::isMainScope ( )

Definition at line 327 of file Parser.php.

328 {
329 return 1 === count($this->importedSymbols);
330 }

◆ isReservedMacroName()

Twig_Parser::isReservedMacroName (   $name)

Definition at line 279 of file Parser.php.

280 {
281 if (null === $this->reservedMacroNames) {
282 $this->reservedMacroNames = array();
283 $r = new ReflectionClass($this->env->getBaseTemplateClass());
284 foreach ($r->getMethods() as $method) {
285 $methodName = strtolower($method->getName());
286
287 if ('get' === substr($methodName, 0, 3) && isset($methodName[3])) {
288 $this->reservedMacroNames[] = substr($methodName, 3);
289 }
290 }
291 }
292
293 return in_array(strtolower($name), $this->reservedMacroNames);
294 }
$r
Definition: example_031.php:79

References $name, and $r.

Referenced by setMacro().

+ Here is the caller graph for this function:

◆ parse()

Twig_Parser::parse ( Twig_TokenStream  $stream,
  $test = null,
  $dropNeedle = false 
)

Definition at line 66 of file Parser.php.

67 {
68 // push all variables into the stack to keep the current state of the parser
69 // using get_object_vars() instead of foreach would lead to https://bugs.php.net/71336
70 // This hack can be removed when min version if PHP 7.0
71 $vars = array();
72 foreach ($this as $k => $v) {
73 $vars[$k] = $v;
74 }
75
76 unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser'], $vars['reservedMacroNames']);
77 $this->stack[] = $vars;
78
79 // tag handlers
80 if (null === $this->handlers) {
81 $this->handlers = $this->env->getTokenParsers();
82 $this->handlers->setParser($this);
83 }
84
85 // node visitors
86 if (null === $this->visitors) {
87 $this->visitors = $this->env->getNodeVisitors();
88 }
89
90 if (null === $this->expressionParser) {
91 $this->expressionParser = new Twig_ExpressionParser($this, $this->env);
92 }
93
94 $this->stream = $stream;
95 $this->parent = null;
96 $this->blocks = array();
97 $this->macros = array();
98 $this->traits = array();
99 $this->blockStack = array();
100 $this->importedSymbols = array(array());
101 $this->embeddedTemplates = array();
102 $this->varNameSalt = 0;
103
104 try {
105 $body = $this->subparse($test, $dropNeedle);
106
107 if (null !== $this->parent && null === $body = $this->filterBodyNodes($body)) {
108 $body = new Twig_Node();
109 }
110 } catch (Twig_Error_Syntax $e) {
111 if (!$e->getSourceContext()) {
112 $e->setSourceContext($this->stream->getSourceContext());
113 }
114
115 if (!$e->getTemplateLine()) {
116 $e->setTemplateLine($this->stream->getCurrent()->getLine());
117 }
118
119 throw $e;
120 }
121
122 $node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->embeddedTemplates, $stream->getSourceContext());
123
124 $traverser = new Twig_NodeTraverser($this->env, $this->visitors);
125
126 $node = $traverser->traverse($node);
127
128 // restore previous stack so previous parse() call can resume working
129 foreach (array_pop($this->stack) as $key => $val) {
130 $this->$key = $val;
131 }
132
133 return $node;
134 }
$test
Definition: Utf8Test.php:84
setSourceContext(Twig_Source $source=null)
Sets the source context of the Twig template where the error occurred.
Definition: Error.php:199
setTemplateLine($lineno)
Sets the template line where the error occurred.
Definition: Error.php:179
getSourceContext()
Gets the source context of the Twig template where the error occurred.
Definition: Error.php:191
getTemplateLine()
Gets the template line where the error occurred.
Definition: Error.php:169
Parses expressions.
Twig_NodeTraverser is a node traverser.
Represents a body node.
Definition: Body.php:18
Represents a module node.
Definition: Module.php:23
Represents a node in the AST.
Definition: Node.php:19
subparse($test, $dropNeedle=false)
Definition: Parser.php:136
$key
Definition: croninfo.php:18

References $key, $stream, $test, filterBodyNodes(), Twig_Error\getSourceContext(), Twig_Error\getTemplateLine(), Twig_Error\setSourceContext(), Twig_Error\setTemplateLine(), and subparse().

+ Here is the call graph for this function:

◆ peekBlockStack()

Twig_Parser::peekBlockStack ( )

Definition at line 235 of file Parser.php.

236 {
237 return $this->blockStack[count($this->blockStack) - 1];
238 }

◆ popBlockStack()

Twig_Parser::popBlockStack ( )

Definition at line 240 of file Parser.php.

241 {
242 array_pop($this->blockStack);
243 }

◆ popLocalScope()

Twig_Parser::popLocalScope ( )

Definition at line 337 of file Parser.php.

338 {
339 array_shift($this->importedSymbols);
340 }

◆ pushBlockStack()

Twig_Parser::pushBlockStack (   $name)

Definition at line 245 of file Parser.php.

246 {
247 $this->blockStack[] = $name;
248 }

References $name.

◆ pushLocalScope()

Twig_Parser::pushLocalScope ( )

Definition at line 332 of file Parser.php.

333 {
334 array_unshift($this->importedSymbols, array());
335 }

◆ setBlock()

Twig_Parser::setBlock (   $name,
Twig_Node_Block  $value 
)

Definition at line 260 of file Parser.php.

261 {
262 $this->blocks[$name] = new Twig_Node_Body(array($value), array(), $value->getTemplateLine());
263 }
getTemplateLine()
Definition: Node.php:121

References $name, and Twig_Node\getTemplateLine().

+ Here is the call graph for this function:

◆ setMacro()

Twig_Parser::setMacro (   $name,
Twig_Node_Macro  $node 
)

Definition at line 270 of file Parser.php.

271 {
272 if ($this->isReservedMacroName($name)) {
273 throw new Twig_Error_Syntax(sprintf('"%s" cannot be used as a macro name as it is a reserved keyword.', $name), $node->getTemplateLine(), $this->stream->getSourceContext());
274 }
275
276 $this->macros[$name] = $node;
277 }
isReservedMacroName($name)
Definition: Parser.php:279

References $name, Twig_Node\getTemplateLine(), isReservedMacroName(), and sprintf.

+ Here is the call graph for this function:

◆ setParent()

Twig_Parser::setParent (   $parent)

Definition at line 355 of file Parser.php.

356 {
357 $this->parent = $parent;
358 }

References $parent.

◆ subparse()

Twig_Parser::subparse (   $test,
  $dropNeedle = false 
)

Definition at line 136 of file Parser.php.

137 {
138 $lineno = $this->getCurrentToken()->getLine();
139 $rv = array();
140 while (!$this->stream->isEOF()) {
141 switch ($this->getCurrentToken()->getType()) {
143 $token = $this->stream->next();
144 $rv[] = new Twig_Node_Text($token->getValue(), $token->getLine());
145 break;
146
148 $token = $this->stream->next();
149 $expr = $this->expressionParser->parseExpression();
150 $this->stream->expect(Twig_Token::VAR_END_TYPE);
151 $rv[] = new Twig_Node_Print($expr, $token->getLine());
152 break;
153
155 $this->stream->next();
156 $token = $this->getCurrentToken();
157
158 if (Twig_Token::NAME_TYPE !== $token->getType()) {
159 throw new Twig_Error_Syntax('A block must start with a tag name.', $token->getLine(), $this->stream->getSourceContext());
160 }
161
162 if (null !== $test && call_user_func($test, $token)) {
163 if ($dropNeedle) {
164 $this->stream->next();
165 }
166
167 if (1 === count($rv)) {
168 return $rv[0];
169 }
170
171 return new Twig_Node($rv, array(), $lineno);
172 }
173
174 $subparser = $this->handlers->getTokenParser($token->getValue());
175 if (null === $subparser) {
176 if (null !== $test) {
177 $e = new Twig_Error_Syntax(sprintf('Unexpected "%s" tag', $token->getValue()), $token->getLine(), $this->stream->getSourceContext());
178
179 if (is_array($test) && isset($test[0]) && $test[0] instanceof Twig_TokenParserInterface) {
180 $e->appendMessage(sprintf(' (expecting closing tag for the "%s" tag defined near line %s).', $test[0]->getTag(), $lineno));
181 }
182 } else {
183 $e = new Twig_Error_Syntax(sprintf('Unknown "%s" tag.', $token->getValue()), $token->getLine(), $this->stream->getSourceContext());
184 $e->addSuggestions($token->getValue(), array_keys($this->env->getTags()));
185 }
186
187 throw $e;
188 }
189
190 $this->stream->next();
191
192 $node = $subparser->parse($token);
193 if (null !== $node) {
194 $rv[] = $node;
195 }
196 break;
197
198 default:
199 throw new Twig_Error_Syntax('Lexer or parser ended up in unsupported state.', $this->getCurrentToken()->getLine(), $this->stream->getSourceContext());
200 }
201 }
202
203 if (1 === count($rv)) {
204 return $rv[0];
205 }
206
207 return new Twig_Node($rv, array(), $lineno);
208 }
Represents a node that outputs an expression.
Definition: Print.php:19
getCurrentToken()
Definition: Parser.php:371
const VAR_START_TYPE
Definition: Token.php:29
const TEXT_TYPE
Definition: Token.php:27
const BLOCK_START_TYPE
Definition: Token.php:28
const VAR_END_TYPE
Definition: Token.php:31
const NAME_TYPE
Definition: Token.php:32
Interface implemented by token parsers.

References $test, Twig_Token\BLOCK_START_TYPE, getCurrentToken(), Twig_Token\NAME_TYPE, sprintf, Twig_Token\TEXT_TYPE, Twig_Token\VAR_END_TYPE, and Twig_Token\VAR_START_TYPE.

Referenced by parse().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $blocks

Twig_Parser::$blocks
protected

Definition at line 26 of file Parser.php.

◆ $blockStack

Twig_Parser::$blockStack
protected

Definition at line 27 of file Parser.php.

Referenced by getBlockStack().

◆ $embeddedTemplates

Twig_Parser::$embeddedTemplates = array()
protected

Definition at line 33 of file Parser.php.

◆ $env

Twig_Parser::$env
protected

Definition at line 29 of file Parser.php.

Referenced by __construct(), and getEnvironment().

◆ $expressionParser

Twig_Parser::$expressionParser
protected

Definition at line 25 of file Parser.php.

Referenced by getExpressionParser().

◆ $handlers

Twig_Parser::$handlers
protected

Definition at line 23 of file Parser.php.

◆ $importedSymbols

Twig_Parser::$importedSymbols
protected

Definition at line 31 of file Parser.php.

◆ $macros

Twig_Parser::$macros
protected

Definition at line 28 of file Parser.php.

◆ $parent

Twig_Parser::$parent
protected

Definition at line 22 of file Parser.php.

Referenced by getParent(), and setParent().

◆ $reservedMacroNames

Twig_Parser::$reservedMacroNames
protected

Definition at line 30 of file Parser.php.

◆ $stack

Twig_Parser::$stack = array()
protected

Definition at line 20 of file Parser.php.

◆ $stream

Twig_Parser::$stream
protected

Definition at line 21 of file Parser.php.

Referenced by getStream(), and parse().

◆ $traits

Twig_Parser::$traits
protected

Definition at line 32 of file Parser.php.

◆ $varNameSalt

Twig_Parser::$varNameSalt = 0
private

Definition at line 34 of file Parser.php.

◆ $visitors

Twig_Parser::$visitors
protected

Definition at line 24 of file Parser.php.


The documentation for this class was generated from the following file: