3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Operation { protected $routine; protected $promise; public function __construct(\Closure $routine, $promise) { $this->routine = $routine; $this->promise = $promise; } public function run() { $routine = $this->routine(); $routine($this->promise); } public function getPromise() { return $this->promise; } } class Promise { protected $successClosure; protected $failClosure; public function fulfill(...$args) { $closure = $this->successClosure; $closure(...$args); } public function reject(...$args) { $closure = $this->failClosure; $closure(...$args); } public function then(\Closure $success, \Closure $fail) { $this->successClosure = $success; $this->failClosure = $fail; } } class Queue { protected $queue = []; public function append(Operation $operation) { $this->queue[] = $operation; } public function getOperationGenerator() { foreach ($this->queue as $operation) { yield $operation; } } } class QueueHandler { protected $queues = []; public function addQueue(Queue $queue) { $this->queues[] = $queue; } protected function getOperationGenerator() { $queues = []; foreach ($this->queues as $queue) { $queues[] = $queue->getOperationGenerator(); } foreach ($this->queues as $queue) { yield $queue; } } protected function getRunGenerator() { $operation_generator = $this->getOperationGenerator(); foreach ($operation_generator as $operation) { yield $operation; } } public function run() { $generator = $this->getRunGenerator(); while (1) { foreach ($generator as $operation) { $operation->run(); } } } } $main_queue = new Queue; $secondary_queue = new Queue; function async(\Closure $routine, Queue $queue) : Promise { $promise = new Promise(); $operation = new Operation($routine, $promise); $queue->append($operation); return $promise; } async(function(Promise $promise) { var_dump('Main Fired'); $promise->fulfill(); }, $main_queue)->then(function() { var_dump('Main fulfilled!'); }, function() { var_dump('Main rejected.'); }); async(function(Promise $promise) { var_dump('Secondary Fired'); $promise->fulfill(); }, $secondary_queue)->then(function() { var_dump('Secondary fulfilled!'); }, function() { var_dump('Main rejected.'); }); $queue_handler = new QueueHandler(); $queue_handler->addQueue($main_queue); $queue_handler->addQueue($secondary_queue); $queue_handler->run();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  (null)
number of ops:  40
compiled vars:  !0 = $main_queue, !1 = $secondary_queue, !2 = $queue_handler
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  118     0  E >   NEW                                              $3      'Queue'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $3
  119     3        NEW                                              $6      'Queue'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $6
  130     6        INIT_FCALL                                               'async'
          7        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FoDrDo%3A130%240'
  133     8        SEND_VAL                                                 ~9
          9        SEND_VAR                                                 !0
         10        DO_FCALL                                      0  $10     
         11        INIT_METHOD_CALL                                         $10, 'then'
         12        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FoDrDo%3A133%241'
  135    13        SEND_VAL_EX                                              ~11
         14        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FoDrDo%3A135%242'
  137    15        SEND_VAL_EX                                              ~12
         16        DO_FCALL                                      0          
  139    17        INIT_FCALL                                               'async'
         18        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FoDrDo%3A139%243'
  142    19        SEND_VAL                                                 ~14
         20        SEND_VAR                                                 !1
         21        DO_FCALL                                      0  $15     
         22        INIT_METHOD_CALL                                         $15, 'then'
         23        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FoDrDo%3A142%244'
  144    24        SEND_VAL_EX                                              ~16
         25        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FoDrDo%3A144%245'
  146    26        SEND_VAL_EX                                              ~17
         27        DO_FCALL                                      0          
  148    28        NEW                                              $19     'QueueHandler'
         29        DO_FCALL                                      0          
         30        ASSIGN                                                   !2, $19
  149    31        INIT_METHOD_CALL                                         !2, 'addQueue'
         32        SEND_VAR_EX                                              !0
         33        DO_FCALL                                      0          
  150    34        INIT_METHOD_CALL                                         !2, 'addQueue'
         35        SEND_VAR_EX                                              !1
         36        DO_FCALL                                      0          
  152    37        INIT_METHOD_CALL                                         !2, 'run'
         38        DO_FCALL                                      0          
         39      > RETURN                                                   1

Function async:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  async
number of ops:  17
compiled vars:  !0 = $routine, !1 = $queue, !2 = $promise, !3 = $operation
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  121     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  122     2        NEW                                              $4      'Promise'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !2, $4
  123     5        NEW                                              $7      'Operation'
          6        SEND_VAR_EX                                              !0
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !3, $7
  125    10        INIT_METHOD_CALL                                         !1, 'append'
         11        SEND_VAR_EX                                              !3
         12        DO_FCALL                                      0          
  126    13        VERIFY_RETURN_TYPE                                       !2
         14      > RETURN                                                   !2
  127    15*       VERIFY_RETURN_TYPE                                       
         16*     > RETURN                                                   null

