Laravel: The hidden Pipeline [Part 4]

Serialized content processing, meet your new friend: the Pipeline.

Photo by Blake Weyland on Unsplash

How the Pipeline works

$array = [1, 2, 3, 4];$callable = function ($carry, $value) {
return ($carry + $value) * 2;
$result = array_reduce($array, $callable, 0);echo $results; // 52
Artistic illustration.
  1. The first argument takes the pipe list and reverses its order, making the last pipe the first one, and vice versa. You will see why later.
  2. The second is the callable to use against each pipe. It basically says to use the Closure that returns the carry() method. More on that also later.
  3. The third and final argument is another Closure. This represent the “initial state” of the “passable” — the callable will be used against this Closure first.
Photo by Maria Hochgesang on Unsplash
  1. The initial value is a veric basic Closure that receives a value and returns the value (thanks, thenReturn()).
  2. The function receives this Closure as $stack, and the item as $pipe.
  3. In exchange, the function it returns a Closure that must receive the argument $passable, but will have access to both the $stack and $pipe internally. Since is a Closure, the code inside the brackets is inert until executed.
  4. If there is another item to process, it will call step 2, but the “carried” value will be the Closure (as $stack). If not, the returned value is this final Closure.
public function handle($passable, Closure $next)
return $next('I have altered the deal!');
public function handle($passable, Closure $next)
// Resolve all the other layers without getting out of here
$endResult = $next($passable);
return $endResult . ' and this is the end';



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Italo Baeza Cabrera

Graphic Designer graduate. Full Stack Web Developer. Retired Tech & Gaming Editor.