3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Router { protected $routes = []; protected $params = [] ; protected $namespace = 'App\Controllers\\'; public function add($route , $params ) { $route = preg_replace('/^\//','' , $route); $route = preg_replace('/\//' , '\\/' , $route); $route = preg_replace('/\{([a-z]+)\}/' , '?([a-z0-9ا-ی]+)' , $route); $route = '/^' . $route . '\/?$/i'; if(is_string($params)) { list($AllParams['controller'] , $AllParams['method']) = explode('@' , $params); } if(is_array($params)) { list($AllParams['controller'] , $AllParams['method']) = explode('@' , $params['uses']); unset($params['uses']); $AllParams = array_merge($AllParams , $params); } $this->routes[$route] = $AllParams; } public function match($url) { foreach ($this->routes as $route => $params) { if(preg_match($route , $url , $matches)) { foreach ($matches as $key => $match) { if(is_string($key)) { $params['params'][$key] = $match; } } $this->params = $params; return true; } } return false; } public function dispatch($url) { $url = $this->removeVariblesOfQueryString($url); if($this->match($url)) { $controller = $this->params['controller']; $controller = $this->getNameSpace() . $controller; if(class_exists($controller)) { $controller_object = new $controller(); $method = $this->params['method']; if(is_callable([$controller_object , $method])) { $this->params['params'] = isset($this->params['params']) ? $this->params['params'] : []; echo call_user_func_array([$controller_object , $method] , $this->params['params']); } else { throw new \Exception("Method {$method} (in controller {$controller}) not found"); } } else { throw new \Exception("Controller class {$controller} not found",404); } } else { throw new \Exception("no route matched.",404); } } public function getRoutes() { return $this->routes; } public function getParams() { return $this->params; } protected function getNameSpace() { $namespace = $this->namespace; if(array_key_exists('namespace' , $this->params)) { $namespace .= $this->params['namespace'] . '\\'; } return $namespace; } protected function removeVariblesOfQueryString($url) { if($url != '') { $parts = explode("&" , $url , 2); if(strpos($parts[0] , '=') === false) { $url = $parts[0]; } else { $url = ''; } return $url; } } } $router = new Router(); $router->add('/news/{city}','cityController@index'); $router->dispatch('news/اراک');
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5V1Ne
function name:  (null)
number of ops:  11
compiled vars:  !0 = $router
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  114     0  E >   NEW                                              $1      'Router'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
  115     3        INIT_METHOD_CALL                                         !0, 'add'
          4        SEND_VAL_EX                                              '%2Fnews%2F%7Bcity%7D'
          5        SEND_VAL_EX                                              'cityController%40index'
          6        DO_FCALL                                      0          
  116     7        INIT_METHOD_CALL                                         !0, 'dispatch'
          8        SEND_VAL_EX                                              'news%2F%D8%A7%D8%B1%D8%A7%DA%A9'
          9        DO_FCALL                                      0          
         10      > RETURN                                                   1

Class Router:
Function add:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 36
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 56
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 56
Branch analysis from position: 36
filename:       /in/5V1Ne
function name:  add
number of ops:  60
compiled vars:  !0 = $route, !1 = $params, !2 = $AllParams
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   13     2        INIT_FCALL                                               'preg_replace'
          3        SEND_VAL                                                 '%2F%5E%5C%2F%2F'
          4        SEND_VAL                                                 ''
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $3      
          7        ASSIGN                                                   !0, $3
   15     8        INIT_FCALL                                               'preg_replace'
          9        SEND_VAL                                                 '%2F%5C%2F%2F'
         10        SEND_VAL                                                 '%5C%2F'
         11        SEND_VAR                                                 !0
         12        DO_ICALL                                         $5      
         13        ASSIGN                                                   !0, $5
   17    14        INIT_FCALL                                               'preg_replace'
         15        SEND_VAL                                                 '%2F%5C%7B%28%5Ba-z%5D%2B%29%5C%7D%2F'
         16        SEND_VAL                                                 '%3F%28%5Ba-z0-9%D8%A7-%DB%8C%5D%2B%29'
         17        SEND_VAR                                                 !0
         18        DO_ICALL                                         $7      
         19        ASSIGN                                                   !0, $7
   20    20        CONCAT                                           ~9      '%2F%5E', !0
         21        CONCAT                                           ~10     ~9, '%5C%2F%3F%24%2Fi'
         22        ASSIGN                                                   !0, ~10
   22    23        TYPE_CHECK                                   64          !1
         24      > JMPZ                                                     ~12, ->36
   23    25    >   INIT_FCALL                                               'explode'
         26        SEND_VAL                                                 '%40'
         27        SEND_VAR                                                 !1
         28        DO_ICALL                                         $13     
         29        FETCH_LIST_R                                     $14     $13, 0
         30        ASSIGN_DIM                                               !2, 'controller'
         31        OP_DATA                                                  $14
         32        FETCH_LIST_R                                     $16     $13, 1
         33        ASSIGN_DIM                                               !2, 'method'
         34        OP_DATA                                                  $16
         35        FREE                                                     $13
   26    36    >   TYPE_CHECK                                  128          !1
         37      > JMPZ                                                     ~18, ->56
   27    38    >   INIT_FCALL                                               'explode'
         39        SEND_VAL                                                 '%40'
         40        FETCH_DIM_R                                      ~19     !1, 'uses'
         41        SEND_VAL                                                 ~19
         42        DO_ICALL                                         $20     
         43        FETCH_LIST_R                                     $21     $20, 0
         44        ASSIGN_DIM                                               !2, 'controller'
         45        OP_DATA                                                  $21
         46        FETCH_LIST_R                                     $23     $20, 1
         47        ASSIGN_DIM                                               !2, 'method'
         48        OP_DATA                                                  $23
         49        FREE                                                     $20
   28    50        UNSET_DIM                                                !1, 'uses'
   29    51        INIT_FCALL                                               'array_merge'
         52        SEND_VAR                                                 !2
         53        SEND_VAR                                                 !1
         54        DO_ICALL                                         $25     
         55        ASSIGN                                                   !2, $25
   33    56    >   FETCH_OBJ_W                                      $27     'routes'
         57        ASSIGN_DIM                                               $27, !0
         58        OP_DATA                                                  !2
   34    59      > RETURN                                                   null

End of function add

Function match:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 26
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 26
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 25
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 20
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 19
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
filename:       /in/5V1Ne
function name:  match
number of ops:  29
compiled vars:  !0 = $url, !1 = $params, !2 = $route, !3 = $matches, !4 = $match, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   38     1        FETCH_OBJ_R                                      ~6      'routes'
          2      > FE_RESET_R                                       $7      ~6, ->26
          3    > > FE_FETCH_R                                       ~8      $7, !1, ->26
          4    >   ASSIGN                                                   !2, ~8
   39     5        INIT_FCALL                                               'preg_match'
          6        SEND_VAR                                                 !2
          7        SEND_VAR                                                 !0
          8        SEND_REF                                                 !3
          9        DO_ICALL                                         $10     
         10      > JMPZ                                                     $10, ->25
   40    11    > > FE_RESET_R                                       $11     !3, ->20
         12    > > FE_FETCH_R                                       ~12     $11, !4, ->20
         13    >   ASSIGN                                                   !5, ~12
   41    14        TYPE_CHECK                                   64          !5
         15      > JMPZ                                                     ~14, ->19
   42    16    >   FETCH_DIM_W                                      $15     !1, 'params'
         17        ASSIGN_DIM                                               $15, !5
         18        OP_DATA                                                  !4
   40    19    > > JMP                                                      ->12
         20    >   FE_FREE                                                  $11
   45    21        ASSIGN_OBJ                                               'params'
         22        OP_DATA                                                  !1
   46    23        FE_FREE                                                  $7
         24      > RETURN                                                   <true>
   38    25    > > JMP                                                      ->3
         26    >   FE_FREE                                                  $7
   49    27      > RETURN                                                   <false>
   50    28*     > RETURN                                                   null

End of function match