End of function async

Function %00%7Bclosure%7D%2Fin%2FoDrDo%3A130%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $promise
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  130     0  E >   RECV                                             !0      
  131     1        INIT_FCALL                                               'var_dump'
          2        SEND_VAL                                                 'Main+Fired'
          3        DO_ICALL                                                 
  132     4        INIT_METHOD_CALL                                         !0, 'fulfill'
          5        DO_FCALL                                      0          
  133     6      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FoDrDo%3A130%240

Function %00%7Bclosure%7D%2Fin%2FoDrDo%3A133%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  134     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'Main+fulfilled%21'
          2        DO_ICALL                                                 
  135     3      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FoDrDo%3A133%241

Function %00%7Bclosure%7D%2Fin%2FoDrDo%3A135%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  136     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'Main+rejected.'
          2        DO_ICALL                                                 
  137     3      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FoDrDo%3A135%242

Function %00%7Bclosure%7D%2Fin%2FoDrDo%3A139%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $promise
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  139     0  E >   RECV                                             !0      
  140     1        INIT_FCALL                                               'var_dump'
          2        SEND_VAL                                                 'Secondary+Fired'
          3        DO_ICALL                                                 
  141     4        INIT_METHOD_CALL                                         !0, 'fulfill'
          5        DO_FCALL                                      0          
  142     6      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FoDrDo%3A139%243

Function %00%7Bclosure%7D%2Fin%2FoDrDo%3A142%244:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  143     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'Secondary+fulfilled%21'
          2        DO_ICALL                                                 
  144     3      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FoDrDo%3A142%244

Function %00%7Bclosure%7D%2Fin%2FoDrDo%3A144%245:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  145     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'Main+rejected.'
          2        DO_ICALL                                                 
  146     3      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FoDrDo%3A144%245

Class Operation:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  __construct
number of ops:  7
compiled vars:  !0 = $routine, !1 = $promise
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   12     2        ASSIGN_OBJ                                               'routine'
          3        OP_DATA                                                  !0
   13     4        ASSIGN_OBJ                                               'promise'
          5        OP_DATA                                                  !1
   14     6      > RETURN                                                   null

End of function __construct

Function run:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  run
number of ops:  9
compiled vars:  !0 = $routine
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   INIT_METHOD_CALL                                         'routine'
          1        DO_FCALL                                      0  $1      
          2        ASSIGN                                                   !0, $1
   19     3        INIT_DYNAMIC_CALL                                        !0
          4        CHECK_FUNC_ARG                                           
          5        FETCH_OBJ_FUNC_ARG                               $3      'promise'
          6        SEND_FUNC_ARG                                            $3
          7        DO_FCALL                                      0          
   20     8      > RETURN                                                   null

End of function run

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

End of function getpromise

End of class Operation.

Class Promise:
Function fulfill:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  fulfill
number of ops:  8
compiled vars:  !0 = $args, !1 = $closure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV_VARIADIC                                    !0      
   38     1        FETCH_OBJ_R                                      ~2      'successClosure'
          2        ASSIGN                                                   !1, ~2
   39     3        INIT_DYNAMIC_CALL                                        !1
          4        SEND_UNPACK                                              !0
          5        CHECK_UNDEF_ARGS                                         
          6        DO_FCALL                                      1          
   40     7      > RETURN                                                   null

End of function fulfill

Function reject:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  reject
number of ops:  8
compiled vars:  !0 = $args, !1 = $closure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV_VARIADIC                                    !0      
   44     1        FETCH_OBJ_R                                      ~2      'failClosure'
          2        ASSIGN                                                   !1, ~2
   45     3        INIT_DYNAMIC_CALL                                        !1
          4        SEND_UNPACK                                              !0
          5        CHECK_UNDEF_ARGS                                         
          6        DO_FCALL                                      1          
   46     7      > RETURN                                                   null

End of function reject

Function then:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  then
number of ops:  7
compiled vars:  !0 = $success, !1 = $fail
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   50     2        ASSIGN_OBJ                                               'successClosure'
          3        OP_DATA                                                  !0
   51     4        ASSIGN_OBJ                                               'failClosure'
          5        OP_DATA                                                  !1
   52     6      > RETURN                                                   null

