|
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\Node; |
|
12
|
|
|
|
|
13
|
|
|
use Twig\Compiler; |
|
14
|
|
|
use Twig\Node\Node; |
|
15
|
|
|
use Twig\Node\Expression\AssignNameExpression; |
|
16
|
|
|
use Twig\Error\SyntaxError; |
|
17
|
|
|
use Twig\Error\RuntimeError; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class RouteNode extends Node |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private static $variableName; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param RouteExpression|null $condition |
|
31
|
|
|
* @param Node $bodyNode |
|
32
|
|
|
* @param Node|null $elseNode |
|
33
|
|
|
* @param int $line |
|
34
|
|
|
*/ |
|
35
|
36 |
|
public function __construct(RouteExpression $condition = null, Node $bodyNode, Node $elseNode = null, $line = 0) |
|
36
|
|
|
{ |
|
37
|
|
|
$nodes = [ |
|
38
|
36 |
|
'body' => $bodyNode, |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
36 |
|
if ($condition) { |
|
42
|
30 |
|
$nodes['condition'] = $condition; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
36 |
|
if ($elseNode) { |
|
46
|
20 |
|
$nodes['else'] = $elseNode; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
36 |
|
parent::__construct($nodes, [], $line); |
|
50
|
36 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $variableName |
|
54
|
|
|
*/ |
|
55
|
50 |
|
public static function setVariableName($variableName) |
|
56
|
|
|
{ |
|
57
|
50 |
|
self::$variableName = $variableName; |
|
58
|
50 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
* |
|
63
|
|
|
* @throws SyntaxError |
|
64
|
|
|
* @throws RuntimeError |
|
65
|
|
|
*/ |
|
66
|
22 |
|
public function compile(Compiler $compiler) |
|
67
|
|
|
{ |
|
68
|
22 |
|
$compiler->addDebugInfo($this); |
|
69
|
|
|
|
|
70
|
22 |
|
if (!$this->hasNode('condition')) { |
|
71
|
1 |
|
throw new SyntaxError('Condition node is required.', $this->getTemplateLine()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
21 |
|
if (!self::$variableName) { |
|
75
|
1 |
|
throw new RuntimeError( |
|
76
|
1 |
|
sprintf('variableName is not set. setVariableName() method should be called before compiling.') |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
20 |
|
$variable = new AssignNameExpression(self::$variableName, 0); |
|
81
|
|
|
|
|
82
|
|
|
$compiler |
|
83
|
20 |
|
->subcompile($variable) |
|
84
|
20 |
|
->write(" = array();\n") |
|
85
|
20 |
|
->write('if (false !== (') |
|
86
|
20 |
|
->subcompile($variable) |
|
87
|
20 |
|
->write('["ref"] = ') |
|
88
|
20 |
|
->subcompile($this->getNode('condition')) |
|
89
|
20 |
|
->write(")) {\n") |
|
90
|
20 |
|
->indent() |
|
91
|
20 |
|
->subcompile($this->getNode('body')) |
|
92
|
|
|
; |
|
93
|
|
|
|
|
94
|
20 |
|
if ($this->hasNode('else')) { |
|
95
|
|
|
$compiler |
|
96
|
19 |
|
->outdent() |
|
97
|
19 |
|
->write("} else {\n") |
|
98
|
19 |
|
->indent() |
|
99
|
19 |
|
->subcompile($this->getNode('else')) |
|
100
|
|
|
; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$compiler |
|
104
|
20 |
|
->outdent() |
|
105
|
20 |
|
->write("}\n") |
|
106
|
20 |
|
->write('unset(') |
|
107
|
20 |
|
->subcompile($variable) |
|
108
|
20 |
|
->write(");\n"); |
|
109
|
20 |
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|