3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Change me! $userSpecifiedPath = '/'; // works, HomepageController // $userSpecifiedPath = '/asdf'; // route doesn't exist, 404Controller //$userSpecifiedPath = '/deep/nested/controller'; // // Don't edit below =============================================== // ================================================================ // Simple config example // ================================================================ $routerConfig = <<<CONFIG [/] Controller = "HomepageController" [/deep*] Controller = "DeepController" [404] Controller = "404Controller" CONFIG; // ================================================================ // Simple router example // ================================================================ class Router { private $routes; public function __construct($routes) { $this->routes = $routes; } public function getRouteFromPath($path) { if(array_key_exists($path, $this->routes) === true) { return $this->routes[$path]; } $hierarchical = $this->getHierarchicalRouteFromPath($path); if(!empty($hierarchical)) { return $hierarchical; } elseif(array_key_exists('404', $this->routes) === true) { return $this->routes['404']; } throw new Exception('No controllers found.'); } public function getAllHierarchicalRoutes() { $routes = array(); foreach($this->routes as $key => $route) { if(strpos($key, '*') === strlen($key) - 1) { $routes[$key] = $route; } } return $routes; } public function getHierarchicalRouteFromPath($path) { foreach($this->routes as $key => $route) { if(strpos($key, '*') === strlen($key) - 1) { if(strpos($path, rtrim($key, '*')) === 0) { return $route; } } } } } // ================================================================ // Sample output // ================================================================ $routes = parse_ini_string($routerConfig, true); $Router = new Router($routes); $selectedRoute = $Router->getRouteFromPath($userSpecifiedPath); echo 'Using controller: ' . $selectedRoute['Controller'];
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DC5Ik
function name:  (null)
number of ops:  19
compiled vars:  !0 = $userSpecifiedPath, !1 = $routerConfig, !2 = $routes, !3 = $Router, !4 = $selectedRoute
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   ASSIGN                                                   !0, '%2F'
   18     1        ASSIGN                                                   !1, '%5B%2F%5D%0AController+%3D+%22HomepageController%22%0A%0A%5B%2Fdeep%2A%5D%0AController+%3D+%22DeepController%22%0A%0A%5B404%5D%0AController+%3D+%22404Controller%22'
   82     2        INIT_FCALL                                               'parse_ini_string'
          3        SEND_VAR                                                 !1
          4        SEND_VAL                                                 <true>
          5        DO_ICALL                                         $7      
          6        ASSIGN                                                   !2, $7
   83     7        NEW                                              $9      'Router'
          8        SEND_VAR_EX                                              !2
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !3, $9
   84    11        INIT_METHOD_CALL                                         !3, 'getRouteFromPath'
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0  $12     
         14        ASSIGN                                                   !4, $12
   85    15        FETCH_DIM_R                                      ~14     !4, 'Controller'
         16        CONCAT                                           ~15     'Using+controller%3A+', ~14
         17        ECHO                                                     ~15
         18      > RETURN                                                   1

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

End of function __construct

Function getroutefrompath:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 24
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/DC5Ik
function name:  getRouteFromPath
number of ops:  29
compiled vars:  !0 = $path, !1 = $hierarchical
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   43     1        FETCH_OBJ_R                                      ~2      'routes'
          2        ARRAY_KEY_EXISTS                                 ~3      !0, ~2
          3        TYPE_CHECK                                    8          ~3
          4      > JMPZ                                                     ~4, ->8
   44     5    >   FETCH_OBJ_R                                      ~5      'routes'
          6        FETCH_DIM_R                                      ~6      ~5, !0
          7      > RETURN                                                   ~6
   46     8    >   INIT_METHOD_CALL                                         'getHierarchicalRouteFromPath'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0  $7      
         11        ASSIGN                                                   !1, $7
   47    12        ISSET_ISEMPTY_CV                                 ~9      !1
         13        BOOL_NOT                                         ~10     ~9
         14      > JMPZ                                                     ~10, ->17
   48    15    > > RETURN                                                   !1
         16*       JMP                                                      ->24
   49    17    >   FETCH_OBJ_R                                      ~11     'routes'
         18        ARRAY_KEY_EXISTS                                 ~12     '404', ~11
         19        TYPE_CHECK                                    8          ~12
         20      > JMPZ                                                     ~13, ->24
   50    21    >   FETCH_OBJ_R                                      ~14     'routes'
         22        FETCH_DIM_R                                      ~15     ~14, 404
         23      > RETURN                                                   ~15
   52    24    >   NEW                                              $16     'Exception'
         25        SEND_VAL_EX                                              'No+controllers+found.'
         26        DO_FCALL                                      0          
         27      > THROW                                         0          $16
   53    28*     > RETURN                                                   null

