Ice Cream Pipeline
, (*1)
This is a basic implementation of the pipeline pattern, wherein something is
passed through a series of classes and the end result is passed out., (*2)
- Requires PHP 7.2
- Is standalone
Purpose
I wanted to build this for the Ice cream framework, it would be useful in passing object in through a pipeline and manipulating them., (*3)
I also built this to learn the pipeline pattern and how it is used and how other frameworks use the pattern when manipulating objects., (*4)
Install
composer require ice-cream/pipeline
, (*5)
Documentation
You can see the full documentation for the project here, (*6)
How to use?
class AddOne {
public function customHandle(int $number) {
return $number + 1;
}
public function handle(int $number) {
return $number + 1;
}
}
class TimesTwo {
public function customHandle(int $number) {
return $number * 2;
}
public function handle(int $number) {
return $number * 2;
}
}
$response = (new Pipeline())
->pass(10)
->through([
new AddOne,
new TimesTwo,
])
->withMethod('customHandle')
->process()
->getResponse();
var_dump($response); // => 22
What you will notice here is that we have a set of classes, the value 10 is passed through
both of these and then returned in the getResponse()
, the reason we do not do this, returning the response
in process
, is because you might want to call a new method: then()
which takes a closure, as you will see in the examples below., (*7)
then(\Closure $func)
Building on the above example, lets assume you want to do something after you process the value, or object passed in:, (*8)
(new Pipeline())
->pass(10)
->through([
new AddOne,
new TimesTwo,
])
->withMethod('customHandle')
->process()
->then(function($response) {
$response *= 10;
var_dump($response); // => 220
});
You cannot call getResponse
because this is a closure, and anonymous function., (*9)
What if I don't have a custom method?
(new Pipeline())
->pass(10)
->through([
new AddOne,
new TimesTwo,
])
->process()
->then(function($response) {
$response *= 10;
var_dump($response); // => 220
});
Building on the above, we see that the classes, AddOne
and TimesTwo
implement a handle()
method. If you do not
define a withMethod(String $string)
then we default to the handle
method that must be implemented or you get an \Exception
thrown telling you which class failed to implement the method in question., (*10)