1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<?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
{
public function tokenize($code, $filename = null)
{
$valid_starting_tokens = array(
'BEGIN',
'BEGINELSE',
'END',
'IF',
'ELSE',
'ELSEIF',
'ENDIF',
'DEFINE',
'DEFINE',
'UNDEFINE',
'ENDDEFINE',
/*'INCLUDE',
'INCLUDEPHP',
'INCLUDEJS',*/
'PHP',
'ENDPHP',
'EVENT',
);
// Replace <!-- INCLUDE blah.html --> with {% include 'blah.html' %}
$code = preg_replace('#<!-- INCLUDE(PHP|JS)? (.*?) -->#', "{% INCLUDE$1 '$2' %}", $code);
// Replace all of our starting tokens, <!-- TOKEN --> with Twig style, {% TOKEN %}
// This also strips the $ inside of a tag directly after the token, which was used in <!-- DEFINE $NAME
// This also strips the . inside of a tag directly after the token, which was used in <!-- IF .blah
// This also strips outer parenthesis, <!-- IF (blah) --> becomes <!-- IF blah -->
$code = preg_replace('#<!-- (' . implode('|', $valid_starting_tokens) . ') (not )?(\$|\.)?(?:(.*?) ?)?-->#', '{% $1 $2$4 %}', $code);
// Replace all of our variables, {VARNAME} or {$VARNAME}, with Twig style, {{ VARNAME }}
$code = preg_replace('#{\$?([A-Z_][A-Z_0-9]+)}#', '{{ $1 }}', $code);
//echo $code;
//exit;
return parent::tokenize($code, $filename);
}
}
|