2013-06-09 22:09:00 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package phpBB3
|
|
|
|
* @copyright (c) 2013 phpBB Group
|
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
*/
|
|
|
|
if (!defined('IN_PHPBB'))
|
|
|
|
{
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
class phpbb_template_twig_lexer extends Twig_Lexer
|
|
|
|
{
|
2013-06-10 11:17:11 -05:00
|
|
|
public function tokenize($code, $filename = null)
|
2013-06-09 22:09:00 -05:00
|
|
|
{
|
2013-07-04 12:44:12 -05:00
|
|
|
// Our phpBB tags
|
|
|
|
// Commented out tokens are handled separately from the main replace
|
|
|
|
$phpbb_tags = array(
|
2013-06-26 12:30:59 -05:00
|
|
|
/*'BEGIN',
|
2013-06-10 11:17:11 -05:00
|
|
|
'BEGINELSE',
|
2013-07-05 14:10:57 -05:00
|
|
|
'END',
|
2013-06-10 11:17:11 -05:00
|
|
|
'IF',
|
|
|
|
'ELSE',
|
|
|
|
'ELSEIF',
|
|
|
|
'ENDIF',
|
2013-07-05 14:10:57 -05:00
|
|
|
'DEFINE',
|
2013-06-29 11:07:10 -05:00
|
|
|
'UNDEFINE',*/
|
2013-06-10 11:17:11 -05:00
|
|
|
'ENDDEFINE',
|
2013-06-29 19:19:18 -05:00
|
|
|
'INCLUDE',
|
|
|
|
'INCLUDEPHP',
|
2013-06-24 13:32:31 -05:00
|
|
|
'INCLUDEJS',
|
2013-07-11 15:22:05 -05:00
|
|
|
'INCLUDECSS',
|
2013-06-10 11:17:11 -05:00
|
|
|
'PHP',
|
|
|
|
'ENDPHP',
|
|
|
|
'EVENT',
|
|
|
|
);
|
2013-06-11 10:57:00 -05:00
|
|
|
|
2013-07-04 12:44:12 -05:00
|
|
|
// Twig tag masks
|
|
|
|
$twig_tags = array(
|
|
|
|
'autoescape',
|
|
|
|
'endautoescape',
|
2013-07-05 14:10:57 -05:00
|
|
|
'if',
|
|
|
|
'elseif',
|
|
|
|
'else',
|
|
|
|
'endif',
|
2013-07-04 12:44:12 -05:00
|
|
|
'block',
|
|
|
|
'endblock',
|
|
|
|
'use',
|
|
|
|
'extends',
|
|
|
|
'embed',
|
|
|
|
'filter',
|
|
|
|
'endfilter',
|
|
|
|
'flush',
|
|
|
|
'for',
|
|
|
|
'endfor',
|
|
|
|
'macro',
|
|
|
|
'endmacro',
|
|
|
|
'import',
|
|
|
|
'from',
|
|
|
|
'sandbox',
|
|
|
|
'endsandbox',
|
|
|
|
'set',
|
|
|
|
'endset',
|
|
|
|
'spaceless',
|
|
|
|
'endspaceless',
|
|
|
|
'verbatim',
|
|
|
|
'endverbatim',
|
|
|
|
);
|
|
|
|
|
2013-06-29 19:19:18 -05:00
|
|
|
// Fix tokens that may have inline variables (e.g. <!-- DEFINE $TEST = '{FOO}')
|
|
|
|
$code = $this->fix_inline_variable_tokens(array(
|
|
|
|
'DEFINE.+=',
|
|
|
|
'INCLUDE',
|
|
|
|
'INCLUDEPHP',
|
2013-07-01 12:26:01 -05:00
|
|
|
'INCLUDEJS',
|
2013-07-11 15:22:05 -05:00
|
|
|
'INCLUDECSS',
|
2013-06-29 19:19:18 -05:00
|
|
|
), $code);
|
|
|
|
|
2013-06-26 12:30:59 -05:00
|
|
|
// Fix our BEGIN statements
|
|
|
|
$code = $this->fix_begin_tokens($code);
|
|
|
|
|
2013-06-29 11:07:10 -05:00
|
|
|
// Fix our IF tokens
|
|
|
|
$code = $this->fix_if_tokens($code);
|
2013-06-10 00:59:06 -05:00
|
|
|
|
2013-06-29 11:07:10 -05:00
|
|
|
// Fix our DEFINE tokens
|
|
|
|
$code = $this->fix_define_tokens($code);
|
2013-06-11 10:57:00 -05:00
|
|
|
|
2013-06-10 11:17:11 -05:00
|
|
|
// Replace all of our starting tokens, <!-- TOKEN --> with Twig style, {% TOKEN %}
|
|
|
|
// This also strips outer parenthesis, <!-- IF (blah) --> becomes <!-- IF blah -->
|
2013-07-04 12:44:12 -05:00
|
|
|
$code = preg_replace('#<!-- (' . implode('|', $phpbb_tags) . ')(?: (.*?) ?)?-->#', '{% $1 $2 %}', $code);
|
|
|
|
|
|
|
|
// Replace all of our twig masks with Twig code (e.g. <!-- BLOCK .+ --> with {% block $1 %})
|
|
|
|
$code = $this->replace_twig_tag_masks($code, $twig_tags);
|
2013-06-11 10:57:00 -05:00
|
|
|
|
2013-07-01 09:32:21 -05:00
|
|
|
// Replace all of our language variables, {L_VARNAME}, with Twig style, {{ lang('NAME') }}
|
2013-07-04 12:44:12 -05:00
|
|
|
// Appends any filters after lang()
|
2013-07-08 15:16:37 -05:00
|
|
|
$code = preg_replace('#{L_([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ lang(\'$1\')$2 }}', $code);
|
2013-07-01 09:32:21 -05:00
|
|
|
|
2013-07-01 10:17:58 -05:00
|
|
|
// Replace all of our escaped language variables, {LA_VARNAME}, with Twig style, {{ lang('NAME')|addslashes }}
|
2013-07-04 12:44:12 -05:00
|
|
|
// Appends any filters after lang(), but before addslashes
|
2013-07-08 15:16:37 -05:00
|
|
|
$code = preg_replace('#{LA_([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ lang(\'$1\')$2|addslashes }}', $code);
|
2013-07-01 09:32:21 -05:00
|
|
|
|
2013-06-29 11:07:10 -05:00
|
|
|
// Replace all of our variables, {VARNAME}, with Twig style, {{ VARNAME }}
|
2013-07-04 12:44:12 -05:00
|
|
|
// Appends any filters
|
2013-07-08 15:16:37 -05:00
|
|
|
$code = preg_replace('#{([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ $1$2 }}', $code);
|
2013-06-11 10:57:00 -05:00
|
|
|
|
2013-06-10 11:17:11 -05:00
|
|
|
return parent::tokenize($code, $filename);
|
2013-06-09 22:09:00 -05:00
|
|
|
}
|
2013-06-11 10:57:00 -05:00
|
|
|
|
2013-06-29 19:19:18 -05:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2013-07-19 14:12:28 -05:00
|
|
|
// Remove matching quotes at the beginning/end if a statement;
|
|
|
|
// E.g. 'asdf'"' -> asdf'"
|
|
|
|
// E.g. "asdf'"" -> asdf'"
|
|
|
|
// E.g. 'asdf'" -> 'asdf'"
|
|
|
|
$matches[2] = preg_replace('#^([\'"])?(.+?)\1$#', '$2', $matches[2]);
|
|
|
|
|
|
|
|
// Replace template variables with start/end to parse variables (' ~ TEST ~ '.html)
|
|
|
|
$matches[2] = preg_replace('#{([a-zA-Z0-9_\.$]+)}#', "'~ \$1 ~'", $matches[2]);
|
2013-06-29 19:19:18 -05:00
|
|
|
|
|
|
|
// Surround the matches in single quotes ('' ~ TEST ~ '.html')
|
|
|
|
return "<!-- {$matches[1]} '{$matches[2]}' -->";
|
|
|
|
};
|
|
|
|
|
|
|
|
return preg_replace_callback('#<!-- (' . implode('|', $tokens) . ') (.+?) -->#', $callback, $code);
|
|
|
|
}
|
|
|
|
|
2013-06-26 12:30:59 -05:00
|
|
|
/**
|
|
|
|
* 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
|
2013-06-29 11:07:10 -05:00
|
|
|
* @param array $parent_nodes (used in recursion)
|
2013-06-26 12:30:59 -05:00
|
|
|
* @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];
|
2013-06-28 15:40:30 -05:00
|
|
|
$subset = trim(substr($matches[2], 1, -1)); // Remove parenthesis
|
2013-06-26 12:30:59 -05:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 14:24:48 -05:00
|
|
|
// Remove all parent nodes, e.g. foo, bar from foo.bar.foobar.VAR
|
2013-06-26 12:30:59 -05:00
|
|
|
foreach ($parent_nodes as $node)
|
|
|
|
{
|
2013-07-02 14:24:48 -05:00
|
|
|
$body = preg_replace('#([^a-zA-Z0-9_])' . $node . '\.([a-zA-Z0-9_]+)\.#', '$1$2.', $body);
|
2013-06-26 12:30:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Need the parent variable name
|
|
|
|
array_pop($parent_nodes);
|
2013-07-14 12:10:49 -05:00
|
|
|
$parent = (!empty($parent_nodes)) ? end($parent_nodes) . '.' : '';
|
2013-06-26 12:30:59 -05:00
|
|
|
|
2013-06-28 15:40:30 -05:00
|
|
|
if ($subset !== '')
|
|
|
|
{
|
|
|
|
$subset = '|subset(' . $subset . ')';
|
|
|
|
}
|
2013-06-26 12:30:59 -05:00
|
|
|
|
2013-07-14 12:10:49 -05:00
|
|
|
// Turn into a Twig for loop
|
|
|
|
return "{% for {$name} in loops.{$parent}{$name}{$subset} %}{$body}{% endfor %}";
|
2013-06-26 12:30:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Replace <!-- BEGINELSE --> correctly, only needs to be done once
|
|
|
|
$code = str_replace('<!-- BEGINELSE -->', '{% else %}', $code);
|
|
|
|
|
2013-06-28 15:40:30 -05:00
|
|
|
return preg_replace_callback('#<!-- BEGIN ([!a-zA-Z0-9_]+)(\([0-9,\-]+\))? -->(.+?)<!-- END \1 -->#s', $callback, $code);
|
2013-06-26 12:30:59 -05:00
|
|
|
}
|
|
|
|
|
2013-06-11 10:57:00 -05:00
|
|
|
/**
|
2013-06-29 11:07:10 -05:00
|
|
|
* Fix IF statements
|
2013-06-11 10:57:00 -05:00
|
|
|
*
|
2013-06-29 11:07:10 -05:00
|
|
|
* @param string $code
|
|
|
|
* @return string
|
2013-06-11 10:57:00 -05:00
|
|
|
*/
|
2013-06-29 11:07:10 -05:00
|
|
|
protected function fix_if_tokens($code)
|
2013-06-11 10:57:00 -05:00
|
|
|
{
|
2013-06-29 11:07:10 -05:00
|
|
|
$callback = function($matches)
|
|
|
|
{
|
2013-07-19 13:08:41 -05:00
|
|
|
$inner = $matches[2];
|
2013-06-29 11:07:10 -05:00
|
|
|
// Replace $TEST with definition.TEST
|
2013-07-19 13:08:41 -05:00
|
|
|
$inner = preg_replace('#\s\$([a-zA-Z_0-9]+)#', ' definition.$1', $inner);
|
2013-06-26 12:30:59 -05:00
|
|
|
|
2013-06-29 11:07:10 -05:00
|
|
|
// Replace .test with test|length
|
2013-07-19 13:08:41 -05:00
|
|
|
$inner = preg_replace('#\s\.([a-zA-Z_0-9\.]+)#', ' $1|length', $inner);
|
2013-06-29 11:07:10 -05:00
|
|
|
|
2013-07-19 13:08:41 -05:00
|
|
|
return "<!-- {$matches[1]}IF{$inner}-->";
|
2013-06-29 11:07:10 -05:00
|
|
|
};
|
|
|
|
|
2013-07-01 09:19:30 -05:00
|
|
|
// Replace our "div by" with Twig's divisibleby (Twig does not like test names with spaces)
|
|
|
|
$code = preg_replace('# div by ([0-9]+)#', ' divisibleby($1)', $code);
|
|
|
|
|
2013-07-19 13:08:41 -05:00
|
|
|
return preg_replace_callback('#<!-- (ELSE)?IF((.*)[\s][\$|\.|!]([^\s]+)(.*))-->#', $callback, $code);
|
2013-06-29 11:07:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fix DEFINE statements and {$VARNAME} variables
|
|
|
|
*
|
|
|
|
* @param string $code
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function fix_define_tokens($code)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Changing $VARNAME to definition.varname because set is only local
|
|
|
|
* context (e.g. DEFINE $TEST will only make $TEST available in current
|
|
|
|
* template and any child templates, but not any parent templates).
|
|
|
|
*
|
|
|
|
* DEFINE handles setting it properly to definition in its node, but the
|
|
|
|
* variables reading FROM it need to be altered to definition.VARNAME
|
|
|
|
*
|
|
|
|
* Setting up definition as a class in the array passed to Twig
|
|
|
|
* ($context) makes set definition.TEST available in the global context
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Replace <!-- DEFINE $NAME with {% DEFINE definition.NAME
|
|
|
|
$code = preg_replace('#<!-- DEFINE \$(.*)-->#', '{% DEFINE $1 %}', $code);
|
|
|
|
|
|
|
|
// Changing UNDEFINE NAME to DEFINE NAME = null to save from creating an extra token parser/node
|
|
|
|
$code = preg_replace('#<!-- UNDEFINE \$(.*)-->#', '{% DEFINE $1= null %}', $code);
|
|
|
|
|
|
|
|
// Replace all of our variables, {$VARNAME}, with Twig style, {{ definition.VARNAME }}
|
|
|
|
$code = preg_replace('#{\$([a-zA-Z0-9_\.]+)}#', '{{ definition.$1 }}', $code);
|
|
|
|
|
2013-06-29 19:19:18 -05:00
|
|
|
// Replace all of our variables, ~ $VARNAME ~, with Twig style, ~ definition.VARNAME ~
|
|
|
|
$code = preg_replace('#~ \$([a-zA-Z0-9_\.]+) ~#', '~ definition.$1 ~', $code);
|
|
|
|
|
2013-06-29 11:07:10 -05:00
|
|
|
return $code;
|
2013-06-11 10:57:00 -05:00
|
|
|
}
|
2013-07-04 12:44:12 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace Twig tag masks with Twig tag calls
|
|
|
|
*
|
|
|
|
* E.g. <!-- BLOCK foo --> with {% block foo %}
|
|
|
|
*
|
|
|
|
* @param string $code
|
|
|
|
* @param array $twig_tags All tags we want to create a mask for
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function replace_twig_tag_masks($code, $twig_tags)
|
|
|
|
{
|
|
|
|
$callback = function ($matches)
|
|
|
|
{
|
|
|
|
$matches[1] = strtolower($matches[1]);
|
|
|
|
|
|
|
|
return "{% {$matches[1]}{$matches[2]}%}";
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach ($twig_tags as &$tag)
|
|
|
|
{
|
|
|
|
$tag = strtoupper($tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
// twig_tags is an array of the twig tags, which are all lowercase, but we use all uppercase tags
|
|
|
|
$code = preg_replace_callback('#<!-- (' . implode('|', $twig_tags) . ')(.*?)-->#',$callback, $code);
|
|
|
|
|
|
|
|
return $code;
|
|
|
|
}
|
2013-06-09 22:09:00 -05:00
|
|
|
}
|