3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Template { private $_template = ''; private $_assigns = array(); private $_prepared_code = null; const VALID_VARIABLE = '[a-zA-Z0-9_\x7f-\xff]*'; const TAG_SIGN_OPEN = '{'; const TAG_SIGN_CLOSE = '}'; public function __construct($template) { $this->_template = $template; } public function assign($name, $value) { $this->_assign($name, $value); } private function _assign($name, $value, array &$assigns = null) { if ($assigns == null) $this->_assigns[$name] = $value; else $assigns[$name] = $value; } public function assignArray($array) { $this->_assignArray($array); } private function _assignArray($array, array &$assigns = null) { foreach ($array as $name => $value) $this->_assign($name, $value, $assigns); } private function _getToken($string, $sign, $pos, &$token) { $position = strpos($string, $sign, $pos); if ($position === false) { $token = substr($string, $pos); $pos = false; } else { $token = substr($string, $pos, $position - $pos); $pos = $position + strlen($sign); } return $pos; } private function _prepareCode() { $prepared_code = '?' . '>'; $pos = 0; while ($pos !== false) { $pos = $this->_getToken($this->_template, self::TAG_SIGN_OPEN, $pos, $token); $prepared_code .= $token; if ($pos !== false) { $pos = $this->_getToken($this->_template, self::TAG_SIGN_CLOSE, $pos, $token); if ($pos === false) throw new Exception('Template syntax error. Expected "}"'); switch (true) { case preg_match('/^' . self::VALID_VARIABLE . '$/', $token): $prepared_code .= '<?php echo $assigns[\'' . $token . '\'] ?' . '>'; break; case preg_match('/^loop +(' . self::VALID_VARIABLE . ')$/', $token, $match): $prepared_code .= '<?php foreach ($assigns[\'' . $match[1] . '\'] as $__index => $__tmp)' . '{$this->_assignArray($__tmp, $assigns); unset($__tmp); ?' . '>'; break; case preg_match('/^end +loop$/', $token): $prepared_code .= '<?php } ?>'; break; case preg_match('/^\%index\%$/', $token): $prepared_code .= '<?php echo $__index ?' . '>'; break; default: throw new Exception('Template syntax error. Unexpected token "' . $token . '"'); break; } } } return $prepared_code; } public function getPreparedCode() { if ($this->_prepared_code === null) $this->_prepared_code = $this->_prepareCode(); return $this->_prepared_code; } private function _execute($prepared_code) { $assigns = $this->_assigns; ob_start(); eval($prepared_code); $content = ob_get_contents(); ob_end_clean(); return $content; } public function parse() { return $this->_execute($this->getPreparedCode()); } } $templateHtml = <<<END_HTML <div>{this}</div> {loop items} <div>{%index%}. {name}</div> {end loop} END_HTML; $template = new Template($templateHtml); $template->assign('this', 'test'); $template->assign('items', array( array('name' => 'First'), array('name' => 'Second') )); echo $template->parse();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VmXX4
function name:  (null)
number of ops:  17
compiled vars:  !0 = $templateHtml, !1 = $template
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  124     0  E >   ASSIGN                                                   !0, '%3Cdiv%3E%7Bthis%7D%3C%2Fdiv%3E%0A%7Bloop+items%7D%0A+++%3Cdiv%3E%7B%25index%25%7D.+%7Bname%7D%3C%2Fdiv%3E%0A%7Bend+loop%7D'
  131     1        NEW                                              $3      'Template'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $3
  132     5        INIT_METHOD_CALL                                         !1, 'assign'
          6        SEND_VAL_EX                                              'this'
          7        SEND_VAL_EX                                              'test'
          8        DO_FCALL                                      0          
  133     9        INIT_METHOD_CALL                                         !1, 'assign'
         10        SEND_VAL_EX                                              'items'
  134    11        SEND_VAL_EX                                              <array>
         12        DO_FCALL                                      0          
  137    13        INIT_METHOD_CALL                                         !1, 'parse'
         14        DO_FCALL                                      0  $8      
         15        ECHO                                                     $8
         16      > RETURN                                                   1

Class Template:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VmXX4
function name:  __construct
number of ops:  4
compiled vars:  !0 = $template
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   15     1        ASSIGN_OBJ                                               '_template'
          2        OP_DATA                                                  !0
   16     3      > RETURN                                                   null

End of function __construct

Function assign:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VmXX4
function name:  assign
number of ops:  7
compiled vars:  !0 = $name, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   20     2        INIT_METHOD_CALL                                         '_assign'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0          
   21     6      > RETURN                                                   null

End of function assign

Function _assign:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VmXX4
function name:  _assign
number of ops:  12
compiled vars:  !0 = $name, !1 = $value, !2 = $assigns
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
   25     3        IS_EQUAL                                                 !2, null
          4      > JMPZ                                                     ~3, ->9
   26     5    >   FETCH_OBJ_W                                      $4      '_assigns'
          6        ASSIGN_DIM                                               $4, !0
          7        OP_DATA                                                  !1
          8      > JMP                                                      ->11
   28     9    >   ASSIGN_DIM                                               !2, !0
         10        OP_DATA                                                  !1
   29    11    > > RETURN                                                   null

