3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Water { public $color = 'clear'; public $temp = 'cold'; } class Pipeline { /** * @var array The parameters to pass through */ protected $parameters = []; /** * @var \Closure[] The list of pipes that the parameters will fall through * Pipe should match function($parameter..., \Closure $next) {} */ protected $pipes = []; /** * @type \Closure The "then" closure, it will receive the parameters you pipe through */ protected $thenClosure; /** * @inheritdoc */ public function send(...$parameters) : PipelineMap { $this->parameters = $parameters; return $this; } /** * @inheritdoc */ public function through($pipes) : PipelineMap { $this->pipes = is_array($pipes) ? $pipes : func_get_args(); return $this; } /** * @inheritdoc */ public function then(callable $then) : PipelineMap { $this->thenClosure = $then; return $this; } /** * @inheritdoc */ public function execute() { $pipes = array_reverse($this->pipes); /** @type \Closure $linked_closure */ $linked_closure = array_reduce($pipes, $this->getIterator(), $this->getInitial($this->thenClosure)); return $linked_closure(...$this->parameters); } /** * Get the iterator closure, this wraps every item in the list and injects the * @return \Closure */ protected function getIterator() : \Closure { return function ($next, $pipe) { return function (...$parameters) use ($next, $pipe) { $parameters[] = $next; return $pipe(...$parameters); }; }; } /** * The initial closure * @return \Closure */ protected function getInitial(\Closure $then) : \Closure { return function(...$parameters) use ($then) { return $then(...$parameters); }; } } // Lets make some pipes $color_water_blue_pipe = function (Water $water, callable $next_pipe) { $water->color = 'blue'; // We colored it, now we're done. Send it to the next pipe and return the result! return $next_pipe($water); }; $heat_water_pipe = function (Water $water, callable $next_pipe) { $water->temp = 'hot'; // We heated it, send it to the next pipe! return $next_pipe($water); }; // Make some water $water = new Water(); // Send it through our pipes $piped_water = (new \Buttress\Pipeline\Pipeline()) ->send($water) ->through([ $color_water_blue_pipe, $heat_water_pipe ]) ->then(function($water) { return $water; }); echo "The water is {$piped_water->color} and {$piped_water->temp}!";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  (null)
number of ops:  31
compiled vars:  !0 = $color_water_blue_pipe, !1 = $heat_water_pipe, !2 = $water, !3 = $piped_water
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FBjuQe%3A83%243'
          1        ASSIGN                                                   !0, ~4
   90     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FBjuQe%3A90%244'
          3        ASSIGN                                                   !1, ~6
   98     4        NEW                                              $8      'Water'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !2, $8
  101     7        NEW                                              $11     'Buttress%5CPipeline%5CPipeline'
          8        DO_FCALL                                      0          
  102     9        INIT_METHOD_CALL                                         $11, 'send'
         10        SEND_VAR_EX                                              !2
         11        DO_FCALL                                      0  $13     
  103    12        INIT_METHOD_CALL                                         $13, 'through'
         13        INIT_ARRAY                                       ~14     !0
         14        ADD_ARRAY_ELEMENT                                ~14     !1
         15        SEND_VAL_EX                                              ~14
         16        DO_FCALL                                      0  $15     
  104    17        INIT_METHOD_CALL                                         $15, 'then'
         18        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FBjuQe%3A104%245'
  106    19        SEND_VAL_EX                                              ~16
         20        DO_FCALL                                      0  $17     
  101    21        ASSIGN                                                   !3, $17
  108    22        ROPE_INIT                                     5  ~22     'The+water+is+'
         23        FETCH_OBJ_R                                      ~19     !3, 'color'
         24        ROPE_ADD                                      1  ~22     ~22, ~19
         25        ROPE_ADD                                      2  ~22     ~22, '+and+'
         26        FETCH_OBJ_R                                      ~20     !3, 'temp'
         27        ROPE_ADD                                      3  ~22     ~22, ~20
         28        ROPE_END                                      4  ~21     ~22, '%21'
         29        ECHO                                                     ~21
         30      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FBjuQe%3A63%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $next, !1 = $pipe
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   64     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FBjuQe%3A64%241'
          3        BIND_LEXICAL                                             ~2, !0
          4        BIND_LEXICAL                                             ~2, !1
   67     5      > RETURN                                                   ~2
   68     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FBjuQe%3A63%240

Function %00%7Bclosure%7D%2Fin%2FBjuQe%3A64%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $parameters, !1 = $next, !2 = $pipe
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   RECV_VARIADIC                                    !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
   65     3        ASSIGN_DIM                                               !0
          4        OP_DATA                                                  !1
   66     5        INIT_DYNAMIC_CALL                                        !2
          6        SEND_UNPACK                                              !0
          7        CHECK_UNDEF_ARGS                                         
          8        DO_FCALL                                      1  $4      
          9      > RETURN                                                   $4
   67    10*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FBjuQe%3A64%241