End of function getroutefrompath

Function getallhierarchicalroutes:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 16
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 16
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 15
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/DC5Ik
function name:  getAllHierarchicalRoutes
number of ops:  19
compiled vars:  !0 = $routes, !1 = $route, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   ASSIGN                                                   !0, <array>
   57     1        FETCH_OBJ_R                                      ~4      'routes'
          2      > FE_RESET_R                                       $5      ~4, ->16
          3    > > FE_FETCH_R                                       ~6      $5, !1, ->16
          4    >   ASSIGN                                                   !2, ~6
   58     5        INIT_FCALL                                               'strpos'
          6        SEND_VAR                                                 !2
          7        SEND_VAL                                                 '%2A'
          8        DO_ICALL                                         $8      
          9        STRLEN                                           ~9      !2
         10        SUB                                              ~10     ~9, 1
         11        IS_IDENTICAL                                             $8, ~10
         12      > JMPZ                                                     ~11, ->15
   59    13    >   ASSIGN_DIM                                               !0, !2
         14        OP_DATA                                                  !1
   57    15    > > JMP                                                      ->3
         16    >   FE_FREE                                                  $5
   62    17      > RETURN                                                   !0
   63    18*     > RETURN                                                   null

End of function getallhierarchicalroutes

Function gethierarchicalroutefrompath:
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 = 13, Position 2 = 25
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 25
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 25
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
filename:       /in/DC5Ik
function name:  getHierarchicalRouteFromPath
number of ops:  28
compiled vars:  !0 = $path, !1 = $route, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
   66     1        FETCH_OBJ_R                                      ~3      'routes'
          2      > FE_RESET_R                                       $4      ~3, ->26
          3    > > FE_FETCH_R                                       ~5      $4, !1, ->26
          4    >   ASSIGN                                                   !2, ~5
   67     5        INIT_FCALL                                               'strpos'
          6        SEND_VAR                                                 !2
          7        SEND_VAL                                                 '%2A'
          8        DO_ICALL                                         $7      
          9        STRLEN                                           ~8      !2
         10        SUB                                              ~9      ~8, 1
         11        IS_IDENTICAL                                             $7, ~9
         12      > JMPZ                                                     ~10, ->25
   68    13    >   INIT_FCALL                                               'strpos'
         14        SEND_VAR                                                 !0
         15        INIT_FCALL                                               'rtrim'
         16        SEND_VAR                                                 !2
         17        SEND_VAL                                                 '%2A'
         18        DO_ICALL                                         $11     
         19        SEND_VAR                                                 $11
         20        DO_ICALL                                         $12     
         21        IS_IDENTICAL                                             $12, 0
         22      > JMPZ                                                     ~13, ->25
   69    23    >   FE_FREE                                                  $4
         24      > RETURN                                                   !1
   66    25    > > JMP                                                      ->3
         26    >   FE_FREE                                                  $4
   73    27      > RETURN                                                   null

End of function gethierarchicalroutefrompath

End of class Router.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.78 ms | 1408 KiB | 19 Q