aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/template
diff options
context:
space:
mode:
authorNathaniel Guse <nathaniel.guse@gmail.com>2013-06-26 12:30:59 -0500
committerNathaniel Guse <nathaniel.guse@gmail.com>2013-06-26 12:30:59 -0500
commit09ed0dd7bccd1f2674525a7a2b7ec99b6c745832 (patch)
tree439780c99c94c2a0415d9e62aef34a5b46c33e9f /phpBB/includes/template
parent6d709525c318bcc6fa4a25aeb6a2a9d1256a9917 (diff)
downloadforums-09ed0dd7bccd1f2674525a7a2b7ec99b6c745832.tar
forums-09ed0dd7bccd1f2674525a7a2b7ec99b6c745832.tar.gz
forums-09ed0dd7bccd1f2674525a7a2b7ec99b6c745832.tar.bz2
forums-09ed0dd7bccd1f2674525a7a2b7ec99b6c745832.tar.xz
forums-09ed0dd7bccd1f2674525a7a2b7ec99b6c745832.zip
[feature/twig] Replace BEGIN with Twig for using Lexer
No longer using the begin tokenparser/node as it did not allow proper handling of <!-- BEGIN !foo, <!-- BEGIN foo(0,2). Now the lexer will use regular expressions to handle that correctly and replace it with Twig's for token Also fixing <!-- IF .foo as I discovered it evaluates to if sizeof(foo) PHPBB3-11598
Diffstat (limited to 'phpBB/includes/template')
-rw-r--r--phpBB/includes/template/twig/extension.php1
-rw-r--r--phpBB/includes/template/twig/lexer.php84
-rw-r--r--phpBB/includes/template/twig/node/begin.php139
-rw-r--r--phpBB/includes/template/twig/tokenparser/begin.php57
-rw-r--r--phpBB/includes/template/twig/twig.php15
5 files changed, 83 insertions, 213 deletions
diff --git a/phpBB/includes/template/twig/extension.php b/phpBB/includes/template/twig/extension.php
index 9d465a4db8..44d694ce1a 100644
--- a/phpBB/includes/template/twig/extension.php
+++ b/phpBB/includes/template/twig/extension.php
@@ -26,7 +26,6 @@ class phpbb_template_twig_extension extends Twig_Extension
{
return array(
new phpbb_template_twig_tokenparser_if,
- new phpbb_template_twig_tokenparser_begin,
new phpbb_template_twig_tokenparser_define,
new phpbb_template_twig_tokenparser_include,
new phpbb_template_twig_tokenparser_includejs,
diff --git a/phpBB/includes/template/twig/lexer.php b/phpBB/includes/template/twig/lexer.php
index b2828c9e25..95b9bd4630 100644
--- a/phpBB/includes/template/twig/lexer.php
+++ b/phpBB/includes/template/twig/lexer.php
@@ -20,9 +20,9 @@ class phpbb_template_twig_lexer extends Twig_Lexer
public function tokenize($code, $filename = null)
{
$valid_starting_tokens = array(
- 'BEGIN',
+ /*'BEGIN',
'BEGINELSE',
- 'END',
+ 'END',*/
'IF',
'ELSE',
'ELSEIF',
@@ -39,13 +39,17 @@ class phpbb_template_twig_lexer extends Twig_Lexer
'EVENT',
);
+ // Fix our BEGIN statements
+ $code = $this->fix_begin_tokens($code);
+
// Replace <!-- INCLUDE blah.html --> with {% include 'blah.html' %}
$code = preg_replace('#<!-- INCLUDE(PHP)? (.*?) -->#', "{% INCLUDE$1 '$2' %}", $code);
// This strips the $ inside of a tag directly after the token, which was used in <!-- DEFINE $NAME
$code = preg_replace('#<!-- DEFINE \$(.*)-->#', '<!-- DEFINE $1-->', $code);
- // This strips the . or $ inside of a tag directly before a variable name, which was used in <!-- IF .blah
+ // This strips the . or $ inside of a tag directly before a variable name, which was used in <!-- IF .blah and <!-- IF $BLAH
+ // In case of .varname, it replaces it with varname|length (as this is how it was treated before)
$code = preg_replace_callback('#<!-- IF((.*)[\s][\$|\.]([^\s]+)(.*))-->#', array($this, 'tag_if_cleanup'), $code);
// Replace all of our starting tokens, <!-- TOKEN --> with Twig style, {% TOKEN %}
@@ -59,6 +63,74 @@ class phpbb_template_twig_lexer extends Twig_Lexer
}
/**
+ * 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
+ *
+ * @param string $code
+ * @param array $parent_nodes
+ * @return string
+ */
+ public function fix_begin_tokens($code, $parent_nodes = array())
+ {
+ // PHP 5.3 cannot use $this in an anonymous function, so use this as a work-around
+ $parent_class = $this;
+ $callback = function ($matches) use ($parent_class, $parent_nodes)
+ {
+ $name = $matches[1];
+ $slice = $matches[2];
+ $body = $matches[3];
+
+ // Is the designer wanting to call another loop in a loop?
+ // <!-- BEGIN loop -->
+ // <!-- BEGIN !loop2 -->
+ // <!-- END !loop2 -->
+ // <!-- END loop -->
+ // 'loop2' is actually on the same nesting level as 'loop' you assign
+ // variables to it with template->assign_block_vars('loop2', array(...))
+ if (strpos($name, '!') === 0)
+ {
+ // Count the number if ! occurrences
+ $count = substr_count($name, '!');
+ for ($i = 0; $i < $count; $i++)
+ {
+ array_pop($parent_nodes);
+ $name = substr($name, 1);
+ }
+ }
+
+ // Remove all parent nodes, e.g. foo, bar from foo.bar.foobar
+ foreach ($parent_nodes as $node)
+ {
+ $body = preg_replace('#([^a-zA-Z0-9])' . $node . '\.#', '$1', $body);
+ }
+
+ // Add current node to list of parent nodes for child nodes
+ $parent_nodes[] = $name;
+
+ // Recursive...fix any child nodes
+ $body = $parent_class->fix_begin_tokens($body, $parent_nodes);
+
+ // Rename loopname vars (to prevent collisions, loop children are named (loop name)_loop_element)
+ $body = str_replace($name . '.', $name . '_loop_element.', $body);
+
+ // Need the parent variable name
+ array_pop($parent_nodes);
+ $parent = (!empty($parent_nodes)) ? end($parent_nodes) . '_loop_element.' : '';
+
+ $slice = ($slice) ? '|slice(' . $slice . ')' : '';
+
+ // Turn into a Twig for loop, using (loop name)_loop_element for each child
+ return "{% for {$name}_loop_element in {$parent}{$name}{$slice} %}{$body}{% endfor %}";
+ };
+
+ // Replace <!-- BEGINELSE --> correctly, only needs to be done once
+ $code = str_replace('<!-- BEGINELSE -->', '{% else %}', $code);
+
+ return preg_replace_callback('#<!-- BEGIN ([!a-zA-Z0-9_]+)\(?([0-9,]+)?\)? -->(.+?)<!-- END \1 -->#s', $callback, $code);
+ }
+
+ /**
* preg_replace_callback to clean up IF statements
*
* This strips the . or $ inside of a tag directly before a variable name.
@@ -68,6 +140,10 @@ class phpbb_template_twig_lexer extends Twig_Lexer
*/
protected function tag_if_cleanup($matches)
{
- return '<!-- IF ' . preg_replace('#\s[\.|\$]([a-zA-Z_0-9]+)#', ' $1', $matches[1]) . ' -->';
+ // Replace $TEST with TEST
+ $matches[1] = preg_replace('#\s\$([a-zA-Z_0-9]+)#', ' $1', $matches[1]);
+ $matches[1] = preg_replace('#\s\.([a-zA-Z_0-9]+)#', ' $1|length', $matches[1]);
+
+ return '<!-- IF ' . $matches[1] . ' -->';
}
}
diff --git a/phpBB/includes/template/twig/node/begin.php b/phpBB/includes/template/twig/node/begin.php
deleted file mode 100644
index 3c1ce1b89b..0000000000
--- a/phpBB/includes/template/twig/node/begin.php
+++ /dev/null
@@ -1,139 +0,0 @@
-<?php
-/**
-*
-* @package phpBB3
-* @copyright (c) 2013 phpBB Group
-* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
-*
-*/
-
-class phpbb_template_twig_node_begin extends Twig_Node
-{
- public function __construct($beginName, Twig_NodeInterface $body, Twig_NodeInterface $else = null, $lineno, $tag = null)
- {
- parent::__construct(array('body' => $body, 'else' => $else), array('beginName' => $beginName), $lineno, $tag);
- }
-
- /**
- * Compiles the node to PHP.
- *
- * @param Twig_Compiler A Twig_Compiler instance
- */
- public function compile(Twig_Compiler $compiler)
- {
- $compiler
- ->write("if (!isset(\$phpbb_blocks)) {\n")
- ->indent()
- ->write("\$phpbb_blocks = array();\n")
- ->write("\$parent = \$context['_phpbb_blocks'];\n")
- ->outdent()
- ->write("}\n")
- ->write("\$phpbb_blocks[] = '" . $this->getAttribute('beginName') . "';\n")
- ;
-
- $compiler
- ->write("if (!empty(\$parent['" . $this->getAttribute('beginName') . "'])) {\n")
- ->indent()
- ->write("foreach (\$parent['" . $this->getAttribute('beginName') . "'] as \$" . $this->getAttribute('beginName') . ") {\n")
- ->indent()
- // Set up $context correctly so that Twig can get the correct data with $this->getAttribute
- ->write("\$this->getEnvironment()->context_recursive_loop_builder(\$" . $this->getAttribute('beginName') . ", \$phpbb_blocks, \$context);\n")
-
- // We store the parent so that we can do this recursively
- ->write("\$parent = \$" . $this->getAttribute('beginName') . ";\n")
- ;
-
- $compiler->subcompile($this->getNode('body'));
-
- $compiler
- ->outdent()
- ->write("}\n")
- ;
-
- if (null !== $this->getNode('else')) {
- $compiler
- ->write("} else {\n")
- ->indent()
- ->subcompile($this->getNode('else'))
- ->outdent()
- ;
- }
-
- $compiler
- ->outdent()
- ->write("}\n")
-
- // Remove the last item from the blocks storage as we've completed iterating over them all
- ->write("array_pop(\$phpbb_blocks);\n")
-
- // If we've gone through all of the blocks, we're back at the main level and have completed, so unset the var
- ->write("if (empty(\$phpbb_blocks)) { unset(\$phpbb_blocks); }\n")
- ;
- }
-
- /**
- * Compiles the node to PHP.
- *
- * Uses anonymous functions to compile the loops, which seems nicer to me, but requires PHP 5.4 (since subcompile uses $this, which is not available in 5.3)
- *
- * @param Twig_Compiler A Twig_Compiler instance
- *
- public function compile(Twig_Compiler $compiler)
- {
- $compiler->addDebugInfo($this);
-
- $compiler
- // name -> loop name
- // local context -> parent template variable context
- // global context -> global template variable context
- // variable chain -> full chain of variables to output template vars properly in subloops
- // e.g. [foo][bar][foobar]
- // current chain location -> current location in subloop
- // e.g. [foobar] of [foo][bar]
- ->write("\$iterator = function (\$name, \$local_context, \$global_context, &\$variable_chain, &\$current_chain_location) {\n")
- ->indent()
- //->write("var_dump(\$name, \$local_context);\n")
- // Try to find the loop in the
- // local context (child of local context passed, in case of a child loop)
- // global context (root template var)
- ->write("if (isset(\$local_context[\$name])) {\n")
- ->indent()
- ->write("\$local_context = \$local_context[\$name];\n")
- ->outdent()
- ->write("}\n")
- ->write("else if (isset(\$global_context[\$name])) {\n")
- ->indent()
- ->write("\$local_context = \$global_context[\$name];\n")
- ->outdent()
- ->write("} else { return; }\n")
-
- ->write("if (!is_array(\$local_context) || empty(\$local_context)) { return; }\n")
-
- ->write("foreach (\$local_context as \$for_context) {\n")
- ->indent()
- // Some hackish stuff for Twig to properly subcompile
- ->write("\$current_chain_location[\$name] = \$for_context;\n")
- ->write("\$context = array_merge(\$global_context, \$variable_chain);\n")
-
- // Children
- ->subcompile($this->getNode('body'))
- ->outdent()
- ->write("}\n")
- ->outdent()
- ->write("};\n")
- ->write("if (isset(\$global_context)) {\n")
- ->indent()
- // We are already inside an anonymous function
- ->write("\$iterator('" . $this->getAttribute('beginName') . "', \$for_context, \$global_context, \$variable_chain, \$current_chain_location[\$name]);\n")
- ->outdent()
- ->write("} else {\n")
- ->indent()
- // We are not inside the anonymous function (first level)
- ->write("\$variable_chain = array();\n")
- ->write("\$current_chain_location = array();\n")
- ->write("\$iterator('" . $this->getAttribute('beginName') . "', array(), \$context, \$variable_chain, \$variable_chain);\n")
- ->outdent()
- ->write("}\n");
- }
- */
-}
diff --git a/phpBB/includes/template/twig/tokenparser/begin.php b/phpBB/includes/template/twig/tokenparser/begin.php
deleted file mode 100644
index 0a2f2da40d..0000000000
--- a/phpBB/includes/template/twig/tokenparser/begin.php
+++ /dev/null
@@ -1,57 +0,0 @@
-<?php
-/**
-*
-* @package phpBB3
-* @copyright (c) 2013 phpBB Group, sections (c) 2009 Fabien Potencier
-* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
-*
-*/
-
-class phpbb_template_twig_tokenparser_begin extends Twig_TokenParser_For
-{
- /**
- * Parses a token and returns a node.
- *
- * @param Twig_Token $token A Twig_Token instance
- *
- * @return Twig_NodeInterface A Twig_NodeInterface instance
- */
- public function parse(Twig_Token $token)
- {
- $lineno = $token->getLine();
- $beginName = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue();
-
- $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
- $body = $this->parser->subparse(array($this, 'decideBeginFork'));
- if ($this->parser->getStream()->next()->getValue() == 'BEGINELSE') {
- $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
- $else = $this->parser->subparse(array($this, 'decideBeginEnd'), true);
- } else {
- $else = null;
- }
- $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, $beginName);
- $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
-
- return new phpbb_template_twig_node_begin($beginName, $body, $else, $lineno, $this->getTag());
- }
-
- public function decideBeginFork(Twig_Token $token)
- {
- return $token->test(array('BEGINELSE', 'END'));
- }
-
- public function decideBeginEnd(Twig_Token $token)
- {
- return $token->test('END');
- }
-
- /**
- * Gets the tag name associated with this token parser.
- *
- * @return string The tag name
- */
- public function getTag()
- {
- return 'BEGIN';
- }
-}
diff --git a/phpBB/includes/template/twig/twig.php b/phpBB/includes/template/twig/twig.php
index e80f42fede..cb2899a2fd 100644
--- a/phpBB/includes/template/twig/twig.php
+++ b/phpBB/includes/template/twig/twig.php
@@ -432,7 +432,7 @@ class phpbb_template_twig implements phpbb_template
*
* @return array
*/
- protected function get_template_vars()
+ public function get_template_vars()
{
$vars = array();
@@ -454,20 +454,11 @@ class phpbb_template_twig implements phpbb_template
$vars = array_merge(
$vars,
$this->context->get_rootref(),
- array(
- '_phpbb_blocks' => $this->context->get_tpldata(),
- )
+ $this->context->get_tpldata()
);
- // Must do this so that <!-- IF .blah --> works correctly
- // (only for the base loops, the rest are properly handled by the begin node)
- foreach ($this->context->get_tpldata() as $block_name => $block_values)
- {
- $vars[$block_name] = !empty($block_values);
- }
-
// cleanup
- unset($vars['_phpbb_blocks']['.']);
+ unset($vars['.']);
return $vars;
}