3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CommandFactory { private $commandMappings; public function __construct(string $mappingFilename = 'default.json') { //$mappingsJSON = file_get_content($mappingFilename); $this->commandMappings = json_decode("{ 'command' : { 'SYSTEMS' : { 'OS1' : { 'Version' : 'command string', 'Version2' : 'command string', }, 'OS2' : { 'Version' : 'command string', 'Version' : 'command string' }, 'COMMANDOPTIONS' : ['var1', 'var2', 'var3'], }, 'add-Firewall' : { 'SYSTEMS' : { 'Ubuntu' : { '14' : 'command string', '11' : 'command string', }, 'Redhat' : { '13' : 'command string', '11' : 'command string' }, 'COMMANDOPTIONS' : ['port',], }, }", true); } public function getCommand(string $command, array $options): Command { $commandArray = $this->commandMappings[$command]; return new Command($commandArray, $options); } } class Command { private $options; private $mapping; public function __construct($mappings, $options) { $this->$options = $options; $this->mapping = $mappings; // check if options are are valid // potentially map in options here? } public function view(string $os, $version): string { // validate $os and $version $command = $this->mapping['SYSTEMS'][$os][$version]; //$command = str_replace(array_keys($options), $options, $command); return $command; } } // usage $factory = new CommandFactory(); $firewallCommand = $factory->getCommand('add-Firewall', ['port' => 23]); echo $firewallCommand->view('Ubuntu', '11');
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlPGr
function name:  (null)
number of ops:  14
compiled vars:  !0 = $factory, !1 = $firewallCommand
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   NEW                                              $2      'CommandFactory'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   74     3        INIT_METHOD_CALL                                         !0, 'getCommand'
          4        SEND_VAL_EX                                              'add-Firewall'
          5        SEND_VAL_EX                                              <array>
          6        DO_FCALL                                      0  $5      
          7        ASSIGN                                                   !1, $5
   75     8        INIT_METHOD_CALL                                         !1, 'view'
          9        SEND_VAL_EX                                              'Ubuntu'
         10        SEND_VAL_EX                                              '11'
         11        DO_FCALL                                      0  $7      
         12        ECHO                                                     $7
         13      > RETURN                                                   1

Class CommandFactory:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlPGr
function name:  __construct
number of ops:  8
compiled vars:  !0 = $mappingFilename
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV_INIT                                        !0      'default.json'
   11     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAL                                                 '%7B%0A++++%27command%27+%3A+%7B%0A++++++++%27SYSTEMS%27+%3A+%7B%0A++++++++++++%27OS1%27+%3A+%7B%0A++++++++++++++++%27Version%27+%3A+%27command+string%27%2C%0A++++++++++++++++%27Version2%27+%3A+%27command+string%27%2C%0A++++++++++++%7D%2C%0A++++++++++++%27OS2%27+%3A+%7B%0A++++++++++++++++%27Version%27+%3A+%27command+string%27%2C%0A++++++++++++++++%27Version%27+%3A+%27command+string%27%0A++++++++++++%7D%2C%0A++++++++%27COMMANDOPTIONS%27+%3A+%5B%27var1%27%2C+%27var2%27%2C+%27var3%27%5D%2C%0A++++%7D%2C%0A++++%27add-Firewall%27+%3A+%7B%0A++++++++++++%27SYSTEMS%27+%3A+%7B%0A++++++++++++++++%27Ubuntu%27+%3A+%7B%0A++++++++++++++++++++%2714%27+%3A+%27command+string%27%2C%0A++++++++++++++++%2711%27+%3A+%27command+string%27%2C%0A++++++++++++%7D%2C%0A++++++++++++%27Redhat%27+%3A+%7B%0A++++++++++++++++++++%2713%27+%3A+%27command+string%27%2C%0A++++++++++++++++%2711%27+%3A+%27command+string%27%0A++++++++++++%7D%2C%0A++++++++%27COMMANDOPTIONS%27+%3A+%5B%27port%27%2C%5D%2C%0A++++%7D%2C%0A%7D'
   36     3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $2      
   11     5        ASSIGN_OBJ                                               'commandMappings'
   36     6        OP_DATA                                                  $2
   37     7      > RETURN                                                   null

End of function __construct

Function getcommand:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlPGr
function name:  getCommand
number of ops:  13
compiled vars:  !0 = $command, !1 = $options, !2 = $commandArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   41     2        FETCH_OBJ_R                                      ~3      'commandMappings'
          3        FETCH_DIM_R                                      ~4      ~3, !0
          4        ASSIGN                                                   !2, ~4
   43     5        NEW                                              $6      'Command'
          6        SEND_VAR_EX                                              !2
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0          
          9        VERIFY_RETURN_TYPE                                       $6
         10      > RETURN                                                   $6
   44    11*       VERIFY_RETURN_TYPE                                       
         12*     > RETURN                                                   null

End of function getcommand

End of class CommandFactory.

Class Command:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlPGr
function name:  __construct
number of ops:  7
compiled vars:  !0 = $mappings, !1 = $options
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   54     2        ASSIGN_OBJ                                               !1
          3        OP_DATA                                                  !1
   55     4        ASSIGN_OBJ                                               'mapping'
          5        OP_DATA                                                  !0
   58     6      > RETURN                                                   null

End of function __construct

Function view:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlPGr
function name:  view
number of ops:  11
compiled vars:  !0 = $os, !1 = $version, !2 = $command
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   64     2        FETCH_OBJ_R                                      ~3      'mapping'
          3        FETCH_DIM_R                                      ~4      ~3, 'SYSTEMS'
          4        FETCH_DIM_R                                      ~5      ~4, !0
          5        FETCH_DIM_R                                      ~6      ~5, !1
          6        ASSIGN                                                   !2, ~6
   68     7        VERIFY_RETURN_TYPE                                       !2
          8      > RETURN                                                   !2
   69     9*       VERIFY_RETURN_TYPE                                       
         10*     > RETURN                                                   null

End of function view

End of class Command.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.08 ms | 1392 KiB | 15 Q