End of function _assign

Function assignarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VmXX4
function name:  assignArray
number of ops:  5
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   33     1        INIT_METHOD_CALL                                         '_assignArray'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
   34     4      > RETURN                                                   null

End of function assignarray

Function _assignarray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/VmXX4
function name:  _assignArray
number of ops:  13
compiled vars:  !0 = $array, !1 = $assigns, !2 = $value, !3 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   38     2      > FE_RESET_R                                       $4      !0, ->11
          3    > > FE_FETCH_R                                       ~5      $4, !2, ->11
          4    >   ASSIGN                                                   !3, ~5
   39     5        INIT_METHOD_CALL                                         '_assign'
          6        SEND_VAR                                                 !3
          7        SEND_VAR                                                 !2
          8        SEND_REF                                                 !1
          9        DO_FCALL                                      0          
   38    10      > JMP                                                      ->3
         11    >   FE_FREE                                                  $4
   40    12      > RETURN                                                   null

End of function _assignarray

Function _gettoken:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 19
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VmXX4
function name:  _getToken
number of ops:  31
compiled vars:  !0 = $string, !1 = $sign, !2 = $pos, !3 = $token, !4 = $position
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   44     4        INIT_FCALL                                               'strpos'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        SEND_VAR                                                 !2
          8        DO_ICALL                                         $5      
          9        ASSIGN                                                   !4, $5
   45    10        TYPE_CHECK                                    4          !4
         11      > JMPZ                                                     ~7, ->19
   47    12    >   INIT_FCALL                                               'substr'
         13        SEND_VAR                                                 !0
         14        SEND_VAR                                                 !2
         15        DO_ICALL                                         $8      
         16        ASSIGN                                                   !3, $8
   48    17        ASSIGN                                                   !2, <false>
         18      > JMP                                                      ->29
   52    19    >   INIT_FCALL                                               'substr'
         20        SEND_VAR                                                 !0
         21        SEND_VAR                                                 !2
         22        SUB                                              ~11     !4, !2
         23        SEND_VAL                                                 ~11
         24        DO_ICALL                                         $12     
         25        ASSIGN                                                   !3, $12
   53    26        STRLEN                                           ~14     !1
         27        ADD                                              ~15     !4, ~14
         28        ASSIGN                                                   !2, ~15
   55    29    > > RETURN                                                   !2
   56    30*     > RETURN                                                   null

End of function _gettoken

