|
@@ -16,6 +16,9 @@
|
|
|
*/
|
|
*/
|
|
|
class Mustache_Compiler
|
|
class Mustache_Compiler
|
|
|
{
|
|
{
|
|
|
|
|
+
|
|
|
|
|
+ private $pragmas;
|
|
|
|
|
+ private $defaultPragmas = array();
|
|
|
private $sections;
|
|
private $sections;
|
|
|
private $source;
|
|
private $source;
|
|
|
private $indentNextLine;
|
|
private $indentNextLine;
|
|
@@ -23,7 +26,6 @@ class Mustache_Compiler
|
|
|
private $entityFlags;
|
|
private $entityFlags;
|
|
|
private $charset;
|
|
private $charset;
|
|
|
private $strictCallables;
|
|
private $strictCallables;
|
|
|
- private $pragmas;
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Compile a Mustache token parse tree into PHP source code.
|
|
* Compile a Mustache token parse tree into PHP source code.
|
|
@@ -40,7 +42,7 @@ class Mustache_Compiler
|
|
|
*/
|
|
*/
|
|
|
public function compile($source, array $tree, $name, $customEscape = false, $charset = 'UTF-8', $strictCallables = false, $entityFlags = ENT_COMPAT)
|
|
public function compile($source, array $tree, $name, $customEscape = false, $charset = 'UTF-8', $strictCallables = false, $entityFlags = ENT_COMPAT)
|
|
|
{
|
|
{
|
|
|
- $this->pragmas = array();
|
|
|
|
|
|
|
+ $this->pragmas = $this->defaultPragmas;
|
|
|
$this->sections = array();
|
|
$this->sections = array();
|
|
|
$this->source = $source;
|
|
$this->source = $source;
|
|
|
$this->indentNextLine = true;
|
|
$this->indentNextLine = true;
|
|
@@ -52,6 +54,23 @@ class Mustache_Compiler
|
|
|
return $this->writeCode($tree, $name);
|
|
return $this->writeCode($tree, $name);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Enable pragmas across all templates, regardless of the presence of pragma
|
|
|
|
|
+ * tags in the individual templates.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @internal Users should set global pragmas in Mustache_Engine, not here :)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param array $pragmas
|
|
|
|
|
+ */
|
|
|
|
|
+ public function setPragmas(array $pragmas)
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->pragmas = array();
|
|
|
|
|
+ foreach ($pragmas as $pragma) {
|
|
|
|
|
+ $this->pragmas[$pragma] = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ $this->defaultPragmas = $this->pragmas;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Helper function for walking the Mustache token parse tree.
|
|
* Helper function for walking the Mustache token parse tree.
|
|
|
*
|
|
*
|
|
@@ -93,7 +112,6 @@ class Mustache_Compiler
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case Mustache_Tokenizer::T_PARTIAL:
|
|
case Mustache_Tokenizer::T_PARTIAL:
|
|
|
- case Mustache_Tokenizer::T_PARTIAL_2:
|
|
|
|
|
$code .= $this->partial(
|
|
$code .= $this->partial(
|
|
|
$node[Mustache_Tokenizer::NAME],
|
|
$node[Mustache_Tokenizer::NAME],
|
|
|
isset($node[Mustache_Tokenizer::INDENT]) ? $node[Mustache_Tokenizer::INDENT] : '',
|
|
isset($node[Mustache_Tokenizer::INDENT]) ? $node[Mustache_Tokenizer::INDENT] : '',
|
|
@@ -101,6 +119,39 @@ class Mustache_Compiler
|
|
|
);
|
|
);
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
|
|
+ case Mustache_Tokenizer::T_PARENT:
|
|
|
|
|
+ $code .= $this->parent(
|
|
|
|
|
+ $node[Mustache_Tokenizer::NAME],
|
|
|
|
|
+ isset($node[Mustache_Tokenizer::INDENT]) ? $node[Mustache_Tokenizer::INDENT] : '',
|
|
|
|
|
+ $node[Mustache_Tokenizer::NODES],
|
|
|
|
|
+ $level
|
|
|
|
|
+ );
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case Mustache_Tokenizer::T_BLOCK_ARG:
|
|
|
|
|
+ $code .= $this->blockArg(
|
|
|
|
|
+ $node[Mustache_Tokenizer::NODES],
|
|
|
|
|
+ $node[Mustache_Tokenizer::NAME],
|
|
|
|
|
+ $node[Mustache_Tokenizer::INDEX],
|
|
|
|
|
+ $node[Mustache_Tokenizer::END],
|
|
|
|
|
+ $node[Mustache_Tokenizer::OTAG],
|
|
|
|
|
+ $node[Mustache_Tokenizer::CTAG],
|
|
|
|
|
+ $level
|
|
|
|
|
+ );
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case Mustache_Tokenizer::T_BLOCK_VAR:
|
|
|
|
|
+ $code .= $this->blockVar(
|
|
|
|
|
+ $node[Mustache_Tokenizer::NODES],
|
|
|
|
|
+ $node[Mustache_Tokenizer::NAME],
|
|
|
|
|
+ $node[Mustache_Tokenizer::INDEX],
|
|
|
|
|
+ $node[Mustache_Tokenizer::END],
|
|
|
|
|
+ $node[Mustache_Tokenizer::OTAG],
|
|
|
|
|
+ $node[Mustache_Tokenizer::CTAG],
|
|
|
|
|
+ $level
|
|
|
|
|
+ );
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
case Mustache_Tokenizer::T_UNESCAPED:
|
|
case Mustache_Tokenizer::T_UNESCAPED:
|
|
|
case Mustache_Tokenizer::T_UNESCAPED_2:
|
|
case Mustache_Tokenizer::T_UNESCAPED_2:
|
|
|
$code .= $this->variable($node[Mustache_Tokenizer::NAME], false, $level);
|
|
$code .= $this->variable($node[Mustache_Tokenizer::NAME], false, $level);
|
|
@@ -135,6 +186,7 @@ class Mustache_Compiler
|
|
|
{
|
|
{
|
|
|
$this->lambdaHelper = new Mustache_LambdaHelper($this->mustache, $context);
|
|
$this->lambdaHelper = new Mustache_LambdaHelper($this->mustache, $context);
|
|
|
$buffer = \'\';
|
|
$buffer = \'\';
|
|
|
|
|
+ $newContext = array();
|
|
|
%s
|
|
%s
|
|
|
|
|
|
|
|
return $buffer;
|
|
return $buffer;
|
|
@@ -149,6 +201,7 @@ class Mustache_Compiler
|
|
|
public function renderInternal(Mustache_Context $context, $indent = \'\')
|
|
public function renderInternal(Mustache_Context $context, $indent = \'\')
|
|
|
{
|
|
{
|
|
|
$buffer = \'\';
|
|
$buffer = \'\';
|
|
|
|
|
+ $newContext = array();
|
|
|
%s
|
|
%s
|
|
|
|
|
|
|
|
return $buffer;
|
|
return $buffer;
|
|
@@ -170,11 +223,75 @@ class Mustache_Compiler
|
|
|
$code = $this->walk($tree);
|
|
$code = $this->walk($tree);
|
|
|
$sections = implode("\n", $this->sections);
|
|
$sections = implode("\n", $this->sections);
|
|
|
$klass = empty($this->sections) ? self::KLASS_NO_LAMBDAS : self::KLASS;
|
|
$klass = empty($this->sections) ? self::KLASS_NO_LAMBDAS : self::KLASS;
|
|
|
|
|
+
|
|
|
$callable = $this->strictCallables ? $this->prepare(self::STRICT_CALLABLE) : '';
|
|
$callable = $this->strictCallables ? $this->prepare(self::STRICT_CALLABLE) : '';
|
|
|
|
|
|
|
|
return sprintf($this->prepare($klass, 0, false, true), $name, $callable, $code, $sections);
|
|
return sprintf($this->prepare($klass, 0, false, true), $name, $callable, $code, $sections);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ const BLOCK_VAR = '
|
|
|
|
|
+ $value = $this->resolveValue($context->findInBlock(%s), $context, $indent);
|
|
|
|
|
+ if ($value && !is_array($value) && !is_object($value)) {
|
|
|
|
|
+ $buffer .= $value;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ %s
|
|
|
|
|
+ }
|
|
|
|
|
+ ';
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Generate Mustache Template inheritance block variable PHP source.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param array $nodes Array of child tokens
|
|
|
|
|
+ * @param string $id Section name
|
|
|
|
|
+ * @param int $start Section start offset
|
|
|
|
|
+ * @param int $end Section end offset
|
|
|
|
|
+ * @param string $otag Current Mustache opening tag
|
|
|
|
|
+ * @param string $ctag Current Mustache closing tag
|
|
|
|
|
+ * @param int $level
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return string Generated PHP source code
|
|
|
|
|
+ */
|
|
|
|
|
+ private function blockVar($nodes, $id, $start, $end, $otag, $ctag, $level)
|
|
|
|
|
+ {
|
|
|
|
|
+ $id_str = var_export($id, true);
|
|
|
|
|
+
|
|
|
|
|
+ return sprintf($this->prepare(self::BLOCK_VAR, $level), $id_str, $this->walk($nodes, 2));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const BLOCK_ARG = '
|
|
|
|
|
+ // %s block_arg
|
|
|
|
|
+ $value = $this->section%s($context, $indent, true);
|
|
|
|
|
+ $newContext[%s] = %s$value;
|
|
|
|
|
+ ';
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Generate Mustache Template inheritance block argument PHP source.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param array $nodes Array of child tokens
|
|
|
|
|
+ * @param string $id Section name
|
|
|
|
|
+ * @param int $start Section start offset
|
|
|
|
|
+ * @param int $end Section end offset
|
|
|
|
|
+ * @param string $otag Current Mustache opening tag
|
|
|
|
|
+ * @param string $ctag Current Mustache closing tag
|
|
|
|
|
+ * @param int $level
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return string Generated PHP source code
|
|
|
|
|
+ */
|
|
|
|
|
+ private function blockArg($nodes, $id, $start, $end, $otag, $ctag, $level)
|
|
|
|
|
+ {
|
|
|
|
|
+ $key = $this->section($nodes, $id, $start, $end, $otag, $ctag, $level, true);
|
|
|
|
|
+ $filters = '';
|
|
|
|
|
+
|
|
|
|
|
+ if (isset($this->pragmas[Mustache_Engine::PRAGMA_FILTERS])) {
|
|
|
|
|
+ list($id, $filters) = $this->getFilters($id, $level);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $method = $this->getFindMethod($id);
|
|
|
|
|
+ $id = var_export($id, true);
|
|
|
|
|
+
|
|
|
|
|
+ return sprintf($this->prepare(self::BLOCK_ARG, $level), $id, $key, $id, $this->flushIndent());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
const SECTION_CALL = '
|
|
const SECTION_CALL = '
|
|
|
// %s section
|
|
// %s section
|
|
|
$value = $context->%s(%s);%s
|
|
$value = $context->%s(%s);%s
|
|
@@ -198,7 +315,8 @@ class Mustache_Compiler
|
|
|
} elseif (!empty($value)) {
|
|
} elseif (!empty($value)) {
|
|
|
$values = $this->isIterable($value) ? $value : array($value);
|
|
$values = $this->isIterable($value) ? $value : array($value);
|
|
|
foreach ($values as $value) {
|
|
foreach ($values as $value) {
|
|
|
- $context->push($value);%s
|
|
|
|
|
|
|
+ $context->push($value);
|
|
|
|
|
+ %s
|
|
|
$context->pop();
|
|
$context->pop();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -216,10 +334,11 @@ class Mustache_Compiler
|
|
|
* @param string $otag Current Mustache opening tag
|
|
* @param string $otag Current Mustache opening tag
|
|
|
* @param string $ctag Current Mustache closing tag
|
|
* @param string $ctag Current Mustache closing tag
|
|
|
* @param int $level
|
|
* @param int $level
|
|
|
|
|
+ * @param bool $arg (default: false)
|
|
|
*
|
|
*
|
|
|
* @return string Generated section PHP source code
|
|
* @return string Generated section PHP source code
|
|
|
*/
|
|
*/
|
|
|
- private function section($nodes, $id, $start, $end, $otag, $ctag, $level)
|
|
|
|
|
|
|
+ private function section($nodes, $id, $start, $end, $otag, $ctag, $level, $arg = false)
|
|
|
{
|
|
{
|
|
|
$filters = '';
|
|
$filters = '';
|
|
|
|
|
|
|
@@ -227,8 +346,6 @@ class Mustache_Compiler
|
|
|
list($id, $filters) = $this->getFilters($id, $level);
|
|
list($id, $filters) = $this->getFilters($id, $level);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $method = $this->getFindMethod($id);
|
|
|
|
|
- $id = var_export($id, true);
|
|
|
|
|
$source = var_export(substr($this->source, $start, $end - $start), true);
|
|
$source = var_export(substr($this->source, $start, $end - $start), true);
|
|
|
$callable = $this->getCallable();
|
|
$callable = $this->getCallable();
|
|
|
|
|
|
|
@@ -238,13 +355,20 @@ class Mustache_Compiler
|
|
|
$delims = '';
|
|
$delims = '';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $key = ucfirst(md5($delims."\n".$source));
|
|
|
|
|
|
|
+ $key = ucfirst(md5($delims."\n".$source));
|
|
|
|
|
|
|
|
if (!isset($this->sections[$key])) {
|
|
if (!isset($this->sections[$key])) {
|
|
|
$this->sections[$key] = sprintf($this->prepare(self::SECTION), $key, $callable, $source, $delims, $this->walk($nodes, 2));
|
|
$this->sections[$key] = sprintf($this->prepare(self::SECTION), $key, $callable, $source, $delims, $this->walk($nodes, 2));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return sprintf($this->prepare(self::SECTION_CALL, $level), $id, $method, $id, $filters, $key);
|
|
|
|
|
|
|
+ if ($arg === true) {
|
|
|
|
|
+ return $key;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $method = $this->getFindMethod($id);
|
|
|
|
|
+ $id = var_export($id, true);
|
|
|
|
|
+
|
|
|
|
|
+ return sprintf($this->prepare(self::SECTION_CALL, $level), $id, $method, $id, $filters, $key);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const INVERTED_SECTION = '
|
|
const INVERTED_SECTION = '
|
|
@@ -301,6 +425,50 @@ class Mustache_Compiler
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ const PARENT = '
|
|
|
|
|
+ %s
|
|
|
|
|
+
|
|
|
|
|
+ if ($parent = $this->mustache->LoadPartial(%s)) {
|
|
|
|
|
+ $context->pushBlockContext($newContext);
|
|
|
|
|
+ $buffer .= $parent->renderInternal($context, $indent);
|
|
|
|
|
+ $context->popBlockContext();
|
|
|
|
|
+ }
|
|
|
|
|
+ ';
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Generate Mustache Template inheritance parent call PHP source.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $id Parent tag name
|
|
|
|
|
+ * @param string $indent Whitespace indent to apply to parent
|
|
|
|
|
+ * @param array $children Child nodes
|
|
|
|
|
+ * @param int $level
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return string Generated PHP source code
|
|
|
|
|
+ */
|
|
|
|
|
+ private function parent($id, $indent, array $children, $level)
|
|
|
|
|
+ {
|
|
|
|
|
+ $realChildren = array_filter($children, array(__CLASS__, 'onlyBlockArgs'));
|
|
|
|
|
+
|
|
|
|
|
+ return sprintf(
|
|
|
|
|
+ $this->prepare(self::PARENT, $level),
|
|
|
|
|
+ $this->walk($realChildren, $level),
|
|
|
|
|
+ var_export($id, true),
|
|
|
|
|
+ var_export($indent, true)
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Helper method for filtering out non-block-arg tokens.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param array $node
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return boolean True if $node is a block arg token.
|
|
|
|
|
+ */
|
|
|
|
|
+ private static function onlyBlockArgs(array $node)
|
|
|
|
|
+ {
|
|
|
|
|
+ return $node[Mustache_Tokenizer::TYPE] === Mustache_Tokenizer::T_BLOCK_ARG;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
const VARIABLE = '
|
|
const VARIABLE = '
|
|
|
$value = $this->resolveValue($context->%s(%s), $context, $indent);%s
|
|
$value = $this->resolveValue($context->%s(%s), $context, $indent);%s
|
|
|
$buffer .= %s%s;
|
|
$buffer .= %s%s;
|
|
@@ -466,6 +634,13 @@ class Mustache_Compiler
|
|
|
const IS_CALLABLE = '!is_string(%s) && is_callable(%s)';
|
|
const IS_CALLABLE = '!is_string(%s) && is_callable(%s)';
|
|
|
const STRICT_IS_CALLABLE = 'is_object(%s) && is_callable(%s)';
|
|
const STRICT_IS_CALLABLE = 'is_object(%s) && is_callable(%s)';
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Helper function to compile strict vs lax "is callable" logic.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $variable (default: '$value')
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return string "is callable" logic
|
|
|
|
|
+ */
|
|
|
private function getCallable($variable = '$value')
|
|
private function getCallable($variable = '$value')
|
|
|
{
|
|
{
|
|
|
$tpl = $this->strictCallables ? self::STRICT_IS_CALLABLE : self::IS_CALLABLE;
|
|
$tpl = $this->strictCallables ? self::STRICT_IS_CALLABLE : self::IS_CALLABLE;
|