Formats macro arguments to PHP code. (It advances tokenizer to the end as a side effect.)
public formatArgs ( |
||
$tokens | ||
return | string |
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++; } ?>'; } }
/** * {imgLink $img [type]} * @param Latte\MacroNode $node * @param Latte\PhpWriter $writer * @return string */ public function macroImgLink(Latte\MacroNode $node, Latte\PhpWriter $writer) { list($img, $type) = $this->getImageFromNode($node); return ' $args = [' . ($type ? "'type' => '{$type}', " : NULL) . "'storage' => " . '$imageStorage' . ($node->args ? ', ' . $writer->formatArgs() : NULL) . ']; echo ' . get_class($this) . '::imgLink(' . $img . ', $args); '; }
/** * {control name[:method] [params]} */ public function macroControl(MacroNode $node, PhpWriter $writer) { $words = $node->tokenizer->fetchWords(); if (!$words) { throw new CompileException('Missing control name in {control}'); } $name = $writer->formatWord($words[0]); $method = isset($words[1]) ? ucfirst($words[1]) : ''; $method = Strings::match($method, '#^\\w*\\z#') ? "render{$method}" : "{\"render{$method}\"}"; $param = $writer->formatArgs(); if (Strings::contains($node->args, '=>')) { $param = "[{$param}]"; } return ($name[0] === '$' ? "if (is_object({$name})) \$_l->tmp = {$name}; else " : '') . '$_l->tmp = $_control->getComponent(' . $name . '); ' . 'if ($_l->tmp instanceof Nette\\Application\\UI\\IRenderable) $_l->tmp->redrawControl(NULL, FALSE); ' . ($node->modifiers === '' ? "\$_l->tmp->{$method}({$param})" : $writer->write("ob_start(); \$_l->tmp->{$method}({$param}); echo %modify(ob_get_clean())")); }
/** * {dump ...} */ public function macroDump(MacroNode $node, PhpWriter $writer) { if ($node->modifiers) { trigger_error("Modifiers are not allowed in {{$node->name}}", E_USER_WARNING); } $args = $writer->formatArgs(); return $writer->write('Tracy\\Debugger::barDump(' . ($args ? "({$args})" : 'get_defined_vars()') . ', %var)', $args ?: 'variables'); }
/** * {dump ...} */ public function macroDump(MacroNode $node, PhpWriter $writer) { if ($node->modifiers) { throw new CompileException("Modifiers are not allowed in {{$node->name}}"); } $args = $writer->formatArgs(); return $writer->write('Tracy\\Debugger::barDump(' . ($args ? "({$args})" : 'get_defined_vars()') . ', %var)', $args ?: 'variables'); }
/** * {ifset #block} * {elseifset #block} */ public function macroIfset(MacroNode $node, PhpWriter $writer) { if ($node->modifiers) { trigger_error("Modifiers are not allowed in {{$node->name}}", E_USER_WARNING); } if (!preg_match('~#|[\\w-]+\\z~A', $node->args)) { return FALSE; } $list = array(); while (($name = $node->tokenizer->fetchWord()) !== FALSE) { $list[] = preg_match('~#|[\\w-]+\\z~A', $name) ? '$_b->blocks["' . ltrim($name, '#') . '"]' : $writer->formatArgs(new Latte\MacroTokens($name)); } return ($node->name === 'elseifset' ? '} else' : '') . 'if (isset(' . implode(', ', $list) . ')) {'; }
/** * {dump ...} */ public function macroDump(MacroNode $node, PhpWriter $writer) { $args = $writer->formatArgs(); return 'Tracy\\Debugger::barDump(' . ($node->args ? $writer->write("array(%var => {$args})", $args) : 'get_defined_vars()') . ', "Template " . str_replace(dirname(dirname($template->getName())), "\\xE2\\x80\\xA6", $template->getName()))'; }
/** * @from https://github.com/nette/latte/blob/master/src/Latte/Macros/CoreMacros.php#L253-L263 * * @param \Latte\MacroNode $node * @param \Latte\PhpWriter $writer * @param string $iterate */ private function createIterator(MacroNode $node, PhpWriter $writer, $iterate) { if ($node->modifiers !== '|noiterator' && preg_match('#\\W(\\$iterator|include|require|get_defined_vars)\\W#', $this->getCompiler()->expandTokens($node->content))) { $node->openingCode = '<?php $iterations = 0; foreach ($iterator = $_l->its[] = new Latte\\Runtime\\CachingIterator(' . $iterate . ') as ' . $writer->formatArgs() . ') { ?>'; $node->closingCode = '<?php $iterations++; } array_pop($_l->its); $iterator = end($_l->its) ?>'; } else { $node->openingCode = '<?php $iterations = 0; foreach (' . $iterate . ' as ' . $writer->formatArgs() . ') { ?>'; $node->closingCode = '<?php $iterations++; } ?>'; } }
/** * {ifset #block} * {elseifset #block} */ public function macroIfset(MacroNode $node, PhpWriter $writer) { if ($node->modifiers) { throw new CompileException('Modifiers are not allowed here.'); } if (!preg_match('~#|[\\w-]+\\z~A', $node->args)) { return FALSE; } $list = []; while (($name = $node->tokenizer->fetchWord()) !== FALSE) { $list[] = preg_match('~#|[\\w-]+\\z~A', $name) ? '$_b->blocks["' . ltrim($name, '#') . '"]' : $writer->formatArgs(new Latte\MacroTokens($name)); } return ($node->name === 'elseifset' ? '} else' : '') . 'if (isset(' . implode(', ', $list) . ')) {'; }
/** * {dump ...} */ public function macroDump(MacroNode $node, PhpWriter $writer) { $args = $writer->formatArgs(); return $writer->write('Tracy\\Debugger::barDump(' . ($args ? "({$args})" : 'get_defined_vars()') . ', %var)', $args ?: 'variables'); }