Pipeline and PHP

Ilya Chubarov
2 min readJul 7, 2017

--

What is Pipeline?

If you haven’t heard about such pattern as Pipeline then now it’s time to learn about it.

It’s very simple, let’s draw an analogy like a regular Assembly line in the factory. The product goes through several stages of processing, and it can be checked or changed in the process.

Let’s say we have a certain sequence of actions and we don’t know if we should handle some specific action, or we should handle all the actions at once, both situations can be used as Pipeline.

Let’s take Laravel as an example

It Laravel includes a package called Illuminate/pipeline.

Let’s that we have a variable and we don’t know the type of it. We want to handle if differently depending of it’s type. Of course we can use simple if/else statements, but we’re smart guys and we don’t wanna use too mane statements, right?

To use Illuminate/pipeline you need to determine three things :

  • To determine which value we want to work on each stage with.
  • Define a method or function to work with the value.
  • Define a function that notifies us when pipeline reaches the end of the operations.

Let’s write some classes to handle our values.

If we’ll analyze the code above, we should see that it uses the same principle of work as Laravel Middleware.

Here is an example :

class RedirectIfAuthenticated 
{
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check())
{
return redirect('/home');
}
return $next($request);
}

If user is not registered then we interrupt our pipeline and redirect the user to the home page, otherwise it goes to the next middleware.

When the code reaches the end, control will be passed to the back to the framework.

Use of the component Illuminate has several drawbacks :

  • We need to implement the handle method in the classes
  • You must specify the Container class in the dependencies

Let’s look to other good alternative League\Pipeline

League\Pipeline package has a more friendly API

Here is the previous example rewritten :

In this example, if we want to continue, just return the original value . Otherwise, we can change it and return the changed value..

Since the pipe is the type of callable we can use more flexible structures for the call.

League’s package provides class Builder for conditional adding pipe.

It is also possible to split it into separate code components, and to reuse them in each other.

Now it’s time to write something more worthwhile..

We have the order that you need :

  • To register in the log
  • Send sms
  • Send email

The main difference from Illuminate/pipeline and the bad side is that we can not pass more than one parameter to the handler method.

It is obvious that it is extremely useful in some cases.

Now I hope it is obvious that the Pipeline pattern can be useful in many cases when the using of simple If/else statements are not enough.

References

How to use the Pipeline Design Pattern in Laravel

The Pipeline Pattern — for fun and profit

My github

My twitter

--

--

Responses (2)