Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplatecompiledloop.php
Go to the documentation of this file.
1 <?php
16 class eZTemplateCompiledLoop
17 {
30 public function __construct( $name, &$newNodes, $parameters, $nodePlacement, $uniqid, $node, $tpl, $privateData )
31 {
32 $this->Name = $name;
33 $this->Parameters = $parameters;
34 $this->NodePlacement = $nodePlacement;
35 $this->UniqID = $uniqid;
36 $this->NewNodes =& $newNodes;
37 $this->Node = $node;
38 $this->Tpl = $tpl;
39 $this->PrivateData = $privateData;
40 }
41
45 function hasSequence()
46 {
47 return isset( $this->Parameters['sequence_var'] );
48 }
49
53 function cleanup()
54 {
55 if ( $this->hasSequence() )
56 $this->destroySequenceVars();
57
58 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$skipDelimiter = false;" );
59 }
60
64 function destroySequenceVars()
65 {
66 $fName = $this->Name;
67 $uniqid = $this->UniqID;
68 $this->NewNodes[] = eZTemplateNodeTool::createVariableUnsetNode( "{$fName}_sequence_array_$uniqid" );
69 $this->NewNodes[] = eZTemplateNodeTool::createVariableUnsetNode( "{$fName}_sequence_var_$uniqid" );
70 $this->NewNodes[] = eZTemplateNodeTool::createVariableUnsetNode( $this->Parameters['sequence_var'][0][1] );
71 }
72
73
77 function createSequenceVars()
78 {
79 if ( !$this->hasSequence() )
80 return;
81
82 $fName = $this->Name;
83 $uniqid = $this->UniqID;
84 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( "// creating sequence variables for \{$fName} loop" );
85 $this->NewNodes[] = eZTemplateNodeTool::createVariableNode( false,
86 $this->Parameters['sequence_array'],
87 $this->NodePlacement,
88 array( 'treat-value-as-non-object' => true, 'text-result' => false ),
89 "{$fName}_sequence_array_$uniqid" );
90 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( "\${$fName}_sequence_var_$uniqid = current( \${$fName}_sequence_array_$uniqid );\n" );
91 }
92
96 function setCurrentSequenceValue()
97 {
98 if ( !$this->hasSequence() )
99 return;
100
101 $fName = $this->Name;
102 $uniqid = $this->UniqID;
103 $seqVar = "{$fName}_sequence_var_$uniqid";
104 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( "// setting current sequence value" );
105 $this->NewNodes[] = eZTemplateNodeTool::createVariableNode( false, $seqVar, $this->NodePlacement, array(),
106 $this->Parameters['sequence_var'][0][1],
107 false, true, true );
108 }
109
113 function iterateSequence()
114 {
115 if ( !$this->hasSequence() )
116 return;
117
118 $fName = $this->Name;
119 $uniqid = $this->UniqID;
120 $seqArray = "{$fName}_sequence_array_$uniqid";
121 $seqVar = "{$fName}_sequence_var_$uniqid";
122 $alterSeqValCode =
123 "if ( ( \$$seqVar = next( \$$seqArray ) ) === false )\n" .
124 "{\n" .
125 " reset( \$$seqArray );\n" .
126 " \$$seqVar = current( \$$seqArray );\n" .
127 "}\n";
128 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( "// sequence iteration" );
129 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( $alterSeqValCode );
130 }
131
132 /*
133 * Compiles loop children (=code residing between start and end tags of the loop).
134 * Besides, does special handling of {break}, {continue}, {skip} and {delimiter} functions.
135 * \return true if the caller loop should break, false otherwise
136 */
137 function processChildren()
138 {
139 // process the loop body
140 $children = eZTemplateNodeTool::extractFunctionNodeChildren( $this->Node );
141 $transformedChildren = eZTemplateCompiler::processNodeTransformationNodes( $this->Tpl, $this->Node, $children, $this->PrivateData );
142
143 $childrenNodes = array();
144 $delimiter = null;
145
146 if ( is_array( $transformedChildren ) )
147 {
148 foreach ( $transformedChildren as $child )
149 {
150 if ( $child[0] == eZTemplate::NODE_FUNCTION ) // check child type
151 {
152 $childFunctionName = $child[2];
153 if ( $childFunctionName == 'delimiter' )
154 {
155 // save delimiter for it to be processed below
156 $delimiter = $child;
157 continue;
158 }
159 elseif ( $childFunctionName == 'break' )
160 {
161 $childrenNodes[] = eZTemplateNodeTool::createCodePieceNode( "break;\n" );
162 continue;
163 }
164 elseif ( $childFunctionName == 'continue' )
165 {
166 $childrenNodes[] = eZTemplateNodeTool::createCodePieceNode( "continue;\n" );
167 continue;
168 }
169 elseif ( $childFunctionName == 'skip' )
170 {
171 $childrenNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$skipDelimiter = true;\ncontinue;\n" );
172 continue;
173 }
174 }
175
176 $childrenNodes[] = $child;
177 }
178 }
179
180 if ( $delimiter ) // if delimiter is specified
181 {
182 $delimiterNodes = eZTemplateNodeTool::extractNodes( $children,
183 array( 'match' => array( 'type' => 'equal',
184 'matches' => array( array( 'match-keys' => array( 0 ),
185 'match-with' => eZTemplate::NODE_FUNCTION ),
186 array( 'match-keys' => array( 2 ),
187 'match-with' => 'delimiter' ) ) ) ) );
188 $delimiterNode = false;
189 if ( count( $delimiterNodes ) > 0 )
190 $delimiterNode = $delimiterNodes[0];
191
192 $delimiterChildren = eZTemplateNodeTool::extractFunctionNodeChildren( $delimiterNode );
193 $delimiterParameters = eZTemplateNodeTool::extractFunctionNodeParameters( $delimiterNode );
194
195 $checkModulo = array();
196 $checkModuloEnd = array();
197 $delemiterModuloValue = array();
198 if ( isset( $delimiterParameters['modulo'] ) )
199 {
200 switch ( $this->Name )
201 {
202 case 'foreach':
203 {
204 $delimiterModulo = $delimiterParameters['modulo'];
205 $delimiterModulo = eZTemplateCompiler::processElementTransformationList( $this->Tpl, $delimiterModulo, $delimiterModulo, $this->PrivateData );
206 // Get unique index
207 $currentIndex = "\$fe_i_$this->UniqID";
208
209 if ( eZTemplateNodeTool::isConstantElement( $delimiterModulo ) )
210 {
211 $moduloValue = (int)eZTemplateNodeTool::elementConstantValue( $delimiterModulo );
212 $matchCode = "( ( $currentIndex ) % $moduloValue ) == 0";
213 }
214 else
215 {
216 $delemiterModuloValue[] = eZTemplateNodeTool::createVariableNode( false, $delimiterModulo, eZTemplateNodeTool::extractFunctionNodePlacement( $this->Node ),
217 array( 'spacing' => 0 ), 'moduloValue' );
218 $matchCode = "( ( $currentIndex ) % \$moduloValue ) == 0";
219 }
220 $checkModulo[] = eZTemplateNodeTool::createCodePieceNode( "if ( $matchCode ) // Check modulo\n{" );
221 $checkModulo[] = eZTemplateNodeTool::createSpacingIncreaseNode( 4 );
222
223 $checkModuloEnd[] = eZTemplateNodeTool::createSpacingDecreaseNode( 4 );
224 $checkModuloEnd[] = eZTemplateNodeTool::createCodePieceNode( "}\n" );
225 }
226 }
227 }
228 $delimiterNodes = array();
229 $delimiterNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( \$skipDelimiter )\n" .
230 " \$skipDelimiter = false;\n" .
231 "else\n" .
232 "{ // delimiter begins" );
233 $delimiterNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
234 if ( is_array( $delimiter[1] ) ) // if delimiter has children
235 {
236 // If modulo is specified
237 $delimiterNodes = array_merge( $delimiterNodes, $checkModulo );
238
239 foreach ( $delimiter[1] as $delimiterChild )
240 $delimiterNodes[] = $delimiterChild;
241
242 // Set end of checking for modulo
243 $delimiterNodes = array_merge( $delimiterNodes, $checkModuloEnd );
244 }
245
246 $delimiterNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
247 $delimiterNodes[] = eZTemplateNodeTool::createCodePieceNode( "} // delimiter ends\n" );
248
249 // we place its code right before other loop children,
250 // if delemiter and modulo are specified and value of modulo is not static
251 // $delemiterModuloValue is initialization of variable
252 // we should place initialization of moduloValue before checking for delimiter
253 $childrenNodes = array_merge( $delemiterModuloValue, $delimiterNodes, $childrenNodes );
254 }
255
256 $this->NewNodes = array_merge( $this->NewNodes, $childrenNodes );
257 }
258
262 function processBody()
263 {
264 // export current sequence value to the specified template variable <$sequence_var>
265 $this->setCurrentSequenceValue();
266
267 // process the loop body
268 $this->processChildren();
269
270 $this->iterateSequence();
271 }
272
276 function initVars()
277 {
278 // initialize delimiter processing
279 $this->NewNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$skipDelimiter = true;" );
280
281 // initialize sequence
282 $this->createSequenceVars();
283 }
284
288 public $Name;
289 public $Parameters;
290 public $NodePlacement;
291 public $UniqID;
292 public $NewNodes;
293 public $Node;
294 public $Tpl;
295 public $PrivateData;
296
297 }
298
299 ?>
eZTemplateCompiledLoop
Common code for compiling the loop functions.
Definition: eztemplatecompiledloop.php:17
eZTemplateCompiledLoop\initVars
initVars()
Definition: eztemplatecompiledloop.php:276
eZTemplateCompiledLoop\$Parameters
$Parameters
Definition: eztemplatecompiledloop.php:289
eZTemplateCompiledLoop\$Tpl
$Tpl
Definition: eztemplatecompiledloop.php:294
eZTemplateCompiledLoop\hasSequence
hasSequence()
Definition: eztemplatecompiledloop.php:45
eZTemplateCompiledLoop\createSequenceVars
createSequenceVars()
Definition: eztemplatecompiledloop.php:77
eZTemplateCompiledLoop\processChildren
processChildren()
Definition: eztemplatecompiledloop.php:137
eZTemplateCompiledLoop\$Node
$Node
Definition: eztemplatecompiledloop.php:293
eZTemplateCompiledLoop\processBody
processBody()
Definition: eztemplatecompiledloop.php:262
eZTemplateCompiledLoop\$Name
$Name
Definition: eztemplatecompiledloop.php:288
eZTemplateCompiledLoop\$NodePlacement
$NodePlacement
Definition: eztemplatecompiledloop.php:290
eZTemplateCompiledLoop\setCurrentSequenceValue
setCurrentSequenceValue()
Definition: eztemplatecompiledloop.php:96
eZTemplateCompiledLoop\cleanup
cleanup()
Definition: eztemplatecompiledloop.php:53
eZTemplateCompiledLoop\destroySequenceVars
destroySequenceVars()
Definition: eztemplatecompiledloop.php:64
eZTemplateCompiledLoop\iterateSequence
iterateSequence()
Definition: eztemplatecompiledloop.php:113
eZTemplateCompiledLoop\$NewNodes
$NewNodes
Definition: eztemplatecompiledloop.php:292
eZTemplateCompiledLoop\$PrivateData
$PrivateData
Definition: eztemplatecompiledloop.php:295
eZTemplateCompiledLoop\__construct
__construct( $name, &$newNodes, $parameters, $nodePlacement, $uniqid, $node, $tpl, $privateData)
Definition: eztemplatecompiledloop.php:30
eZTemplateCompiledLoop\$UniqID
$UniqID
Definition: eztemplatecompiledloop.php:291
eZTemplateCompiler\processElementTransformationList
static processElementTransformationList( $tpl, &$node, $elements, &$privateData)
Definition: eztemplatecompiler.php:1211
eZTemplateCompiler\processNodeTransformationNodes
static processNodeTransformationNodes( $tpl, &$node, &$nodes, &$privateData)
Definition: eztemplatecompiler.php:1012
eZTemplateNodeTool\createVariableNode
static createVariableNode( $originalNode=false, $variableData=false, $variablePlacement=false, $parameters=array(), $variableAssignmentName=false, $onlyExisting=false, $overWrite=true, $assignFromVariable=false, $rememberSet=false)
Definition: eztemplatenodetool.php:395
eZTemplateNodeTool\elementConstantValue
static elementConstantValue( $elements)
Definition: eztemplatenodetool.php:197
eZTemplateNodeTool\extractNodes
static extractNodes( $nodeList, $parameters=array())
Definition: eztemplatenodetool.php:543
eZTemplateNodeTool\createSpacingDecreaseNode
static createSpacingDecreaseNode( $spacing=4, $parameters=array())
Definition: eztemplatenodetool.php:508
eZTemplateNodeTool\createVariableUnsetNode
static createVariableUnsetNode( $variableName, $parameters=array())
Definition: eztemplatenodetool.php:462
eZTemplateNodeTool\createCodePieceNode
static createCodePieceNode( $codePiece, $parameters=array())
Definition: eztemplatenodetool.php:457
eZTemplateNodeTool\createSpacingIncreaseNode
static createSpacingIncreaseNode( $spacing=4, $parameters=array())
Definition: eztemplatenodetool.php:503
eZTemplateNodeTool\extractFunctionNodePlacement
static extractFunctionNodePlacement(&$node)
Definition: eztemplatenodetool.php:660
eZTemplateNodeTool\extractFunctionNodeParameters
static extractFunctionNodeParameters(&$node)
Definition: eztemplatenodetool.php:678
eZTemplateNodeTool\extractFunctionNodeChildren
static extractFunctionNodeChildren(&$node)
Definition: eztemplatenodetool.php:669
eZTemplateNodeTool\isConstantElement
static isConstantElement( $elements)
Definition: eztemplatenodetool.php:233
eZTemplate\NODE_FUNCTION
const NODE_FUNCTION
Definition: eztemplate.php:213
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.