3v4l.org

run code in 500+ 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 = 16, Position 2 = 27
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 47
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
Branch analysis from position: 27
filename:       /in/5V1Ne
function name:  add
number of ops:  51
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        FRAMELESS_ICALL_3                preg_replace        ~3      '%2F%5E%5C%2F%2F', ''
          3        OP_DATA                                                      !0
          4        ASSIGN                                                       !0, ~3
   15     5        FRAMELESS_ICALL_3                preg_replace        ~5      '%2F%5C%2F%2F', '%5C%2F'
          6        OP_DATA                                                      !0
          7        ASSIGN                                                       !0, ~5
   17     8        FRAMELESS_ICALL_3                preg_replace        ~7      '%2F%5C%7B%28%5Ba-z%5D%2B%29%5C%7D%2F', '%3F%28%5Ba-z0-9%D8%A7-%DB%8C%5D%2B%29'
          9        OP_DATA                                                      !0
         10        ASSIGN                                                       !0, ~7
   20    11        CONCAT                                               ~9      '%2F%5E', !0
         12        CONCAT                                               ~10     ~9, '%5C%2F%3F%24%2Fi'
         13        ASSIGN                                                       !0, ~10
   22    14        TYPE_CHECK                                       64          !1
         15      > JMPZ                                                         ~12, ->27
   23    16    >   INIT_FCALL                                                   'explode'
         17        SEND_VAL                                                     '%40'
         18        SEND_VAR                                                     !1
         19        DO_ICALL                                             $13     
         20        FETCH_LIST_R                                         $14     $13, 0
         21        ASSIGN_DIM                                                   !2, 'controller'
         22        OP_DATA                                                      $14
         23        FETCH_LIST_R                                         $16     $13, 1
         24        ASSIGN_DIM                                                   !2, 'method'
         25        OP_DATA                                                      $16
         26        FREE                                                         $13
   26    27    >   TYPE_CHECK                                      128          !1
         28      > JMPZ                                                         ~18, ->47
   27    29    >   INIT_FCALL                                                   'explode'
         30        SEND_VAL                                                     '%40'
         31        FETCH_DIM_R                                          ~19     !1, 'uses'
         32        SEND_VAL                                                     ~19
         33        DO_ICALL                                             $20     
         34        FETCH_LIST_R                                         $21     $20, 0
         35        ASSIGN_DIM                                                   !2, 'controller'
         36        OP_DATA                                                      $21
         37        FETCH_LIST_R                                         $23     $20, 1
         38        ASSIGN_DIM                                                   !2, 'method'
         39        OP_DATA                                                      $23
         40        FREE                                                         $20
   28    41        UNSET_DIM                                                    !1, 'uses'
   29    42        INIT_FCALL                                                   'array_merge'
         43        SEND_VAR                                                     !2
         44        SEND_VAR                                                     !1
         45        DO_ICALL                                             $25     
         46        ASSIGN                                                       !2, $25
   33    47    >   FETCH_OBJ_W                                          $27     'routes'
         48        ASSIGN_DIM                                                   $27, !0
         49        OP_DATA                                                      !2
   34    50      > 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 = 71
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 62
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 52
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 38
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
1 jumps found. (Code = 42) Position 1 = 76
Branch analysis from position: 76
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
Branch analysis from position: 52
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 62
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 71
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/5V1Ne
function name:  dispatch
number of ops:  77
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, ->71
   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        FRAMELESS_ICALL_1                class_exists        ~13     !1
         17      > JMPZ                                                         ~13, ->62
   60    18    >   FETCH_CLASS                                       0  $14     !1
         19        NEW                                                  $15     $14
         20        DO_FCALL                                          0          
         21        ASSIGN                                                       !2, $15
   62    22        FETCH_OBJ_R                                          ~18     'params'
         23        FETCH_DIM_R                                          ~19     ~18, 'method'
         24        ASSIGN                                                       !3, ~19
   64    25        INIT_FCALL                                                   'is_callable'
         26        INIT_ARRAY                                           ~21     !2
         27        ADD_ARRAY_ELEMENT                                    ~21     !3
         28        SEND_VAL                                                     ~21
         29        DO_ICALL                                             $22     
         30      > JMPZ                                                         $22, ->52
   65    31    >   FETCH_OBJ_IS                                         ~25     'params'
         32        ISSET_ISEMPTY_DIM_OBJ                             0          ~25, 'params'
         33      > JMPZ                                                         ~26, ->38
         34    >   FETCH_OBJ_R                                          ~27     'params'
         35        FETCH_DIM_R                                          ~28     ~27, 'params'
         36        QM_ASSIGN                                            ~29     ~28
         37      > JMP                                                          ->39
         38    >   QM_ASSIGN                                            ~29     <array>
         39    >   FETCH_OBJ_W                                          $23     'params'
         40        ASSIGN_DIM                                                   $23, 'params'
         41        OP_DATA                                                      ~29
   66    42        INIT_ARRAY                                           ~30     !2
         43        ADD_ARRAY_ELEMENT                                    ~30     !3
         44        INIT_USER_CALL                                    0          'call_user_func_array', ~30
         45        FETCH_OBJ_R                                          ~31     'params'
         46        FETCH_DIM_R                                          ~32     ~31, 'params'
         47        SEND_ARRAY                                                   ~32
         48        CHECK_UNDEF_ARGS                                             
         49        DO_FCALL                                          1  $33     
         50        ECHO                                                         $33
   64    51      > JMP                                                          ->61
   69    52    >   NEW                                                  $34     'Exception'
         53        ROPE_INIT                                         5  ~36     'Method+'
         54        ROPE_ADD                                          1  ~36     ~36, !3
         55        ROPE_ADD                                          2  ~36     ~36, '+%28in+controller+'
         56        ROPE_ADD                                          3  ~36     ~36, !1
         57        ROPE_END                                          4  ~35     ~36, '%29+not+found'
         58        SEND_VAL_EX                                                  ~35
         59        DO_FCALL                                          0          
         60      > THROW                                             0          $34
   59    61    > > JMP                                                          ->70
   72    62    >   NEW                                                  $40     'Exception'
         63        ROPE_INIT                                         3  ~42     'Controller+class+'
         64        ROPE_ADD                                          1  ~42     ~42, !1
         65        ROPE_END                                          2  ~41     ~42, '+not+found'
         66        SEND_VAL_EX                                                  ~41
         67        SEND_VAL_EX                                                  404
         68        DO_FCALL                                          0          
         69      > THROW                                             0          $40
   55    70    > > JMP                                                          ->76
   75    71    >   NEW                                                  $45     'Exception'
         72        SEND_VAL_EX                                                  'no+route+matched.'
         73        SEND_VAL_EX                                                  404
         74        DO_FCALL                                          0          
         75      > THROW                                             0          $45
   77    76    > > 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 = 18
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 16
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5V1Ne
function name:  removeVariblesOfQueryString
number of ops:  19
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, ->18
  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        FETCH_DIM_R                                          ~5      !1, 0
         10        FRAMELESS_ICALL_2                strpos              ~6      ~5, '%3D'
         11        TYPE_CHECK                                        4          ~6
         12      > JMPZ                                                         ~7, ->16
  104    13    >   FETCH_DIM_R                                          ~8      !1, 0
         14        ASSIGN                                                       !0, ~8
  103    15      > JMP                                                          ->17
  106    16    >   ASSIGN                                                       !0, ''
  108    17    > > RETURN                                                       !0
  110    18    > > RETURN                                                       null

End of function removevariblesofquerystring

End of class Router.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
162.03 ms | 3463 KiB | 23 Q