From 09ed0dd7bccd1f2674525a7a2b7ec99b6c745832 Mon Sep 17 00:00:00 2001 From: Nathaniel Guse Date: Wed, 26 Jun 2013 12:30:59 -0500 Subject: [PATCH] [feature/twig] Replace BEGIN with Twig for using Lexer No longer using the begin tokenparser/node as it did not allow proper handling of with {% include 'blah.html' %} $code = preg_replace('##', "{% INCLUDE$1 '$2' %}", $code); // This strips the $ inside of a tag directly after the token, which was used in #', '', $code); - // This strips the . or $ inside of a tag directly before a variable name, which was used in #', array($this, 'tag_if_cleanup'), $code); // Replace all of our starting tokens, with Twig style, {% TOKEN %} @@ -58,6 +62,74 @@ class phpbb_template_twig_lexer extends Twig_Lexer return parent::tokenize($code, $filename); } + /** + * 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? + // + // + // + // + // '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 correctly, only needs to be done once + $code = str_replace('', '{% else %}', $code); + + return preg_replace_callback('#(.+?)#s', $callback, $code); + } + /** * preg_replace_callback to clean up IF statements * @@ -68,6 +140,10 @@ class phpbb_template_twig_lexer extends Twig_Lexer */ protected function tag_if_cleanup($matches) { - return ''; + // 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 ''; } } 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 @@ - $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 @@ -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 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; }