[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/phpbb/template/twig/ -> lexer.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\template\twig;
  15  
  16  class lexer extends \Twig_Lexer
  17  {
  18  	public function set_environment(\Twig_Environment $env)
  19      {
  20          $this->env = $env;
  21      }
  22  
  23  	public function tokenize($code, $filename = null)
  24      {
  25          // Handle \Twig_Source format input
  26          if ($code instanceof \Twig_Source)
  27          {
  28              $source = $code;
  29              $code = $source->getCode();
  30              $filename = $source->getName();
  31          }
  32  
  33          // Our phpBB tags
  34          // Commented out tokens are handled separately from the main replace
  35          $phpbb_tags = array(
  36              /*'BEGIN',
  37              'BEGINELSE',
  38              'END',
  39              'IF',
  40              'ELSE',
  41              'ELSEIF',
  42              'ENDIF',
  43              'DEFINE',
  44              'UNDEFINE',*/
  45              'ENDDEFINE',
  46              'INCLUDE',
  47              'INCLUDEPHP',
  48              'INCLUDEJS',
  49              'INCLUDECSS',
  50              'PHP',
  51              'ENDPHP',
  52              'EVENT',
  53          );
  54  
  55          // Twig tag masks
  56          $twig_tags = array(
  57              'autoescape',
  58              'endautoescape',
  59              'if',
  60              'elseif',
  61              'else',
  62              'endif',
  63              'block',
  64              'endblock',
  65              'use',
  66              'extends',
  67              'embed',
  68              'filter',
  69              'endfilter',
  70              'flush',
  71              'for',
  72              'endfor',
  73              'macro',
  74              'endmacro',
  75              'import',
  76              'from',
  77              'sandbox',
  78              'endsandbox',
  79              'set',
  80              'endset',
  81              'spaceless',
  82              'endspaceless',
  83              'verbatim',
  84              'endverbatim',
  85          );
  86  
  87          // Fix tokens that may have inline variables (e.g. <!-- DEFINE $TEST = '{FOO}')
  88          $code = $this->strip_surrounding_quotes(array(
  89              'INCLUDE',
  90              'INCLUDEPHP',
  91              'INCLUDEJS',
  92              'INCLUDECSS',
  93          ), $code);
  94          $code = $this->fix_inline_variable_tokens(array(
  95              'DEFINE \$[a-zA-Z0-9_]+ =',
  96              'INCLUDE',
  97              'INCLUDEPHP',
  98              'INCLUDEJS',
  99              'INCLUDECSS',
 100          ), $code);
 101          $code = $this->add_surrounding_quotes(array(
 102              'INCLUDE',
 103              'INCLUDEPHP',
 104              'INCLUDEJS',
 105              'INCLUDECSS',
 106          ), $code);
 107  
 108          // Fix our BEGIN statements
 109          $code = $this->fix_begin_tokens($code);
 110  
 111          // Fix our IF tokens
 112          $code = $this->fix_if_tokens($code);
 113  
 114          // Fix our DEFINE tokens
 115          $code = $this->fix_define_tokens($code);
 116  
 117          // Replace all of our starting tokens, <!-- TOKEN --> with Twig style, {% TOKEN %}
 118          // This also strips outer parenthesis, <!-- IF (blah) --> becomes <!-- IF blah -->
 119          $code = preg_replace('#<!-- (' . implode('|', $phpbb_tags) . ')(?: (.*?) ?)?-->#', '{% $1 $2 %}', $code);
 120  
 121          // Replace all of our twig masks with Twig code (e.g. <!-- BLOCK .+ --> with {% block $1 %})
 122          $code = $this->replace_twig_tag_masks($code, $twig_tags);
 123  
 124          // Replace all of our language variables, {L_VARNAME}, with Twig style, {{ lang('NAME') }}
 125          // Appends any filters after lang()
 126          $code = preg_replace('#{L_([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ lang(\'$1\')$2 }}', $code);
 127  
 128          // Replace all of our escaped language variables, {LA_VARNAME}, with Twig style, {{ lang('NAME')|escape('js') }}
 129          // Appends any filters after lang(), but before escape('js')
 130          $code = preg_replace('#{LA_([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ lang(\'$1\')$2|escape(\'js\') }}', $code);
 131  
 132          // Replace all of our variables, {VARNAME}, with Twig style, {{ VARNAME }}
 133          // Appends any filters
 134          $code = preg_replace('#{([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ $1$2 }}', $code);
 135  
 136          // Tokenize \Twig_Source instance
 137          return parent::tokenize(new \Twig_Source($code, $filename));
 138      }
 139  
 140      /**
 141      * Strip surrounding quotes
 142      *
 143      * First step to fix tokens that may have inline variables
 144      * E.g. <!-- INCLUDE '{TEST}.html' to <!-- INCLUDE {TEST}.html
 145      *
 146      * @param array $tokens array of tokens to search for (imploded to a regular expression)
 147      * @param string $code
 148      * @return string
 149      */
 150  	protected function strip_surrounding_quotes($tokens, $code)
 151      {
 152          // Remove matching quotes at the beginning/end if a statement;
 153          // E.g. 'asdf'"' -> asdf'"
 154          // E.g. "asdf'"" -> asdf'"
 155          // E.g. 'asdf'" -> 'asdf'"
 156          return preg_replace('#<!-- (' . implode('|', $tokens) . ') (([\'"])?(.*?)\1) -->#', '<!-- $1 $2 -->', $code);
 157      }
 158  
 159      /**
 160      * Fix tokens that may have inline variables
 161      *
 162      * Second step to fix tokens that may have inline variables
 163      * E.g. <!-- INCLUDE '{TEST}.html' to <!-- INCLUDE ' ~ {TEST} ~ '.html
 164      *
 165      * @param array $tokens array of tokens to search for (imploded to a regular expression)
 166      * @param string $code
 167      * @return string
 168      */
 169  	protected function fix_inline_variable_tokens($tokens, $code)
 170      {
 171          $callback = function($matches)
 172          {
 173              // Replace template variables with start/end to parse variables (' ~ TEST ~ '.html)
 174              $matches[2] = preg_replace('#{([a-zA-Z0-9_\.$]+)}#', "'~ \$1 ~'", $matches[2]);
 175  
 176              return "<!-- {$matches[1]} {$matches[2]} -->";
 177          };
 178  
 179          return preg_replace_callback('#<!-- (' . implode('|', $tokens) . ') (.+?) -->#', $callback, $code);
 180      }
 181  
 182      /**
 183      * Add surrounding quotes
 184      *
 185      * Last step to fix tokens that may have inline variables
 186      * E.g. <!-- INCLUDE '{TEST}.html' to <!-- INCLUDE '' ~ {TEST} ~ '.html'
 187      *
 188      * @param array $tokens array of tokens to search for (imploded to a regular expression)
 189      * @param string $code
 190      * @return string
 191      */
 192  	protected function add_surrounding_quotes($tokens, $code)
 193      {
 194          return preg_replace('#<!-- (' . implode('|', $tokens) . ') (.+?) -->#', '<!-- $1 \'$2\' -->', $code);
 195      }
 196  
 197      /**
 198      * Fix begin tokens (convert our BEGIN to Twig for)
 199      *
 200      * Not meant to be used outside of this context, public because the anonymous function calls this
 201      *
 202      * @param string $code
 203      * @param array $parent_nodes (used in recursion)
 204      * @return string
 205      */
 206  	public function fix_begin_tokens($code, $parent_nodes = array())
 207      {
 208          // PHP 5.3 cannot use $this in an anonymous function, so use this as a work-around
 209          $parent_class = $this;
 210          $callback = function ($matches) use ($parent_class, $parent_nodes)
 211          {
 212              $hard_parents = explode('.', $matches[1]);
 213              array_pop($hard_parents); // ends with .
 214              if ($hard_parents)
 215              {
 216                  $parent_nodes = array_merge($hard_parents, $parent_nodes);
 217              }
 218  
 219              $name = $matches[2];
 220              $subset = trim(substr($matches[3], 1, -1)); // Remove parenthesis
 221              $body = $matches[4];
 222  
 223              // Replace <!-- BEGINELSE -->
 224              $body = str_replace('<!-- BEGINELSE -->', '{% else %}', $body);
 225  
 226              // Is the designer wanting to call another loop in a loop?
 227              // <!-- BEGIN loop -->
 228              // <!-- BEGIN !loop2 -->
 229              // <!-- END !loop2 -->
 230              // <!-- END loop -->
 231              // 'loop2' is actually on the same nesting level as 'loop' you assign
 232              // variables to it with template->assign_block_vars('loop2', array(...))
 233              if (strpos($name, '!') === 0)
 234              {
 235                  // Count the number if ! occurrences
 236                  $count = substr_count($name, '!');
 237                  for ($i = 0; $i < $count; $i++)
 238                  {
 239                      array_pop($parent_nodes);
 240                      $name = substr($name, 1);
 241                  }
 242              }
 243  
 244              // Remove all parent nodes, e.g. foo, bar from foo.bar.foobar.VAR
 245              foreach ($parent_nodes as $node)
 246              {
 247                  $body = preg_replace('#([^a-zA-Z0-9_])' . $node . '\.([a-zA-Z0-9_]+)\.#', '$1$2.', $body);
 248              }
 249  
 250              // Add current node to list of parent nodes for child nodes
 251              $parent_nodes[] = $name;
 252  
 253              // Recursive...fix any child nodes
 254              $body = $parent_class->fix_begin_tokens($body, $parent_nodes);
 255  
 256              // Need the parent variable name
 257              array_pop($parent_nodes);
 258              $parent = (!empty($parent_nodes)) ? end($parent_nodes) . '.' : '';
 259  
 260              if ($subset !== '')
 261              {
 262                  $subset = '|subset(' . $subset . ')';
 263              }
 264  
 265              $parent = ($parent) ?: 'loops.';
 266              // Turn into a Twig for loop
 267              return "{% for {$name} in {$parent}{$name}{$subset} %}{$body}{% endfor %}";
 268          };
 269  
 270          return preg_replace_callback('#<!-- BEGIN ((?:[a-zA-Z0-9_]+\.)*)([!a-zA-Z0-9_]+)(\([0-9,\-]+\))? -->(.+?)<!-- END \1\2 -->#s', $callback, $code);
 271      }
 272  
 273      /**
 274      * Fix IF statements
 275      *
 276      * @param string $code
 277      * @return string
 278      */
 279  	protected function fix_if_tokens($code)
 280      {
 281          // Replace ELSE IF with ELSEIF
 282          $code = preg_replace('#<!-- ELSE IF (.+?) -->#', '<!-- ELSEIF $1 -->', $code);
 283  
 284          // Replace our "div by" with Twig's divisibleby (Twig does not like test names with spaces)
 285          $code = preg_replace('# div by ([0-9]+)#', ' divisibleby($1)', $code);
 286  
 287          $callback = function($matches)
 288          {
 289              $inner = $matches[2];
 290              // Replace $TEST with definition.TEST
 291              $inner = preg_replace('#(\s\(*!?)\$([a-zA-Z_0-9]+)#', '$1definition.$2', $inner);
 292  
 293              // Replace .foo with loops.foo|length
 294              $inner = preg_replace('#(\s\(*!?)\.([a-zA-Z_0-9]+)([^a-zA-Z_0-9\.])#', '$1loops.$2|length$3', $inner);
 295  
 296              // Replace .foo.bar with foo.bar|length
 297              $inner = preg_replace('#(\s\(*!?)\.([a-zA-Z_0-9\.]+)([^a-zA-Z_0-9\.])#', '$1$2|length$3', $inner);
 298  
 299              return "<!-- {$matches[1]}IF{$inner}-->";
 300          };
 301  
 302          return preg_replace_callback('#<!-- (ELSE)?IF((.*?) (?:\(*!?[\$|\.]([^\s]+)(.*?))?)-->#', $callback, $code);
 303      }
 304  
 305      /**
 306      * Fix DEFINE statements and {$VARNAME} variables
 307      *
 308      * @param string $code
 309      * @return string
 310      */
 311  	protected function fix_define_tokens($code)
 312      {
 313          /**
 314          * Changing $VARNAME to definition.varname because set is only local
 315          * context (e.g. DEFINE $TEST will only make $TEST available in current
 316          * template and any child templates, but not any parent templates).
 317          *
 318          * DEFINE handles setting it properly to definition in its node, but the
 319          * variables reading FROM it need to be altered to definition.VARNAME
 320          *
 321          * Setting up definition as a class in the array passed to Twig
 322          * ($context) makes set definition.TEST available in the global context
 323          */
 324  
 325          // Replace <!-- DEFINE $NAME with {% DEFINE definition.NAME
 326          $code = preg_replace('#<!-- DEFINE \$(.*?) -->#', '{% DEFINE $1 %}', $code);
 327  
 328          // Changing UNDEFINE NAME to DEFINE NAME = null to save from creating an extra token parser/node
 329          $code = preg_replace('#<!-- UNDEFINE \$(.*?)-->#', '{% DEFINE $1= null %}', $code);
 330  
 331          // Replace all of our variables, {$VARNAME}, with Twig style, {{ definition.VARNAME }}
 332          $code = preg_replace('#{\$([a-zA-Z0-9_\.]+)}#', '{{ definition.$1 }}', $code);
 333  
 334          // Replace all of our variables, ~ $VARNAME ~, with Twig style, ~ definition.VARNAME ~
 335          $code = preg_replace('#~ \$([a-zA-Z0-9_\.]+) ~#', '~ definition.$1 ~', $code);
 336  
 337          return $code;
 338      }
 339  
 340      /**
 341      * Replace Twig tag masks with Twig tag calls
 342      *
 343      * E.g. <!-- BLOCK foo --> with {% block foo %}
 344      *
 345      * @param string $code
 346      * @param array $twig_tags All tags we want to create a mask for
 347      * @return string
 348      */
 349  	protected function replace_twig_tag_masks($code, $twig_tags)
 350      {
 351          $callback = function ($matches)
 352          {
 353              $matches[1] = strtolower($matches[1]);
 354  
 355              return "{% {$matches[1]}{$matches[2]}%}";
 356          };
 357  
 358          foreach ($twig_tags as &$tag)
 359          {
 360              $tag = strtoupper($tag);
 361          }
 362  
 363          // twig_tags is an array of the twig tags, which are all lowercase, but we use all uppercase tags
 364          $code = preg_replace_callback('#<!-- (' . implode('|', $twig_tags) . ')(.*?)-->#',$callback, $code);
 365  
 366          return $code;
 367      }
 368  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1