1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Templates System. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.ú. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Component\TemplatesSystem\Twig\Node; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Gimme twig node. |
19
|
|
|
*/ |
20
|
|
|
class GimmeListNode extends \Twig_Node |
21
|
|
|
{ |
22
|
|
|
protected static $count = 1; |
23
|
|
|
|
24
|
|
|
protected $loop; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* GimmeListNode constructor. |
28
|
|
|
* |
29
|
|
|
* @param \Twig_Node $variable |
30
|
|
|
* @param \Twig_Node $collectionType |
31
|
|
|
* @param \Twig_Node_Expression_Filter|null $collectionFilters |
32
|
|
|
* @param \Twig_Node_Expression|null $parameters |
33
|
|
|
* @param \Twig_Node_Expression|null $ignoreContext |
34
|
|
|
* @param \Twig_Node_Expression|null $ifExpression |
35
|
|
|
* @param \Twig_NodeInterface|null $else |
36
|
|
|
* @param \Twig_NodeInterface $body |
37
|
|
|
* @param $lineno |
38
|
|
|
* @param null $tag |
39
|
|
|
*/ |
40
|
3 |
|
public function __construct( |
41
|
|
|
\Twig_Node $variable, |
42
|
|
|
\Twig_Node $collectionType, |
43
|
|
|
\Twig_Node_Expression_Filter $collectionFilters = null, |
44
|
|
|
\Twig_Node_Expression $parameters = null, |
45
|
|
|
\Twig_Node_Expression $ignoreContext = null, |
46
|
|
|
\Twig_Node_Expression $ifExpression = null, |
47
|
|
|
\Twig_NodeInterface $else = null, |
48
|
|
|
\Twig_NodeInterface $body, |
49
|
|
|
$lineno, |
50
|
|
|
$tag = null |
51
|
|
|
) { |
52
|
3 |
|
$body = new \Twig_Node([$body, $this->loop = new \Twig_Node_ForLoop($lineno, $tag)]); |
53
|
|
|
|
54
|
3 |
|
if (null !== $ifExpression) { |
55
|
1 |
|
$body = new \Twig_Node_If(new \Twig_Node([$ifExpression, $body]), null, $lineno, $tag); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$nodes = [ |
59
|
3 |
|
'variable' => $variable, |
60
|
3 |
|
'collectionType' => $collectionType, |
61
|
3 |
|
'body' => $body, |
62
|
|
|
]; |
63
|
|
|
|
64
|
3 |
|
if (!is_null($parameters)) { |
65
|
3 |
|
$nodes['parameters'] = $parameters; |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
if (!is_null($ignoreContext)) { |
69
|
1 |
|
$nodes['ignoreContext'] = $ignoreContext; |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
if (!is_null($collectionFilters)) { |
73
|
1 |
|
$nodes['collectionFilters'] = $collectionFilters; |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
if (!is_null($ifExpression)) { |
77
|
1 |
|
$nodes['ifExpression'] = $ifExpression; |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
if (!is_null($else)) { |
81
|
1 |
|
$nodes['else'] = $else; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
parent::__construct($nodes, ['with_loop' => true, 'ifexpr' => null !== $ifExpression], $lineno, $tag); |
85
|
3 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
8 |
|
public function compile(\Twig_Compiler $compiler) |
91
|
|
|
{ |
92
|
8 |
|
$i = self::$count++; |
93
|
|
|
|
94
|
8 |
|
$collectionTypeName = $this->getNode('collectionType')->getNode(0)->getAttribute('name'); |
95
|
|
|
|
96
|
|
|
$compiler |
97
|
8 |
|
->addDebugInfo($this); |
98
|
|
|
|
99
|
8 |
|
if ($this->hasNode('collectionFilters')) { |
100
|
5 |
|
$compiler->write("\$context['_collection_type_filters'] = [];\n"); |
101
|
5 |
|
$compiler->write("\$context['".$collectionTypeName."'] = null;\n"); |
102
|
5 |
|
$compiler->write("\$context['_collection_type_filters'] = ")->subcompile($this->getNode('collectionFilters'))->raw("['_collection_type_filters']; unset(\$context['".$collectionTypeName."']['_collection_type_filters']);\n"); |
103
|
|
|
|
104
|
5 |
View Code Duplication |
if ($this->hasNode('parameters')) { |
|
|
|
|
105
|
4 |
|
$compiler->write('$parameters = array_merge(')->subcompile($this->getNode('parameters'))->raw(", \$context['_collection_type_filters']);\n"); |
106
|
|
|
} else { |
107
|
5 |
|
$compiler->write("\$parameters = \$context['_collection_type_filters'];\n"); |
108
|
|
|
} |
109
|
|
View Code Duplication |
} else { |
|
|
|
|
110
|
3 |
|
if ($this->hasNode('parameters')) { |
111
|
2 |
|
$compiler->raw('$parameters = ')->subcompile($this->getNode('parameters'))->raw(";\n"); |
112
|
|
|
} else { |
113
|
3 |
|
$compiler->raw("\$parameters = [];\n"); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
8 |
|
$compiler->write('$swpCollectionMetaLoader'.$i." = \$this->env->getExtension('swp_gimme')->getLoader();\n"); |
118
|
8 |
View Code Duplication |
if ($this->hasNode('ignoreContext')) { |
|
|
|
|
119
|
1 |
|
$compiler->write('$swpContext'.$i."GimmeList = \$this->env->getExtension('swp_gimme')->getContext();\n"); |
120
|
1 |
|
$compiler->write('$swpIgnoreContext'.$i.'GimmeList = $swpContext'.$i.'GimmeList->temporaryUnset(')->subcompile($this->getNode('ignoreContext'))->raw(");\n"); |
121
|
|
|
} |
122
|
8 |
|
$compiler->write('')->subcompile($this->getNode('collectionType'))->raw(' = twig_ensure_traversable($swpCollectionMetaLoader'.$i.'->load("')->raw($collectionTypeName)->raw('", '); |
123
|
8 |
|
$compiler->raw('$parameters'); |
124
|
8 |
|
$compiler->raw(", \SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface::COLLECTION));\n"); |
125
|
|
|
|
126
|
|
|
// the (array) cast bypasses a PHP 5.2.6 bug |
127
|
8 |
|
$compiler->write("\$context['_parent'] = (array) \$context;\n"); |
128
|
|
|
|
129
|
8 |
|
if ($this->hasNode('else')) { |
130
|
1 |
|
$compiler->write("\$context['_iterated'] = false;\n"); |
131
|
|
|
} |
132
|
|
|
|
133
|
8 |
|
if ($this->getAttribute('with_loop')) { |
134
|
|
|
$compiler |
135
|
8 |
|
->write("\$context['loop'] = array(\n") |
136
|
8 |
|
->write(" 'parent' => \$context['_parent'],\n") |
137
|
8 |
|
->write(" 'index0' => 0,\n") |
138
|
8 |
|
->write(" 'index' => 1,\n") |
139
|
8 |
|
->write(" 'first' => true,\n") |
140
|
8 |
|
->write(");\n"); |
141
|
|
|
|
142
|
8 |
|
if (!$this->getAttribute('ifexpr') && $this->getNode('collectionType')) { |
143
|
|
|
$compiler |
144
|
5 |
|
->write('if (is_array(')->subcompile($this->getNode('collectionType'))->raw(') || (is_object(')->subcompile($this->getNode('collectionType'))->raw(') && ')->subcompile($this->getNode('collectionType'))->raw(" instanceof Countable)) {\n") |
145
|
5 |
|
->indent() |
146
|
5 |
|
->write('$length = count(')->subcompile($this->getNode('collectionType'))->raw(");\n") |
147
|
5 |
|
->write("\$context['loop']['revindex0'] = \$length - 1;\n") |
148
|
5 |
|
->write("\$context['loop']['revindex'] = \$length;\n") |
149
|
5 |
|
->write("\$context['loop']['length'] = \$length;\n") |
150
|
5 |
|
->write("\$context['loop']['totalLength'] = \$length;\n") |
151
|
5 |
|
->write("\$context['loop']['last'] = 1 === \$length;\n") |
152
|
5 |
|
->outdent() |
153
|
5 |
|
->write("}\n"); |
154
|
|
|
|
155
|
|
|
$compiler |
156
|
5 |
|
->write('if(is_object(')->subcompile($this->getNode('collectionType'))->raw(') && ')->subcompile($this->getNode('collectionType'))->raw(" instanceof \SWP\Component\TemplatesSystem\Gimme\Meta\MetaCollection) {\n") |
157
|
5 |
|
->indent() |
158
|
5 |
|
->write('$context[\'loop\'][\'totalLength\'] = ')->subcompile($this->getNode('collectionType'))->raw("->getTotalItemsCount();\n") |
159
|
5 |
|
->outdent() |
160
|
5 |
|
->write("}\n"); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
8 |
|
$this->loop->setAttribute('else', $this->hasNode('else')); |
165
|
8 |
|
$this->loop->setAttribute('with_loop', $this->getAttribute('with_loop')); |
166
|
8 |
|
$this->loop->setAttribute('ifexpr', $this->getAttribute('ifexpr')); |
167
|
|
|
|
168
|
8 |
|
if (null !== $this->getNode('collectionType')) { |
169
|
|
|
$compiler |
170
|
8 |
|
->write('foreach (') |
171
|
8 |
|
->subcompile($this->getNode('collectionType')) |
172
|
8 |
|
->raw(' as $_key') |
173
|
8 |
|
->raw(' => ') |
174
|
8 |
|
->subcompile($this->getNode('variable')) |
175
|
8 |
|
->raw(") {\n") |
176
|
8 |
|
->indent() |
177
|
8 |
|
->subcompile($this->getNode('body')) |
178
|
8 |
|
->outdent() |
179
|
8 |
|
->write("}\n"); |
180
|
|
|
} |
181
|
|
|
|
182
|
8 |
|
if ($this->hasNode('else')) { |
183
|
|
|
$compiler |
184
|
1 |
|
->write("if (!\$context['_iterated']) {\n") |
185
|
1 |
|
->indent() |
186
|
1 |
|
->subcompile($this->getNode('else')) |
187
|
1 |
|
->outdent() |
188
|
1 |
|
->write("}\n"); |
189
|
|
|
} |
190
|
|
|
|
191
|
8 |
|
if ($this->hasNode('ignoreContext')) { |
192
|
1 |
|
$compiler->write('$swpContext'.$i.'GimmeList->restoreTemporaryUnset($swpIgnoreContext'.$i."GimmeList);\n"); |
193
|
|
|
} |
194
|
8 |
|
$compiler->write("\$_parent = \$context['_parent'];\n"); |
195
|
|
|
|
196
|
|
|
// remove some "private" loop variables (needed for nested loops) |
197
|
8 |
|
$compiler->write('unset($context[\''.$this->getNode('variable')->getNode(0)->getAttribute('name').'\'], $context[\'_iterated\'], $context[\''.$collectionTypeName.'\'], $context[\'_parent\'], $context[\'loop\']);'."\n"); |
198
|
|
|
|
199
|
8 |
|
if ($this->hasNode('collectionFilters')) { |
200
|
5 |
|
$compiler->write("unset(\$context['_collection_type_filters']);\n"); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// keep the values set in the inner context for variables defined in the outer context |
204
|
8 |
|
$compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n"); |
205
|
8 |
|
} |
206
|
|
|
} |
207
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.