mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-27 21:58:52 +00:00
[feature/twig] Fixing more stuff for DEFINE/INCLUDE
PHPBB3-11598
This commit is contained in:
parent
64963b5962
commit
f18cbd50f0
2 changed files with 44 additions and 9 deletions
|
@ -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);
|
||||
|
@ -61,6 +65,31 @@ class phpbb_template_twig_lexer extends Twig_Lexer
|
|||
return parent::tokenize($code, $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*
|
||||
|
@ -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]);
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue