/** * @param Tokenizer $tokens * @param $name * @return string * @throws InvalidUsageException */ public function parseMacroCall(Tokenizer $tokens, $name) { $recursive = false; $macro = false; if (isset($this->macros[$name])) { $macro = $this->macros[$name]; $recursive = $macro['recursive']; } else { foreach ($this->_stack as $scope) { if ($scope->name == 'macro' && $scope['name'] == $name) { // invoke recursive $recursive = $scope; $macro = $scope['macro']; break; } } if (!$macro) { throw new InvalidUsageException("Undefined macro '{$name}'"); } } $tokens->next(); $p = $this->parseParams($tokens); $args = array(); foreach ($macro['args'] as $arg) { if (isset($p[$arg])) { $args[$arg] = $p[$arg]; } elseif (isset($macro['defaults'][$arg])) { $args[$arg] = $macro['defaults'][$arg]; } else { throw new InvalidUsageException("Macro '{$name}' require '{$arg}' argument"); } } if ($recursive) { if ($recursive instanceof Tag) { $recursive['recursive'] = true; } return '$tpl->getMacro("' . $name . '")->__invoke(' . Compiler::toArray($args) . ', $tpl);'; } else { $vars = $this->tmpVar(); return $vars . ' = $var; $var = ' . Compiler::toArray($args) . ';' . PHP_EOL . '?>' . $macro["body"] . '<?php' . PHP_EOL . '$var = ' . $vars . '; unset(' . $vars . ');'; } }
/** * @param Tokenizer $tokens * @param $name * @return string * @throws InvalidUsageException */ public function parseMacroCall(Tokenizer $tokens, $name) { $recursive = false; $macro = false; if (isset($this->macros[$name])) { $macro = $this->macros[$name]; } else { foreach ($this->_stack as $scope) { if ($scope->name == 'macro' && $scope['name'] == $name) { // invoke recursive $recursive = $scope; $macro = $scope['macro']; break; } } if (!$macro) { throw new InvalidUsageException("Undefined macro '{$name}'"); } } $tokens->next(); $p = $this->parseParams($tokens); $args = array(); foreach ($macro['args'] as $arg) { if (isset($p[$arg])) { $args[$arg] = $p[$arg]; } elseif (isset($macro['defaults'][$arg])) { $args[$arg] = $macro['defaults'][$arg]; } else { throw new InvalidUsageException("Macro '{$name}' require '{$arg}' argument"); } } $n = sprintf('%u_%d', crc32($this->_name), $this->i++); if ($recursive) { $recursive['recursive'] = true; $body = '$tpl->getMacro("' . $name . '")->__invoke($tpl);'; } else { $body = '?>' . $macro["body"] . '<?php'; } return '$_tpl' . $n . ' = $tpl->exchangeArray(' . Compiler::toArray($args) . ');' . PHP_EOL . $body . PHP_EOL . '$tpl->exchangeArray($_tpl' . $n . '); /* X */ unset($_tpl' . $n . ');'; }