-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWithTokenParser.php
86 lines (76 loc) · 2.25 KB
/
WithTokenParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* @copyright Zicht Online <https://zicht.nl>
*/
namespace Zicht\Bundle\FrameworkExtraBundle\Twig\ControlStructures;
use Twig_Token;
use Twig_NodeInterface;
/**
* The 'with' tag allows a scope-shift into a defined array. The format is as follows:
*
* {% with expr [as localName] [, expr2 [as localName2], [....]] {sandboxed|merged} %}
* content
* {% endwith %}
*/
class WithTokenParser extends \Twig_TokenParser
{
/** @var string[] */
private $options = [
'merged',
'sandboxed',
'always',
];
/**
* Gets the tag name associated with this token parser.
*
* @return string
*/
public function getTag()
{
return 'with';
}
/**
* Parses a token and returns a node.
*
* @param Twig_Token $token A Twig_Token instance
* @return Twig_NodeInterface A Twig_NodeInterface instance
*/
public function parse(Twig_Token $token)
{
$options = [];
$stream = $this->parser->getStream();
$start = $stream->getCurrent();
$arguments = [];
do {
$value = $this->parser->getExpressionParser()->parseExpression();
if ($stream->test('as')) {
$stream->expect('as');
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
} else {
$name = null;
}
$arguments[] = ['name' => $name, 'value' => $value];
$end = !$stream->test(Twig_Token::PUNCTUATION_TYPE, ',');
if (!$end) {
$stream->expect(Twig_Token::PUNCTUATION_TYPE, ',');
}
} while (!$end);
while ($stream->test($this->options)) {
$options[] = $stream->expect($this->options)->getValue();
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse([$this, 'decideWithEnd'], true);
$stream->expect(Twig_Token::BLOCK_END_TYPE);
return new WithNode($arguments, $body, $options, $start->getLine(), $start->getValue());
}
/**
* Checks for the end of the control structure.
*
* @param Twig_Token $token
* @return bool
*/
public function decideWithEnd($token)
{
return $token->test('endwith');
}
}