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