Function dispatch:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 73
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 64
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 54
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 40
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 72
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
Branch analysis from position: 54
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 64
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 73
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/5V1Ne
function name:  dispatch
number of ops:  79
compiled vars:  !0 = $url, !1 = $controller, !2 = $controller_object, !3 = $method
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   54     1        INIT_METHOD_CALL                                         'removeVariblesOfQueryString'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $4      
          4        ASSIGN                                                   !0, $4
   55     5        INIT_METHOD_CALL                                         'match'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0  $6      
          8      > JMPZ                                                     $6, ->73
   56     9    >   FETCH_OBJ_R                                      ~7      'params'
         10        FETCH_DIM_R                                      ~8      ~7, 'controller'
         11        ASSIGN                                                   !1, ~8
   57    12        INIT_METHOD_CALL                                         'getNameSpace'
         13        DO_FCALL                                      0  $10     
         14        CONCAT                                           ~11     $10, !1
         15        ASSIGN                                                   !1, ~11
   59    16        INIT_FCALL                                               'class_exists'
         17        SEND_VAR                                                 !1
         18        DO_ICALL                                         $13     
         19      > JMPZ                                                     $13, ->64
   60    20    >   FETCH_CLASS                                   0  $14     !1
         21        NEW                                              $15     $14
         22        DO_FCALL                                      0          
         23        ASSIGN                                                   !2, $15
   62    24        FETCH_OBJ_R                                      ~18     'params'
         25        FETCH_DIM_R                                      ~19     ~18, 'method'
         26        ASSIGN                                                   !3, ~19
   64    27        INIT_FCALL                                               'is_callable'
         28        INIT_ARRAY                                       ~21     !2
         29        ADD_ARRAY_ELEMENT                                ~21     !3
         30        SEND_VAL                                                 ~21
         31        DO_ICALL                                         $22     
         32      > JMPZ                                                     $22, ->54
   65    33    >   FETCH_OBJ_IS                                     ~25     'params'
         34        ISSET_ISEMPTY_DIM_OBJ                         0          ~25, 'params'
         35      > JMPZ                                                     ~26, ->40
         36    >   FETCH_OBJ_R                                      ~27     'params'
         37        FETCH_DIM_R                                      ~28     ~27, 'params'
         38        QM_ASSIGN                                        ~29     ~28
         39      > JMP                                                      ->41
         40    >   QM_ASSIGN                                        ~29     <array>
         41    >   FETCH_OBJ_W                                      $23     'params'
         42        ASSIGN_DIM                                               $23, 'params'
         43        OP_DATA                                                  ~29
   66    44        INIT_ARRAY                                       ~30     !2
         45        ADD_ARRAY_ELEMENT                                ~30     !3
         46        INIT_USER_CALL                                0          'call_user_func_array', ~30
         47        FETCH_OBJ_R                                      ~31     'params'
         48        FETCH_DIM_R                                      ~32     ~31, 'params'
         49        SEND_ARRAY                                               ~32
         50        CHECK_UNDEF_ARGS                                         
         51        DO_FCALL                                      0  $33     
         52        ECHO                                                     $33
         53      > JMP                                                      ->63
   69    54    >   NEW                                              $34     'Exception'
         55        ROPE_INIT                                     5  ~36     'Method+'
         56        ROPE_ADD                                      1  ~36     ~36, !3
         57        ROPE_ADD                                      2  ~36     ~36, '+%28in+controller+'
         58        ROPE_ADD                                      3  ~36     ~36, !1
         59        ROPE_END                                      4  ~35     ~36, '%29+not+found'
         60        SEND_VAL_EX                                              ~35
         61        DO_FCALL                                      0          
         62      > THROW                                         0          $34
         63    > > JMP                                                      ->72
   72    64    >   NEW                                              $40     'Exception'
         65        ROPE_INIT                                     3  ~42     'Controller+class+'
         66        ROPE_ADD                                      1  ~42     ~42, !1
         67        ROPE_END                                      2  ~41     ~42, '+not+found'
         68        SEND_VAL_EX                                              ~41
         69        SEND_VAL_EX                                              404
         70        DO_FCALL                                      0          
         71      > THROW                                         0          $40
         72    > > JMP                                                      ->78
   75    73    >   NEW                                              $45     'Exception'
         74        SEND_VAL_EX                                              'no+route+matched.'
         75        SEND_VAL_EX                                              404
         76        DO_FCALL                                      0          
         77      > THROW                                         0          $45
   77    78    > > RETURN                                                   null

End of function dispatch

Function getroutes:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5V1Ne
function name:  getRoutes
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   FETCH_OBJ_R                                      ~0      'routes'
          1      > RETURN                                                   ~0
   81     2*     > RETURN                                                   null

End of function getroutes

Function getparams:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5V1Ne
function name:  getParams
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   FETCH_OBJ_R                                      ~0      'params'
          1      > RETURN                                                   ~0
   86     2*     > RETURN                                                   null

End of function getparams

Function getnamespace:
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 = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/5V1Ne
function name:  getNameSpace
number of ops:  11
compiled vars:  !0 = $namespace
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   FETCH_OBJ_R                                      ~1      'namespace'
          1        ASSIGN                                                   !0, ~1
   92     2        FETCH_OBJ_R                                      ~3      'params'
          3        ARRAY_KEY_EXISTS                                         'namespace', ~3
          4      > JMPZ                                                     ~4, ->9
   93     5    >   FETCH_OBJ_R                                      ~5      'params'
          6        FETCH_DIM_R                                      ~6      ~5, 'namespace'
          7        CONCAT                                           ~7      ~6, '%5C'
          8        ASSIGN_OP                                     8          !0, ~7
   96     9    > > RETURN                                                   !0
   97    10*     > RETURN                                                   null

End of function getnamespace

Function removevariblesofquerystring:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 21
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5V1Ne
function name:  removeVariblesOfQueryString
number of ops:  22
compiled vars:  !0 = $url, !1 = $parts
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   99     0  E >   RECV                                             !0      
  101     1        IS_NOT_EQUAL                                             !0, ''
          2      > JMPZ                                                     ~2, ->21
  102     3    >   INIT_FCALL                                               'explode'
          4        SEND_VAL                                                 '%26'
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 2
          7        DO_ICALL                                         $3      
          8        ASSIGN                                                   !1, $3
  103     9        INIT_FCALL                                               'strpos'
         10        FETCH_DIM_R                                      ~5      !1, 0
         11        SEND_VAL                                                 ~5
         12        SEND_VAL                                                 '%3D'
         13        DO_ICALL                                         $6      
         14        TYPE_CHECK                                    4          $6
         15      > JMPZ                                                     ~7, ->19
  104    16    >   FETCH_DIM_R                                      ~8      !1, 0
         17        ASSIGN                                                   !0, ~8
         18      > JMP                                                      ->20
  106    19    >   ASSIGN                                                   !0, ''
  108    20    > > RETURN                                                   !0
  110    21    > > RETURN                                                   null

End of function removevariblesofquerystring

End of class Router.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
209.13 ms | 1420 KiB | 27 Q