End of function then

End of class Promise.

Class Queue:
Function append:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  append
number of ops:  5
compiled vars:  !0 = $operation
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   62     1        FETCH_OBJ_W                                      $1      'queue'
          2        ASSIGN_DIM                                               $1
          3        OP_DATA                                                  !0
   63     4      > RETURN                                                   null

End of function append

Function getoperationgenerator:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 6
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 6
filename:       /in/oDrDo
function name:  getOperationGenerator
number of ops:  8
compiled vars:  !0 = $operation
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   GENERATOR_CREATE                                         
   67     1        FETCH_OBJ_R                                      ~1      'queue'
          2      > FE_RESET_R                                       $2      ~1, ->6
          3    > > FE_FETCH_R                                               $2, !0, ->6
   68     4    >   YIELD                                                    !0
   67     5      > JMP                                                      ->3
          6    >   FE_FREE                                                  $2
   70     7      > GENERATOR_RETURN                                         

End of function getoperationgenerator

End of class Queue.

Class QueueHandler:
Function addqueue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/oDrDo
function name:  addQueue
number of ops:  5
compiled vars:  !0 = $queue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   RECV                                             !0      
   81     1        FETCH_OBJ_W                                      $1      'queues'
          2        ASSIGN_DIM                                               $1
          3        OP_DATA                                                  !0
   83     4      > RETURN                                                   null

End of function addqueue

Function getoperationgenerator:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 10
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 16
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 16
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 16
Branch analysis from position: 10
filename:       /in/oDrDo
function name:  getOperationGenerator
number of ops:  18
compiled vars:  !0 = $queues, !1 = $queue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   GENERATOR_CREATE                                         
   87     1        ASSIGN                                                   !0, <array>
   88     2        FETCH_OBJ_R                                      ~3      'queues'
          3      > FE_RESET_R                                       $4      ~3, ->10
          4    > > FE_FETCH_R                                               $4, !1, ->10
   89     5    >   INIT_METHOD_CALL                                         !1, 'getOperationGenerator'
          6        DO_FCALL                                      0  $6      
          7        ASSIGN_DIM                                               !0
          8        OP_DATA                                                  $6
   88     9      > JMP                                                      ->4
         10    >   FE_FREE                                                  $4
   92    11        FETCH_OBJ_R                                      ~7      'queues'
         12      > FE_RESET_R                                       $8      ~7, ->16
         13    > > FE_FETCH_R                                               $8, !1, ->16
   93    14    >   YIELD                                                    !1
   92    15      > JMP                                                      ->13
         16    >   FE_FREE                                                  $8
   95    17      > GENERATOR_RETURN                                         

End of function getoperationgenerator

Function getrungenerator:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 8
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 8
filename:       /in/oDrDo
function name:  getRunGenerator
number of ops:  10
compiled vars:  !0 = $operation_generator, !1 = $operation
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   97     0  E >   GENERATOR_CREATE                                         
   99     1        INIT_METHOD_CALL                                         'getOperationGenerator'
          2        DO_FCALL                                      0  $2      
          3        ASSIGN                                                   !0, $2
  101     4      > FE_RESET_R                                       $4      !0, ->8
          5    > > FE_FETCH_R                                               $4, !1, ->8
  102     6    >   YIELD                                                    !1
  101     7      > JMP                                                      ->5
          8    >   FE_FREE                                                  $4
  104     9      > GENERATOR_RETURN                                         

End of function getrungenerator

Function run:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 4
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 4
Branch analysis from position: 11
Branch analysis from position: 4
Branch analysis from position: 9
filename:       /in/oDrDo
function name:  run
number of ops:  12
compiled vars:  !0 = $generator, !1 = $operation
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  108     0  E >   INIT_METHOD_CALL                                         'getRunGenerator'
          1        DO_FCALL                                      0  $2      
          2        ASSIGN                                                   !0, $2
  110     3      > JMP                                                      ->10
  111     4    > > FE_RESET_R                                       $4      !0, ->9
          5    > > FE_FETCH_R                                               $4, !1, ->9
  112     6    >   INIT_METHOD_CALL                                         !1, 'run'
          7        DO_FCALL                                      0          
  111     8      > JMP                                                      ->5
          9    >   FE_FREE                                                  $4
  110    10    > > JMPNZ                                                    1, ->4
  115    11    > > RETURN                                                   null

End of function run

End of class QueueHandler.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
175.57 ms | 1423 KiB | 17 Q