public function macroEachEnd(MacroNode $node, PhpWriter $writer)
{
if ($node->modifiers && $node->modifiers !== '|noiterator') {
trigger_error('Only modifier |noiterator is allowed here.', E_USER_WARNING);
}
$args = $writer->formatArgs();
if (substr($args, 0, 3) === 'as ') {
$parts = [self::$defaultEachArrayName, substr($args, 3)];
} elseif (empty($args)) {
$parts = [self::$defaultEachArrayName];
} else {
$parts = Strings::split($args, '#\\s+as\\s+#i');
}
if (count($parts) === 1) {
$parts[] = '$' . self::$defaultEachVarName;
}
$array = $parts[0];
$parts[0] = '(($_l_q instanceof WP_Query)?$_l_q->get_posts():(array)$_l_q)';
$args = implode(' as ', $parts);
if ($node->modifiers !== '|noiterator' && preg_match('#\\W(\\$iterator|include|require|get_defined_vars)\\W#', $this->getCompiler()->expandTokens($node->content))) {
$node->openingCode = '<?php $iterations = 0; $_l_q=' . $array . '; foreach ($iterator = $_l->its[] = new Latte\\Runtime\\CachingIterator(' . preg_replace('#(.*)\\s+as\\s+#i', '$1) as ', $args, 1) . ') { ?>';
$node->closingCode = '<?php $iterations++; } array_pop($_l->its); $iterator = end($_l->its) ?>';
} else {
$node->openingCode = '<?php $iterations = 0; $_l_q=' . $array . '; foreach (' . $args . ') { ?>';
$node->closingCode = '<?php $iterations++; } ?>';
}
}