3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ServiceContainer { protected $services = []; public function get($id, callable $creator) { // Нет в кэше? Берём из creator'а. todo: Сделать защиту от цикличных зависимостей if (! isset($this->services[$id])) $this->services[$id] = $creator(); // Возвращаем кэш return $this->services[$id]; } public function set($id, $service) { $this->services[$id] = $service; } } class Framework { private static $services = null; public static function getServices() { if (is_null(self::$services)) { throw new \Exception('Не инициализировано'); } return self::$services; } public static function init(ServiceContainer $services = null) // Можно в качестве аргумента передать нужную реализацию ServiceContainer { if ( $services ) self::$services = $services; else self::$services = new ServiceContainer(); } public static function run() { // Маршрутизация => Контроллер => Выполнение Response } public static function getRouter() { return static::getServices()->get('framework.router', function() { return "router"; }); } } class CMS extends Framework { private static $services = null; public static function getServices() { if (is_null(self::$services)) { throw new \Exception('Не инициализировано (говорит CMS)'); } return self::$services; } public static function run() { // Маршрутизация => Встроенные контроллеры/Модули => Выполнение Response } public static function getModulesManager() { return static::getServices()->get('cms.modulesmanager', function() { return "modulesManager"; }); } } // based on framework Framework::init() && Framework::run(); // где-то в компоненте: var_dump(Framework::getRouter()); // based on CMS CMS::init() && CMS::run(); // где-то в модуле: var_dump(CMS::getRouter()); var_dump(CMS::getModulesManager()); CMS::getServices()->set('framework.router', 'Ha, its not router!'); var_dump(CMS::getRouter());
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
2 jumps found. (Code = 46) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
Branch analysis from position: 6
filename:       /in/5Bf0X
function name:  (null)
number of ops:  39
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   INIT_STATIC_METHOD_CALL                                  'Framework', 'init'
          1        DO_FCALL                                      0  $0      
          2      > JMPZ_EX                                          ~1      $0, ->6
          3    >   INIT_STATIC_METHOD_CALL                                  'Framework', 'run'
          4        DO_FCALL                                      0  $2      
          5        BOOL                                             ~1      $2
   86     6    >   INIT_FCALL                                               'var_dump'
          7        INIT_STATIC_METHOD_CALL                                  'Framework', 'getRouter'
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR                                                 $3
         10        DO_ICALL                                                 
   89    11        INIT_STATIC_METHOD_CALL                                  'CMS', 'init'
         12        DO_FCALL                                      0  $5      
         13      > JMPZ_EX                                          ~6      $5, ->17
         14    >   INIT_STATIC_METHOD_CALL                                  'CMS', 'run'
         15        DO_FCALL                                      0  $7      
         16        BOOL                                             ~6      $7
   91    17    >   INIT_FCALL                                               'var_dump'
         18        INIT_STATIC_METHOD_CALL                                  'CMS', 'getRouter'
         19        DO_FCALL                                      0  $8      
         20        SEND_VAR                                                 $8
         21        DO_ICALL                                                 
   92    22        INIT_FCALL                                               'var_dump'
         23        INIT_STATIC_METHOD_CALL                                  'CMS', 'getModulesManager'
         24        DO_FCALL                                      0  $10     
         25        SEND_VAR                                                 $10
         26        DO_ICALL                                                 
   93    27        INIT_STATIC_METHOD_CALL                                  'CMS', 'getServices'
         28        DO_FCALL                                      0  $12     
         29        INIT_METHOD_CALL                                         $12, 'set'
         30        SEND_VAL_EX                                              'framework.router'
         31        SEND_VAL_EX                                              'Ha%2C+its+not+router%21'
         32        DO_FCALL                                      0          
   94    33        INIT_FCALL                                               'var_dump'
         34        INIT_STATIC_METHOD_CALL                                  'CMS', 'getRouter'
         35        DO_FCALL                                      0  $14     
         36        SEND_VAR                                                 $14
         37        DO_ICALL                                                 
         38      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F5Bf0X%3A51%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Bf0X
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E > > RETURN                                                   'router'
   53     1*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F5Bf0X%3A51%240

Function %00%7Bclosure%7D%2Fin%2F5Bf0X%3A77%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Bf0X
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E > > RETURN                                                   'modulesManager'
   79     1*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F5Bf0X%3A77%241

Class ServiceContainer:
Function get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/5Bf0X
function name:  get
number of ops:  15
compiled vars:  !0 = $id, !1 = $creator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   10     2        FETCH_OBJ_IS                                     ~2      'services'
          3        ISSET_ISEMPTY_DIM_OBJ                         0  ~3      ~2, !0
          4        BOOL_NOT                                         ~4      ~3
          5      > JMPZ                                                     ~4, ->11
   11     6    >   INIT_DYNAMIC_CALL                                        !1
          7        DO_FCALL                                      0  $7      
          8        FETCH_OBJ_W                                      $5      'services'
          9        ASSIGN_DIM                                               $5, !0
         10        OP_DATA                                                  $7
   14    11    >   FETCH_OBJ_R                                      ~8      'services'
         12        FETCH_DIM_R                                      ~9      ~8, !0
         13      > RETURN                                                   ~9
   15    14*     > RETURN                                                   null

End of function get

Function set:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Bf0X
function name:  set
number of ops:  6
compiled vars:  !0 = $id, !1 = $service
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   19     2        FETCH_OBJ_W                                      $2      'services'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
   20     5      > RETURN                                                   null

End of function set

End of class ServiceContainer.

Class Framework:
Function getservices:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Bf0X
function name:  getServices
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   FETCH_STATIC_PROP_R          unknown             ~0      'services'
          1        TYPE_CHECK                                    2          ~0
          2      > JMPZ                                                     ~1, ->7
   30     3    >   NEW                                              $2      'Exception'
          4        SEND_VAL_EX                                              '%D0%9D%D0%B5+%D0%B8%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%BE'
          5        DO_FCALL                                      0          
          6      > THROW                                         0          $2
   33     7    >   FETCH_STATIC_PROP_R          unknown             ~4      'services'
          8      > RETURN                                                   ~4
   34     9*     > RETURN                                                   null

End of function getservices

Function init:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 5
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Bf0X
function name:  init
number of ops:  10
compiled vars:  !0 = $services
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV_INIT                                        !0      null
   38     1      > JMPZ                                                     !0, ->5
   39     2    >   ASSIGN_STATIC_PROP                                       'services'
          3        OP_DATA                                                  !0
          4      > JMP                                                      ->9
   41     5    >   NEW                                              $3      'ServiceContainer'
          6        DO_FCALL                                      0          
          7        ASSIGN_STATIC_PROP                                       'services'
          8        OP_DATA                                                  $3
   42     9    > > RETURN                                                   null

End of function init

Function run:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Bf0X
function name:  run
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E > > RETURN                                                   null

End of function run

Function getrouter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Bf0X
function name:  getRouter
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   INIT_STATIC_METHOD_CALL                                  'getServices'
          1        DO_FCALL                                      0  $0      
          2        INIT_METHOD_CALL                                         $0, 'get'
          3        SEND_VAL_EX                                              'framework.router'
          4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F5Bf0X%3A51%240'
   53     5        SEND_VAL_EX                                              ~1
          6        DO_FCALL                                      0  $2      
          7      > RETURN                                                   $2
   54     8*     > RETURN                                                   null

End of function getrouter

End of class Framework.

Class CMS:
Function getservices:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Bf0X
function name:  getServices
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   FETCH_STATIC_PROP_R          unknown             ~0      'services'
          1        TYPE_CHECK                                    2          ~0
          2      > JMPZ                                                     ~1, ->7
   64     3    >   NEW                                              $2      'Exception'
          4        SEND_VAL_EX                                              '%D0%9D%D0%B5+%D0%B8%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%BE+%28%D0%B3%D0%BE%D0%B2%D0%BE%D1%80%D0%B8%D1%82+CMS%29'
          5        DO_FCALL                                      0          
          6      > THROW                                         0          $2
   67     7    >   FETCH_STATIC_PROP_R          unknown             ~4      'services'
          8      > RETURN                                                   ~4
   68     9*     > RETURN                                                   null

End of function getservices

Function run:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Bf0X
function name:  run
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E > > RETURN                                                   null

End of function run

Function getmodulesmanager:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Bf0X
function name:  getModulesManager
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   INIT_STATIC_METHOD_CALL                                  'getServices'
          1        DO_FCALL                                      0  $0      
          2        INIT_METHOD_CALL                                         $0, 'get'
          3        SEND_VAL_EX                                              'cms.modulesmanager'
          4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F5Bf0X%3A77%241'
   79     5        SEND_VAL_EX                                              ~1
          6        DO_FCALL                                      0  $2      
          7      > RETURN                                                   $2
   80     8*     > RETURN                                                   null

End of function getmodulesmanager

Function init:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 5
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Bf0X
function name:  init
number of ops:  10
compiled vars:  !0 = $services
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV_INIT                                        !0      null
   38     1      > JMPZ                                                     !0, ->5
   39     2    >   ASSIGN_STATIC_PROP                                       'services'
          3        OP_DATA                                                  !0
          4      > JMP                                                      ->9
   41     5    >   NEW                                              $3      'ServiceContainer'
          6        DO_FCALL                                      0          
          7        ASSIGN_STATIC_PROP                                       'services'
          8        OP_DATA                                                  $3
   42     9    > > RETURN                                                   null

End of function init

Function getrouter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5Bf0X
function name:  getRouter
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   INIT_STATIC_METHOD_CALL                                  'getServices'
          1        DO_FCALL                                      0  $0      
          2        INIT_METHOD_CALL                                         $0, 'get'
          3        SEND_VAL_EX                                              'framework.router'
          4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F5Bf0X%3A51%240'
   53     5        SEND_VAL_EX                                              ~1
          6        DO_FCALL                                      0  $2      
          7      > RETURN                                                   $2
   54     8*     > RETURN                                                   null

End of function getrouter

End of class CMS.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.38 ms | 1404 KiB | 15 Q