Function %00%7Bclosure%7D%2Fin%2FBjuQe%3A76%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $parameters, !1 = $then
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   RECV_VARIADIC                                    !0      
          1        BIND_STATIC                                              !1
   77     2        INIT_DYNAMIC_CALL                                        !1
          3        SEND_UNPACK                                              !0
          4        CHECK_UNDEF_ARGS                                         
          5        DO_FCALL                                      1  $2      
          6      > RETURN                                                   $2
   78     7*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FBjuQe%3A76%242

Function %00%7Bclosure%7D%2Fin%2FBjuQe%3A83%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $water, !1 = $next_pipe
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   84     2        ASSIGN_OBJ                                               !0, 'color'
          3        OP_DATA                                                  'blue'
   87     4        INIT_DYNAMIC_CALL                                        !1
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $3      
          7      > RETURN                                                   $3
   88     8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FBjuQe%3A83%243

Function %00%7Bclosure%7D%2Fin%2FBjuQe%3A90%244:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $water, !1 = $next_pipe
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   91     2        ASSIGN_OBJ                                               !0, 'temp'
          3        OP_DATA                                                  'hot'
   94     4        INIT_DYNAMIC_CALL                                        !1
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $3      
          7      > RETURN                                                   $3
   95     8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FBjuQe%3A90%244

Function %00%7Bclosure%7D%2Fin%2FBjuQe%3A104%245:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  {closure}
number of ops:  3
compiled vars:  !0 = $water
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  104     0  E >   RECV                                             !0      
  105     1      > RETURN                                                   !0
  106     2*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FBjuQe%3A104%245

Class Water: [no user functions]
Class Pipeline:
Function send:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  send
number of ops:  8
compiled vars:  !0 = $parameters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV_VARIADIC                                    !0      
   28     1        ASSIGN_OBJ                                               'parameters'
          2        OP_DATA                                                  !0
   29     3        FETCH_THIS                                       ~2      
          4        VERIFY_RETURN_TYPE                                       ~2
          5      > RETURN                                                   ~2
   30     6*       VERIFY_RETURN_TYPE                                       
          7*     > RETURN                                                   null

End of function send

Function through:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  through
number of ops:  14
compiled vars:  !0 = $pipes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   36     1        TYPE_CHECK                                  128          !0
          2      > JMPZ                                                     ~2, ->5
          3    >   QM_ASSIGN                                        ~3      !0
          4      > JMP                                                      ->7
          5    >   FUNC_GET_ARGS                                    ~4      
          6        QM_ASSIGN                                        ~3      ~4
          7    >   ASSIGN_OBJ                                               'pipes'
          8        OP_DATA                                                  ~3
   37     9        FETCH_THIS                                       ~5      
         10        VERIFY_RETURN_TYPE                                       ~5
         11      > RETURN                                                   ~5
   38    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null

End of function through

Function then:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  then
number of ops:  8
compiled vars:  !0 = $then
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   44     1        ASSIGN_OBJ                                               'thenClosure'
          2        OP_DATA                                                  !0
   45     3        FETCH_THIS                                       ~2      
          4        VERIFY_RETURN_TYPE                                       ~2
          5      > RETURN                                                   ~2
   46     6*       VERIFY_RETURN_TYPE                                       
          7*     > RETURN                                                   null

End of function then

Function execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  execute
number of ops:  25
compiled vars:  !0 = $pipes, !1 = $linked_closure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   INIT_FCALL                                               'array_reverse'
          1        FETCH_OBJ_R                                      ~2      'pipes'
          2        SEND_VAL                                                 ~2
          3        DO_ICALL                                         $3      
          4        ASSIGN                                                   !0, $3
   54     5        INIT_FCALL                                               'array_reduce'
          6        SEND_VAR                                                 !0
          7        INIT_METHOD_CALL                                         'getIterator'
          8        DO_FCALL                                      0  $5      
          9        SEND_VAR                                                 $5
         10        INIT_METHOD_CALL                                         'getInitial'
         11        CHECK_FUNC_ARG                                           
         12        FETCH_OBJ_FUNC_ARG                               $6      'thenClosure'
         13        SEND_FUNC_ARG                                            $6
         14        DO_FCALL                                      0  $7      
         15        SEND_VAR                                                 $7
         16        DO_ICALL                                         $8      
         17        ASSIGN                                                   !1, $8
   55    18        INIT_DYNAMIC_CALL                                        !1
         19        FETCH_OBJ_R                                      ~10     'parameters'
         20        SEND_UNPACK                                              ~10
         21        CHECK_UNDEF_ARGS                                         
         22        DO_FCALL                                      1  $11     
         23      > RETURN                                                   $11
   56    24*     > RETURN                                                   null

End of function execute

Function getiterator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  getIterator
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FBjuQe%3A63%240'
   68     1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   69     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function getiterator

Function getinitial:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BjuQe
function name:  getInitial
number of ops:  7
compiled vars:  !0 = $then
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   RECV                                             !0      
   76     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FBjuQe%3A76%242'
          2        BIND_LEXICAL                                             ~1, !0
   78     3        VERIFY_RETURN_TYPE                                       ~1
          4      > RETURN                                                   ~1
   79     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function getinitial

End of class Pipeline.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.85 ms | 1404 KiB | 17 Q