|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright 2011 Johannes M. Schmitt <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace TwigJs\Compiler; |
|
20
|
|
|
|
|
21
|
|
|
use TwigJs\JsCompiler; |
|
22
|
|
|
use TwigJs\TypeCompilerInterface; |
|
23
|
|
|
|
|
24
|
|
|
class ForCompiler implements TypeCompilerInterface |
|
25
|
|
|
{ |
|
26
|
|
|
private $count = 0; |
|
27
|
|
|
|
|
28
|
|
|
public function getType() |
|
29
|
|
|
{ |
|
30
|
|
|
return 'Twig_Node_For'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!$node instanceof \Twig_Node_For) { |
|
36
|
|
|
throw new \RuntimeException( |
|
37
|
|
|
sprintf( |
|
38
|
|
|
'$node must be an instanceof of \For, but got "%s".', |
|
39
|
|
|
get_class($node) |
|
40
|
|
|
) |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$count = $this->count++; |
|
45
|
|
|
$suffix = $count > 0 ? $count : ''; |
|
46
|
|
|
$seqName = 'seq'.$suffix; |
|
47
|
|
|
$keyName = 'k'.$suffix; |
|
48
|
|
|
$valueName = 'v'.$suffix; |
|
49
|
|
|
$iteratedName = 'iterated'.$suffix; |
|
50
|
|
|
$loopName = 'loop'.$suffix; |
|
51
|
|
|
|
|
52
|
|
|
if ($count > 0) { |
|
53
|
|
|
$compiler->enterScope(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$compiler |
|
57
|
|
|
->setVar('_seq', $seqName) |
|
58
|
|
|
->setVar('_iterated', $iteratedName) |
|
59
|
|
|
->setVar('loop', $loopName) |
|
60
|
|
|
->addDebugInfo($node) |
|
61
|
|
|
; |
|
62
|
|
|
|
|
63
|
|
|
// keep parent reference as some might rely on this |
|
64
|
|
|
if (0 === $count) { |
|
65
|
|
|
$compiler->write("context['_parent'] = context;\n"); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$compiler |
|
69
|
|
|
->write("var $seqName = ") |
|
70
|
|
|
// ->write("\$context['_seq'] = twig_ensure_traversable(") |
|
|
|
|
|
|
71
|
|
|
->subcompile($node->getNode('seq')) |
|
72
|
|
|
// ->raw(");\n") |
|
|
|
|
|
|
73
|
|
|
->raw(";\n") |
|
74
|
|
|
; |
|
75
|
|
|
|
|
76
|
|
|
if ($this->hasElseNode($node)) { |
|
77
|
|
|
$compiler->write("var $iteratedName = false;\n"); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ($node->getAttribute('with_loop')) { |
|
81
|
|
|
$compiler |
|
82
|
|
|
->write("var $loopName = {\n") |
|
83
|
|
|
->indent() |
|
84
|
|
|
; |
|
85
|
|
|
|
|
86
|
|
|
if ($count > 0) { |
|
87
|
|
|
$parentSuffix = ($count-1 > 0) ? $count - 1 : ''; |
|
88
|
|
|
|
|
89
|
|
|
$compiler |
|
90
|
|
|
->write("'parent': loop$parentSuffix,\n") |
|
91
|
|
|
; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$compiler |
|
95
|
|
|
->write("'index0': 0,\n") |
|
96
|
|
|
->write("'index': 1,\n") |
|
97
|
|
|
->write("'first': true\n") |
|
98
|
|
|
->outdent() |
|
99
|
|
|
->write("};\n") |
|
100
|
|
|
; |
|
101
|
|
|
|
|
102
|
|
|
if (false === $node->getAttribute('ifexpr')) { |
|
103
|
|
|
$compiler |
|
104
|
|
|
->write("if (twig.countable($seqName)) {\n") |
|
105
|
|
|
->indent() |
|
106
|
|
|
->write("var length = twig.count($seqName);\n") |
|
107
|
|
|
->write("{$loopName}['revindex0'] = length - 1;\n") |
|
108
|
|
|
->write("{$loopName}['revindex'] = length;\n") |
|
109
|
|
|
->write("{$loopName}['length'] = length;\n") |
|
110
|
|
|
->write("{$loopName}['last'] = 1 === length;\n") |
|
111
|
|
|
->outdent() |
|
112
|
|
|
->write("}\n") |
|
113
|
|
|
; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$ref = new \ReflectionProperty($node, 'loop'); |
|
118
|
|
|
$ref->setAccessible(true); |
|
119
|
|
|
$loop = $ref->getValue($node); |
|
120
|
|
|
$loop->setAttribute('else', $this->hasElseNode($node)); |
|
121
|
|
|
$loop->setAttribute('with_loop', $node->getAttribute('with_loop')); |
|
122
|
|
|
$loop->setAttribute('ifexpr', $node->getAttribute('ifexpr')); |
|
123
|
|
|
|
|
124
|
|
|
$compiler |
|
125
|
|
|
->write("twig.forEach($seqName, function($valueName, $keyName) {\n") |
|
126
|
|
|
->indent() |
|
127
|
|
|
->write("") |
|
128
|
|
|
->subcompile($node->getNode('key_target')) |
|
129
|
|
|
->raw(" = $keyName;\n") |
|
130
|
|
|
->write("") |
|
131
|
|
|
->subcompile($node->getNode('value_target')) |
|
132
|
|
|
->raw(" = $valueName;\n") |
|
133
|
|
|
->subcompile($node->getNode('body')) |
|
134
|
|
|
->outdent() |
|
135
|
|
|
->write("}, this);\n") |
|
136
|
|
|
; |
|
137
|
|
|
|
|
138
|
|
View Code Duplication |
if ($this->hasElseNode($node)) { |
|
|
|
|
|
|
139
|
|
|
$compiler |
|
140
|
|
|
->write("if (!$iteratedName) {\n") |
|
141
|
|
|
->indent() |
|
142
|
|
|
->subcompile($node->getNode('else')) |
|
143
|
|
|
->outdent() |
|
144
|
|
|
->write("}\n") |
|
145
|
|
|
; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if ($count > 0) { |
|
149
|
|
|
$compiler->leaveScope(); |
|
150
|
|
|
} |
|
151
|
|
|
$this->count = $count; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
View Code Duplication |
private function hasElseNode(\Twig_NodeInterface $node) |
|
|
|
|
|
|
155
|
|
|
{ |
|
156
|
|
|
if (!$node->hasNode('else')) { |
|
157
|
|
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if (null === $node->getNode('else')) { |
|
|
|
|
|
|
161
|
|
|
return false; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return true; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.