3v4l.org

run code in 500+ PHP versions simultaneously
<?php class RouteCollector { private array $routes; /* [path => [httpMethod => callback]] [ '/' => [ ['GET' => fn()], ['POST' => fn()] ] ] */ public function add(string $httpMethod, string $path, string|callable $callback): void { $this->routes[$path][$httpMethod] = $callback; } public function getRoutes(): array { return $this->routes; } } class RouteDispatcher { private string | \Closure $callback; # 404 Not Found public private(set) bool $isRouteFound = false; # 405 Method Not Allowed public private(set) bool $isMethodAllowed = false; public private(set) array $allowedMethods = []; public function __construct( private array $routes ){ } public function dispatch(string $httpMethod, string $uri): void { $result = $this->match($uri); # Found route verification if (!$result) { return; } $this->isRouteFound = true; # Method verification if (array_key_exists($httpMethod, $this->routes[$result])) { $this->isMethodAllowed = true; $this->callback = $this->routes[$result][$httpMethod]; } else { $this->allowedMethods = array_keys($this->routes[$result]); } } public function match(string $uri): ?string { $match = array_filter( $this->routes, fn($path): bool => $path === $uri, ARRAY_FILTER_USE_KEY ); return key($match); } public function getCallback(): string | \Closure { return $this->callback; } } /****************************************************************/ $router = new RouteCollector(); $router->add('GET', '/user/login', fn() => 'Form!'); $router->add('POST', '/user/login', fn() => 'Processing!'); $router->add('GET', '/hello', function () { return 'Hello ' . htmlspecialchars($_GET['user'] ?? '') .'!'; }); $router->add('POST', '/admin', fn() => 'Hello Admin!'); # 405 route $router->add('GET', '/', fn() => 'Hello World!'); $dispatcher = new RouteDispatcher($router->getRoutes()); $dispatcher->dispatch( 'PUT', '/user/login' ); echo match (true) { ! $dispatcher->isRouteFound => '404 Sorry, nothing here.', ! $dispatcher->isMethodAllowed => '405 Allowed is: ' . implode(', ', $dispatcher->allowedMethods), default => $dispatcher->getCallback()() };
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 52
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 51, Position 2 = 54
Branch analysis from position: 51
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
filename:       /in/dZNFB
function name:  (null)
number of ops:  67
compiled vars:  !0 = $router, !1 = $dispatcher
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   80     0  E >   NEW                                                  $2      'RouteCollector'
          1        DO_FCALL                                          0          
          2        ASSIGN                                                       !0, $2
   82     3        INIT_METHOD_CALL                                             !0, 'add'
          4        SEND_VAL_EX                                                  'GET'
          5        SEND_VAL_EX                                                  '%2Fuser%2Flogin'
          6        DECLARE_LAMBDA_FUNCTION                              ~5      [0]
          7        SEND_VAL_EX                                                  ~5
          8        DO_FCALL                                          0          
   84     9        INIT_METHOD_CALL                                             !0, 'add'
         10        SEND_VAL_EX                                                  'POST'
         11        SEND_VAL_EX                                                  '%2Fuser%2Flogin'
         12        DECLARE_LAMBDA_FUNCTION                              ~7      [1]
         13        SEND_VAL_EX                                                  ~7
         14        DO_FCALL                                          0          
   86    15        INIT_METHOD_CALL                                             !0, 'add'
         16        SEND_VAL_EX                                                  'GET'
         17        SEND_VAL_EX                                                  '%2Fhello'
         18        DECLARE_LAMBDA_FUNCTION                              ~9      [2]
   88    19        SEND_VAL_EX                                                  ~9
   86    20        DO_FCALL                                          0          
   90    21        INIT_METHOD_CALL                                             !0, 'add'
         22        SEND_VAL_EX                                                  'POST'
         23        SEND_VAL_EX                                                  '%2Fadmin'
         24        DECLARE_LAMBDA_FUNCTION                              ~11     [3]
         25        SEND_VAL_EX                                                  ~11
         26        DO_FCALL                                          0          
   92    27        INIT_METHOD_CALL                                             !0, 'add'
         28        SEND_VAL_EX                                                  'GET'
         29        SEND_VAL_EX                                                  '%2F'
         30        DECLARE_LAMBDA_FUNCTION                              ~13     [4]
         31        SEND_VAL_EX                                                  ~13
         32        DO_FCALL                                          0          
   94    33        NEW                                                  $15     'RouteDispatcher'
         34        INIT_METHOD_CALL                                             !0, 'getRoutes'
         35        DO_FCALL                                          0  $16     
         36        SEND_VAR_NO_REF_EX                                           $16
         37        DO_FCALL                                          0          
         38        ASSIGN                                                       !1, $15
   95    39        INIT_METHOD_CALL                                             !1, 'dispatch'
   96    40        SEND_VAL_EX                                                  'PUT'
   97    41        SEND_VAL_EX                                                  '%2Fuser%2Flogin'
   95    42        DO_FCALL                                          0          
  101    43        FETCH_OBJ_R                                          ~21     !1, 'isRouteFound'
         44        BOOL_NOT                                             ~22     ~21
         45        IS_IDENTICAL                                                 ~22, <true>
         46      > JMPNZ                                                        ~20, ->52
  103    47    >   FETCH_OBJ_R                                          ~23     !1, 'isMethodAllowed'
         48        BOOL_NOT                                             ~24     ~23
         49        IS_IDENTICAL                                                 ~24, <true>
         50      > JMPNZ                                                        ~20, ->54
         51    > > JMP                                                          ->59
  102    52    >   QM_ASSIGN                                            ~25     '404+Sorry%2C+nothing+here.'
         53      > JMP                                                          ->65
  104    54    >   FETCH_OBJ_R                                          ~26     !1, 'allowedMethods'
         55        FRAMELESS_ICALL_2                implode             ~27     '%2C+', ~26
         56        CONCAT                                               ~28     '405+Allowed+is%3A+', ~27
         57        QM_ASSIGN                                            ~25     ~28
         58      > JMP                                                          ->65
  106    59    >   INIT_METHOD_CALL                                             !1, 'getCallback'
         60        DO_FCALL                                          0  $29     
         61        INIT_DYNAMIC_CALL                                            $29
         62        DO_FCALL                                          0  $30     
         63        QM_ASSIGN                                            ~25     $30
         64      > JMP                                                          ->65
         65    >   ECHO                                                         ~25
  107    66      > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dZNFB
function name:  {closure:/in/dZNFB:82}
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   82     0  E > > RETURN                                                       'Form%21'
          1*     > RETURN                                                       null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dZNFB
function name:  {closure:/in/dZNFB:84}
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   84     0  E > > RETURN                                                       'Processing%21'
          1*     > RETURN                                                       null

End of Dynamic Function 1

Dynamic Function 2
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dZNFB
function name:  {closure:/in/dZNFB:86}
number of ops:  11
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   87     0  E >   INIT_FCALL                                                   'htmlspecialchars'
          1        FETCH_IS                                             ~0      '_GET'
          2        FETCH_DIM_IS                                         ~1      ~0, 'user'
          3        COALESCE                                             ~2      ~1
          4        QM_ASSIGN                                            ~2      ''
          5        SEND_VAL                                                     ~2
          6        DO_ICALL                                             $3      
          7        CONCAT                                               ~4      'Hello+', $3
          8        CONCAT                                               ~5      ~4, '%21'
          9      > RETURN                                                       ~5
   88    10*     > RETURN                                                       null

End of Dynamic Function 2

Dynamic Function 3
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dZNFB
function name:  {closure:/in/dZNFB:90}
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   90     0  E > > RETURN                                                       'Hello+Admin%21'
          1*     > RETURN                                                       null

End of Dynamic Function 3

Dynamic Function 4
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dZNFB
function name:  {closure:/in/dZNFB:92}
number of ops:  2
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   92     0  E > > RETURN                                                       'Hello+World%21'
          1*     > RETURN                                                       null

End of Dynamic Function 4

Class RouteCollector:
Function add:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dZNFB
function name:  add
number of ops:  8
compiled vars:  !0 = $httpMethod, !1 = $path, !2 = $callback
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   17     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV                                                 !2      
   19     3        FETCH_OBJ_W                                          $3      'routes'
          4        FETCH_DIM_W                                          $4      $3, !1
          5        ASSIGN_DIM                                                   $4, !0
          6        OP_DATA                                                      !2
   20     7      > RETURN                                                       null

End of function add

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

End of function getroutes

End of class RouteCollector.

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

End of function __construct

Function dispatch:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 23
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dZNFB
function name:  dispatch
number of ops:  31
compiled vars:  !0 = $httpMethod, !1 = $uri, !2 = $result
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   43     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   45     2        INIT_METHOD_CALL                                             'match'
          3        SEND_VAR_EX                                                  !1
          4        DO_FCALL                                          0  $3      
          5        ASSIGN                                                       !2, $3
   48     6        BOOL_NOT                                             ~5      !2
          7      > JMPZ                                                         ~5, ->9
   49     8    > > RETURN                                                       null
   51     9    >   ASSIGN_OBJ                                                   'isRouteFound'
         10        OP_DATA                                                      <true>
   54    11        FETCH_OBJ_R                                          ~7      'routes'
         12        FETCH_DIM_R                                          ~8      ~7, !2
         13        ARRAY_KEY_EXISTS                                             !0, ~8
         14      > JMPZ                                                         ~9, ->23
   55    15    >   ASSIGN_OBJ                                                   'isMethodAllowed'
         16        OP_DATA                                                      <true>
   56    17        FETCH_OBJ_R                                          ~12     'routes'
         18        FETCH_DIM_R                                          ~13     ~12, !2
         19        FETCH_DIM_R                                          ~14     ~13, !0
         20        ASSIGN_OBJ                                                   'callback'
         21        OP_DATA                                                      ~14
   54    22      > JMP                                                          ->30
   58    23    >   INIT_FCALL                                                   'array_keys'
         24        FETCH_OBJ_R                                          ~16     'routes'
         25        FETCH_DIM_R                                          ~17     ~16, !2
         26        SEND_VAL                                                     ~17
         27        DO_ICALL                                             $18     
         28        ASSIGN_OBJ                                                   'allowedMethods'
         29        OP_DATA                                                      $18
   60    30    > > RETURN                                                       null

End of function dispatch

Function match:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dZNFB
function name:  match
number of ops:  17
compiled vars:  !0 = $uri, !1 = $match
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   62     0  E >   RECV                                                 !0      
   64     1        INIT_FCALL                                                   'array_filter'
   65     2        FETCH_OBJ_R                                          ~2      'routes'
          3        SEND_VAL                                                     ~2
   66     4        DECLARE_LAMBDA_FUNCTION                              ~3      [0]
          5        BIND_LEXICAL                                                 ~3, !0
          6        SEND_VAL                                                     ~3
   67     7        SEND_VAL                                                     2
   64     8        DO_ICALL                                             $4      
          9        ASSIGN                                                       !1, $4
   69    10        INIT_FCALL                                                   'key'
         11        SEND_VAR                                                     !1
         12        DO_ICALL                                             $6      
         13        VERIFY_RETURN_TYPE                                           $6
         14      > RETURN                                                       $6
   70    15*       VERIFY_RETURN_TYPE                                           
         16*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dZNFB
function name:  {closure:RouteDispatcher::match():66}
number of ops:  7
compiled vars:  !0 = $path, !1 = $uri
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   66     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
          2        IS_IDENTICAL                                         ~2      !0, !1
          3        VERIFY_RETURN_TYPE                                           ~2
          4      > RETURN                                                       ~2
          5*       VERIFY_RETURN_TYPE                                           
          6*     > RETURN                                                       null

End of Dynamic Function 0

End of function match

Function getcallback:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dZNFB
function name:  getCallback
number of ops:  5
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   74     0  E >   FETCH_OBJ_R                                          ~0      'callback'
          1        VERIFY_RETURN_TYPE                                           ~0
          2      > RETURN                                                       ~0
   75     3*       VERIFY_RETURN_TYPE                                           
          4*     > RETURN                                                       null

End of function getcallback

End of class RouteDispatcher.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
158.04 ms | 570 KiB | 12 Q