1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bolt\Twig; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Adapted from code originally in Twig/extensions. |
7
|
|
|
* |
8
|
|
|
* Usage: |
9
|
|
|
* |
10
|
|
|
* {% set foo = 1 %} |
11
|
|
|
* {% switch foo %} |
12
|
|
|
* {% case 1 %} |
13
|
|
|
* Foo was equal to the number one. |
14
|
|
|
* {% case 2 %} |
15
|
|
|
* Foo was two. |
16
|
|
|
* {% default %} |
17
|
|
|
* This is the default fallback. |
18
|
|
|
* {% endswitch %} |
19
|
|
|
* |
20
|
|
|
* |
21
|
|
|
* @see: https://gist.github.com/maxgalbu/9409182 |
22
|
|
|
*/ |
23
|
|
|
class SwitchTokenParser extends \Twig_TokenParser |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Parses a token and returns a node. |
27
|
|
|
* |
28
|
|
|
* @param Twig_Token $token A Twig_Token instance |
29
|
|
|
* |
30
|
|
|
* @return Twig_NodeInterface A Twig_NodeInterface instance |
31
|
|
|
*/ |
32
|
|
|
public function parse(\Twig_Token $token) |
33
|
|
|
{ |
34
|
|
|
$parser = $this->parser; |
35
|
|
|
$stream = $parser->getStream(); |
36
|
|
|
|
37
|
|
|
$default = null; |
38
|
|
|
$cases = []; |
39
|
|
|
$end = false; |
40
|
|
|
|
41
|
|
|
$name = $parser->getExpressionParser()->parseExpression(); |
42
|
|
|
$stream->expect(\Twig_Token::BLOCK_END_TYPE); |
43
|
|
|
$stream->expect(\Twig_Token::TEXT_TYPE); |
44
|
|
|
$stream->expect(\Twig_Token::BLOCK_START_TYPE); |
45
|
|
|
while (!$end) { |
46
|
|
|
$v = $stream->next(); |
47
|
|
|
switch ($v->getValue()) { |
48
|
|
|
case 'default': |
49
|
|
|
$stream->expect(\Twig_Token::BLOCK_END_TYPE); |
50
|
|
|
$default = $parser->subparse([$this, 'decideIfEnd']); |
51
|
|
|
break; |
52
|
|
|
|
53
|
|
|
case 'case': |
54
|
|
|
$expr = $parser->getExpressionParser()->parseExpression(); |
55
|
|
|
$stream->expect(\Twig_Token::BLOCK_END_TYPE); |
56
|
|
|
$body = $parser->subparse([$this, 'decideIfFork']); |
57
|
|
|
$cases[] = $expr; |
58
|
|
|
$cases[] = $body; |
59
|
|
|
break; |
60
|
|
|
|
61
|
|
|
case 'endswitch': |
62
|
|
|
$end = true; |
63
|
|
|
break; |
64
|
|
|
|
65
|
|
|
default: |
66
|
|
|
throw new \Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "case", "default", or "endswitch" to close the "switch" block started at line %d)', $lineno), -1); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$stream->expect(\Twig_Token::BLOCK_END_TYPE); |
71
|
|
|
|
72
|
|
|
return new SwitchNode($name, new \Twig_Node($cases), $default, $token->getLine(), $this->getTag()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function decideIfFork($token) |
76
|
|
|
{ |
77
|
|
|
return $token->test(['case', 'default', 'endswitch']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function decideIfEnd($token) |
81
|
|
|
{ |
82
|
|
|
return $token->test(['endswitch']); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Gets the tag name associated with this token parser. |
87
|
|
|
* |
88
|
|
|
* @param string The tag name |
89
|
|
|
*/ |
90
|
|
|
public function getTag() |
91
|
|
|
{ |
92
|
|
|
return 'switch'; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|