aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Guse <nathaniel.guse@gmail.com>2013-06-29 19:19:18 -0500
committerNathaniel Guse <nathaniel.guse@gmail.com>2013-06-29 19:19:18 -0500
commitf18cbd50f0c54aa1a97d1b4ece69d5bf70b68734 (patch)
tree61009bc8b02e1b60b35f77466c8bef773eab9ba2
parent64963b5962d9a4bf1888dd1642a32ba3f6037258 (diff)
downloadforums-f18cbd50f0c54aa1a97d1b4ece69d5bf70b68734.tar
forums-f18cbd50f0c54aa1a97d1b4ece69d5bf70b68734.tar.gz
forums-f18cbd50f0c54aa1a97d1b4ece69d5bf70b68734.tar.bz2
forums-f18cbd50f0c54aa1a97d1b4ece69d5bf70b68734.tar.xz
forums-f18cbd50f0c54aa1a97d1b4ece69d5bf70b68734.zip
[feature/twig] Fixing more stuff for DEFINE/INCLUDE
PHPBB3-11598
-rw-r--r--phpBB/includes/template/twig/lexer.php49
-rw-r--r--phpBB/includes/template/twig/node/define.php4
2 files changed, 44 insertions, 9 deletions
diff --git a/phpBB/includes/template/twig/lexer.php b/phpBB/includes/template/twig/lexer.php
index e8ceef3d13..258b913a2c 100644
--- a/phpBB/includes/template/twig/lexer.php
+++ b/phpBB/includes/template/twig/lexer.php
@@ -31,14 +31,21 @@ class phpbb_template_twig_lexer extends Twig_Lexer
/*'DEFINE',
'UNDEFINE',*/
'ENDDEFINE',
- /*'INCLUDE',
- 'INCLUDEPHP',*/
+ 'INCLUDE',
+ 'INCLUDEPHP',
'INCLUDEJS',
'PHP',
'ENDPHP',
'EVENT',
);
+ // Fix tokens that may have inline variables (e.g. <!-- DEFINE $TEST = '{FOO}')
+ $code = $this->fix_inline_variable_tokens(array(
+ 'DEFINE.+=',
+ 'INCLUDE',
+ 'INCLUDEPHP',
+ ), $code);
+
// Fix our BEGIN statements
$code = $this->fix_begin_tokens($code);
@@ -48,9 +55,6 @@ class phpbb_template_twig_lexer extends Twig_Lexer
// Fix our DEFINE tokens
$code = $this->fix_define_tokens($code);
- // Replace <!-- INCLUDE blah.html --> with {% include 'blah.html' %}
- $code = preg_replace('#<!-- INCLUDE(PHP)? (.*?) -->#', "{% INCLUDE$1 '$2' %}", $code);
-
// Replace all of our starting tokens, <!-- TOKEN --> with Twig style, {% TOKEN %}
// This also strips outer parenthesis, <!-- IF (blah) --> becomes <!-- IF blah -->
$code = preg_replace('#<!-- (' . implode('|', $valid_starting_tokens) . ')(?: (.*?) ?)?-->#', '{% $1 $2 %}', $code);
@@ -62,6 +66,31 @@ class phpbb_template_twig_lexer extends Twig_Lexer
}
/**
+ * Fix tokens that may have inline variables
+ *
+ * E.g. <!-- INCLUDE {TEST}.html
+ *
+ * @param array $tokens array of tokens to search for (imploded to a regular expression)
+ * @param string $code
+ * @return string
+ */
+ protected function fix_inline_variable_tokens($tokens, $code)
+ {
+ $callback = function($matches)
+ {
+ // Remove any quotes that may have been used in different implementations
+ // E.g. DEFINE $TEST = 'blah' vs INCLUDE foo
+ // Replace {} with start/end to parse variables (' ~ TEST ~ '.html)
+ $matches[2] = str_replace(array('"', "'", '{', '}'), array('', '', "' ~ ", " ~ '"), $matches[2]);
+
+ // Surround the matches in single quotes ('' ~ TEST ~ '.html')
+ return "<!-- {$matches[1]} '{$matches[2]}' -->";
+ };
+
+ return preg_replace_callback('#<!-- (' . implode('|', $tokens) . ') (.+?) -->#', $callback, $code);
+ }
+
+ /**
* Fix begin tokens (convert our BEGIN to Twig for)
*
* Not meant to be used outside of this context, public because the anonymous function calls this
@@ -148,10 +177,13 @@ class phpbb_template_twig_lexer extends Twig_Lexer
// Replace .test with test|length
$matches[1] = preg_replace('#\s\.([a-zA-Z_0-9]+)#', ' $1|length', $matches[1]);
- return '<!-- IF ' . $matches[1] . ' -->';
+ // Replace our "div by" with Twig's divisibleby (Twig does not like test names with spaces?)
+ $matches[1] = preg_replace('# div by ([0-9]+)#', ' divisibleby($1)', $matches[1]);
+
+ return '<!-- IF' . $matches[1] . '-->';
};
- return preg_replace_callback('#<!-- IF((.*)[\s][\$|\.]([^\s]+)(.*))-->#', $callback, $code);
+ return preg_replace_callback('#<!-- IF((.*)[\s][\$|\.|!]([^\s]+)(.*))-->#', $callback, $code);
}
/**
@@ -183,6 +215,9 @@ class phpbb_template_twig_lexer extends Twig_Lexer
// Replace all of our variables, {$VARNAME}, with Twig style, {{ definition.VARNAME }}
$code = preg_replace('#{\$([a-zA-Z0-9_\.]+)}#', '{{ definition.$1 }}', $code);
+ // Replace all of our variables, ~ $VARNAME ~, with Twig style, ~ definition.VARNAME ~
+ $code = preg_replace('#~ \$([a-zA-Z0-9_\.]+) ~#', '~ definition.$1 ~', $code);
+
return $code;
}
}
diff --git a/phpBB/includes/template/twig/node/define.php b/phpBB/includes/template/twig/node/define.php
index ef7565d27d..87e93be7be 100644
--- a/phpBB/includes/template/twig/node/define.php
+++ b/phpBB/includes/template/twig/node/define.php
@@ -29,13 +29,13 @@ class phpbb_template_twig_node_define extends Twig_Node
->subcompile($this->getNode('value'))
;
- $compiler->raw(" = ('' === \$value = ob_get_clean()) ? '' : new Twig_Markup(\$value, \$this->env->getCharset())");
+ $compiler->write("\$value = ('' === \$value = ob_get_clean()) ? '' : new Twig_Markup(\$value, \$this->env->getCharset());\n");
}
else
{
$compiler
->write("\$value = '")
- ->raw($this->getNode('value')->getAttribute('value'))
+ ->subcompile($this->getNode('value'))
->raw("';\n")
;
}