|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Yaroslav Honcharuk <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Yarhon\RouteGuardBundle\Twig\TokenParser; |
|
12
|
|
|
|
|
13
|
|
|
use Twig\TokenParser\AbstractTokenParser; |
|
14
|
|
|
use Twig\Token; |
|
15
|
|
|
use Twig\Parser; |
|
16
|
|
|
use Yarhon\RouteGuardBundle\Twig\Node\RouteNode; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class RouteTokenParser extends AbstractTokenParser |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $tagName; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $endTagName; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
private $allowDiscover; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var RouteExpressionParser |
|
40
|
|
|
*/ |
|
41
|
|
|
private $expressionParser; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $tagName |
|
45
|
|
|
* @param bool $allowDiscover |
|
46
|
|
|
*/ |
|
47
|
46 |
|
public function __construct($tagName, $allowDiscover = false) |
|
48
|
|
|
{ |
|
49
|
46 |
|
$this->tagName = $tagName; |
|
50
|
46 |
|
$this->endTagName = 'end'.$tagName; |
|
51
|
46 |
|
$this->allowDiscover = $allowDiscover; |
|
52
|
|
|
|
|
53
|
46 |
|
$this->expressionParser = new RouteExpressionParser(); |
|
54
|
46 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
39 |
|
public function setParser(Parser $parser) |
|
60
|
|
|
{ |
|
61
|
39 |
|
parent::setParser($parser); |
|
62
|
39 |
|
$this->expressionParser->setParser($parser); |
|
63
|
39 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
39 |
|
public function parse(Token $token) |
|
69
|
|
|
{ |
|
70
|
39 |
|
$condition = null; |
|
71
|
39 |
|
$elseNode = null; |
|
72
|
|
|
|
|
73
|
39 |
|
$parser = $this->parser; |
|
74
|
39 |
|
$stream = $parser->getStream(); |
|
75
|
|
|
|
|
76
|
39 |
|
if ($this->allowDiscover && $stream->test('discover')) { |
|
77
|
5 |
|
$stream->next(); |
|
78
|
|
|
} else { |
|
79
|
34 |
|
$condition = $this->expressionParser->parse($token); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
36 |
|
$stream->expect(Token::BLOCK_END_TYPE); |
|
83
|
|
|
|
|
84
|
|
|
$bodyNode = $parser->subparse(function (Token $token) { |
|
85
|
35 |
|
return $token->test(['else', $this->endTagName]); |
|
86
|
35 |
|
}); |
|
87
|
|
|
|
|
88
|
34 |
|
if ('else' === $stream->next()->getValue()) { |
|
89
|
20 |
|
$stream->expect(Token::BLOCK_END_TYPE); |
|
90
|
|
|
|
|
91
|
|
|
$elseNode = $parser->subparse(function (Token $token) { |
|
92
|
20 |
|
return $token->test([$this->endTagName]); |
|
93
|
20 |
|
}); |
|
94
|
|
|
|
|
95
|
20 |
|
$stream->expect($this->endTagName); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
34 |
|
$stream->expect(Token::BLOCK_END_TYPE); |
|
99
|
|
|
|
|
100
|
34 |
|
return new RouteNode($condition, $bodyNode, $elseNode, $token->getLine()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
44 |
|
public function getTag() |
|
107
|
|
|
{ |
|
108
|
44 |
|
return $this->tagName; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|