Function _preparecode:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
2 jumps found. (Code = 44) Position 1 = 75, Position 2 = 3
Branch analysis from position: 75
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 73
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 28
Branch analysis from position: 24
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 50
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 55
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 44, Position 2 = 62
Branch analysis from position: 44
2 jumps found. (Code = 44) Position 1 = 49, Position 2 = 64
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 66
Branch analysis from position: 66
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 73
Branch analysis from position: 73
Branch analysis from position: 73
filename:       /in/VmXX4
function name:  _prepareCode
number of ops:  77
compiled vars:  !0 = $prepared_code, !1 = $pos, !2 = $token, !3 = $match
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   ASSIGN                                                   !0, '%3F%3E'
   61     1        ASSIGN                                                   !1, 0
   62     2      > JMP                                                      ->73
   64     3    >   INIT_METHOD_CALL                                         '_getToken'
          4        FETCH_OBJ_R                                      ~6      '_template'
          5        SEND_VAL                                                 ~6
          6        SEND_VAL                                                 '%7B'
          7        SEND_VAR                                                 !1
          8        SEND_REF                                                 !2
          9        DO_FCALL                                      0  $7      
         10        ASSIGN                                                   !1, $7
   65    11        ASSIGN_OP                                     8          !0, !2
   66    12        TYPE_CHECK                                  1018          !1
         13      > JMPZ                                                     ~10, ->73
   68    14    >   INIT_METHOD_CALL                                         '_getToken'
         15        FETCH_OBJ_R                                      ~11     '_template'
         16        SEND_VAL                                                 ~11
         17        SEND_VAL                                                 '%7D'
         18        SEND_VAR                                                 !1
         19        SEND_REF                                                 !2
         20        DO_FCALL                                      0  $12     
         21        ASSIGN                                                   !1, $12
   69    22        TYPE_CHECK                                    4          !1
         23      > JMPZ                                                     ~14, ->28
   70    24    >   NEW                                              $15     'Exception'
         25        SEND_VAL_EX                                              'Template+syntax+error.+Expected+%22%7D%22'
         26        DO_FCALL                                      0          
         27      > THROW                                         0          $15
   74    28    >   INIT_FCALL                                               'preg_match'
         29        SEND_VAL                                                 '%2F%5E%5Ba-zA-Z0-9_%5Cx7f-%5Cxff%5D%2A%24%2F'
         30        SEND_VAR                                                 !2
         31        DO_ICALL                                         $18     
         32      > JMPNZ                                                    $18, ->50
   78    33    >   INIT_FCALL                                               'preg_match'
         34        SEND_VAL                                                 '%2F%5Eloop+%2B%28%5Ba-zA-Z0-9_%5Cx7f-%5Cxff%5D%2A%29%24%2F'
         35        SEND_VAR                                                 !2
         36        SEND_REF                                                 !3
         37        DO_ICALL                                         $19     
         38      > JMPNZ                                                    $19, ->55
   83    39    >   INIT_FCALL                                               'preg_match'
         40        SEND_VAL                                                 '%2F%5Eend+%2Bloop%24%2F'
         41        SEND_VAR                                                 !2
         42        DO_ICALL                                         $20     
         43      > JMPNZ                                                    $20, ->62
   87    44    >   INIT_FCALL                                               'preg_match'
         45        SEND_VAL                                                 '%2F%5E%5C%25index%5C%25%24%2F'
         46        SEND_VAR                                                 !2
         47        DO_ICALL                                         $21     
         48      > JMPNZ                                                    $21, ->64
         49    > > JMP                                                      ->66
   75    50    >   CONCAT                                           ~22     '%3C%3Fphp+echo+%24assigns%5B%27', !2
         51        CONCAT                                           ~23     ~22, '%27%5D+%3F'
         52        CONCAT                                           ~24     ~23, '%3E'
         53        ASSIGN_OP                                     8          !0, ~24
   76    54      > JMP                                                      ->73
   79    55    >   FETCH_DIM_R                                      ~26     !3, 1
         56        CONCAT                                           ~27     '%3C%3Fphp+foreach+%28%24assigns%5B%27', ~26
         57        CONCAT                                           ~28     ~27, '%27%5D+as+%24__index+%3D%3E+%24__tmp%29'
   80    58        CONCAT                                           ~29     ~28, '%7B%24this-%3E_assignArray%28%24__tmp%2C+%24assigns%29%3B+unset%28%24__tmp%29%3B+%3F'
         59        CONCAT                                           ~30     ~29, '%3E'
         60        ASSIGN_OP                                     8          !0, ~30
   81    61      > JMP                                                      ->73
   84    62    >   ASSIGN_OP                                     8          !0, '%3C%3Fphp+%7D+%3F%3E'
   85    63      > JMP                                                      ->73
   88    64    >   ASSIGN_OP                                     8          !0, '%3C%3Fphp+echo+%24__index+%3F%3E'
   89    65      > JMP                                                      ->73
   92    66    >   NEW                                              $34     'Exception'
         67        CONCAT                                           ~35     'Template+syntax+error.+Unexpected+token+%22', !2
         68        CONCAT                                           ~36     ~35, '%22'
         69        SEND_VAL_EX                                              ~36
         70        DO_FCALL                                      0          
         71      > THROW                                         0          $34
   93    72*       JMP                                                      ->73
   62    73    >   TYPE_CHECK                                  1018          !1
         74      > JMPNZ                                                    ~38, ->3
   97    75    > > RETURN                                                   !0
   98    76*     > RETURN                                                   null

End of function _preparecode

Function getpreparedcode:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/VmXX4
function name:  getPreparedCode
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  102     0  E >   FETCH_OBJ_R                                      ~0      '_prepared_code'
          1        TYPE_CHECK                                    2          ~0
          2      > JMPZ                                                     ~1, ->7
  103     3    >   INIT_METHOD_CALL                                         '_prepareCode'
          4        DO_FCALL                                      0  $3      
          5        ASSIGN_OBJ                                               '_prepared_code'
          6        OP_DATA                                                  $3
  104     7    >   FETCH_OBJ_R                                      ~4      '_prepared_code'
          8      > RETURN                                                   ~4
  105     9*     > RETURN                                                   null

End of function getpreparedcode

Function _execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VmXX4
function name:  _execute
number of ops:  13
compiled vars:  !0 = $prepared_code, !1 = $assigns, !2 = $content
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  107     0  E >   RECV                                             !0      
  109     1        FETCH_OBJ_R                                      ~3      '_assigns'
          2        ASSIGN                                                   !1, ~3
  110     3        INIT_FCALL                                               'ob_start'
          4        DO_ICALL                                                 
  111     5        INCLUDE_OR_EVAL                                          !0, EVAL
  112     6        INIT_FCALL                                               'ob_get_contents'
          7        DO_ICALL                                         $7      
          8        ASSIGN                                                   !2, $7
  113     9        INIT_FCALL                                               'ob_end_clean'
         10        DO_ICALL                                                 
  114    11      > RETURN                                                   !2
  115    12*     > RETURN                                                   null

End of function _execute

Function parse:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VmXX4
function name:  parse
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  119     0  E >   INIT_METHOD_CALL                                         '_execute'
          1        INIT_METHOD_CALL                                         'getPreparedCode'
          2        DO_FCALL                                      0  $0      
          3        SEND_VAR                                                 $0
          4        DO_FCALL                                      0  $1      
          5      > RETURN                                                   $1
  120     6*     > RETURN                                                   null

End of function parse

End of class Template.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.71 ms | 1416 KiB | 25 Q