1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/twig-trigger/blob/master/LICENSE.md |
6
|
|
|
* @link https://github.com/flipboxfactory/twig-trigger |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\twig\tokenparsers; |
10
|
|
|
|
11
|
|
|
use flipbox\twig\nodes\TriggerNode; |
12
|
|
|
use Twig_Error_Syntax; |
13
|
|
|
use Twig_Node_Text; |
14
|
|
|
use Twig_Token; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class TriggerTokenParser extends \Twig_TokenParser |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @inheritdoc |
24
|
|
|
*/ |
25
|
|
|
public function getTag() |
26
|
|
|
{ |
27
|
|
|
return 'trigger'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritdoc |
32
|
|
|
*/ |
33
|
|
|
public function parse(Twig_Token $token) |
34
|
|
|
{ |
35
|
|
|
$line = $token->getLine(); |
36
|
|
|
$parser = $this->parser; |
37
|
|
|
|
38
|
|
|
$nodes = [ |
39
|
|
|
'event' => $this->parser->getExpressionParser()->parseExpression() |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
$variables = [ |
43
|
|
|
'capture' => true |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
// Look for value as an attribute |
47
|
|
|
$variables['capture'] = $this->parseValueAttribute($nodes); |
48
|
|
|
|
49
|
|
|
// Params 'with' |
50
|
|
|
$variables['params'] = $this->parseWith(); |
51
|
|
|
|
52
|
|
|
// Close out opening tag |
53
|
|
|
$parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); |
54
|
|
|
|
55
|
|
|
// Is there a closing tag? |
56
|
|
|
if ($variables['capture']) { |
57
|
|
|
$this->parseValueBetweenTags($nodes); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return new TriggerNode($nodes, $variables, $line, $this->getTag()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $nodes |
65
|
|
|
* @return bool |
66
|
|
|
* @throws Twig_Error_Syntax |
67
|
|
|
*/ |
68
|
|
|
protected function parseValueAttribute(array &$nodes): bool |
69
|
|
|
{ |
70
|
|
|
$parser = $this->parser; |
71
|
|
|
$stream = $parser->getStream(); |
72
|
|
|
$expressionParser = $this->parser->getExpressionParser(); |
73
|
|
|
|
74
|
|
|
// Look for value as a param |
75
|
|
|
if ($stream->nextIf(Twig_Token::NAME_TYPE, 'value')) { |
76
|
|
|
$stream->expect(Twig_Token::OPERATOR_TYPE, '='); |
77
|
|
|
|
78
|
|
|
$nodes['value'] = $expressionParser->parseExpression(); |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param array $nodes |
87
|
|
|
* @throws Twig_Error_Syntax |
88
|
|
|
*/ |
89
|
|
|
protected function parseValueBetweenTags(array &$nodes) |
90
|
|
|
{ |
91
|
|
|
$parser = $this->parser; |
92
|
|
|
$stream = $parser->getStream(); |
93
|
|
|
|
94
|
|
|
$nodes['value'] = $parser->subparse(array($this, 'decideBlockEnd'), true); |
95
|
|
|
$stream->expect(Twig_Token::BLOCK_END_TYPE); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return null|Twig_Node_Text |
100
|
|
|
* @throws Twig_Error_Syntax |
101
|
|
|
*/ |
102
|
|
|
protected function parseWith() |
103
|
|
|
{ |
104
|
|
|
$stream = $this->parser->getStream(); |
105
|
|
|
$expressionParser = $this->parser->getExpressionParser(); |
106
|
|
|
|
107
|
|
|
// Is there an options param? |
108
|
|
|
if ($stream->test(Twig_Token::NAME_TYPE, 'with')) { |
109
|
|
|
$stream->next(); |
110
|
|
|
return $expressionParser->parseExpression(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param Twig_Token $token |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function decideBlockEnd(Twig_Token $token): bool |
122
|
|
|
{ |
123
|
|
|
return $token->test('end' . strtolower($this->getTag())); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|