Documentation
Introduction
Pipeline is the beautiful architectural design pattern. Pipelines is referred to series of stages where each stage has chance to operate on the payload and return a response. This response is passed onto the next stage in line. In cygnite, it is specially used for HTTP Middleware.
[Note: If a stage does not returns the $next closure, no stages after it in the pipeline will run.]
Using Closures
You may pass Closure stages as array into Pipeline. Closure stages accepts the first parameter as given input and second parameter as next pipe in the pipeline. Let us take a look at a simple example:
public function indexAction()
{
$pipeline = new \Cygnite\Pipeline\Pipeline();
$pipeline->setContainer($this->app);
$pipes = [
function ($request, $next) {
$request .= "-bar";
return $next($request);
},
function ($request, $next) {
$request .= "-baz";
return $next($request);
}
];
echo $pipeline->send("foo")
->through($pipes)
->run();
//output: foo-bar-baz
}
Using Objects
You may pass array of objects into Pipeline. For Example:
namespace Apps\Middleware;
use Cygnite\Http\Requests\Request;
class AuthMiddleware
{
public function handle(Request $request, \Closure $next)
{
$request .= "-play";
return $next($request);
}
}
namespace Apps\Middleware;
class CsrfVerifierMiddleware
{
public function handle($request, \Closure $next)
{
$request .= "-football";
return $next($request);
}
}
$pipes = [new \Apps\Middleware\AuthMiddleware(), new \Apps\Middleware\CsrfVerifierMiddleware()];
echo $pipeline->send("I")
->through($pipes)
->run();
//output: I-play-football
By default handle method will be called. However you can specify the method name to call. Let us take a look at an example:
$pipes = [new \Apps\Middleware\AuthMiddleware()];
namespace Apps\Middleware;
class AuthMiddleware
{
public function process($request, \Closure $next)
{
$request .= "-bar";
return $next($request);
}
}
echo $pipeline->send("foo")
->through($pipes, 'process')
->run();
Using Classes With Parameter
Sometimes you may want to pass some parameters into the Middleware class. Let us have a look at example:
$parameters = ['one', 'two'];
$pipes = ['\Apps\Middleware\PipelineParameter:'.implode(',', $parameters)];
namespace Apps\Middleware;
class PipelineParameter
{
public function process($request, \Closure $next, $one, $two)
{
$request .= "-bar".$one."-".$two;
return $next($request);
}
}
echo $pipeline->send("foo")
->through($pipes)
->run();
//output: foo-bar-one-two
Resolving Via Container
You might has stored a object into Container, pipeline can call the method resolving the object from Container.
$app['pipeline.resolver'] = $app->make('\Apps\Middleware\PipelineTestResolver');
namespace Apps\Middleware;
class PipelineTestResolver
{
public function handle($request, \Closure $next)
{
return $next($request);
}
}
echo $pipeline->send("foo")
->through(['pipeline.resolver'])
->run();
Dependency Injection In Middleware Class
You may wish to inject some object to perform some operation before returning request.
namespace Apps\Middleware;
use Apps\Resources\Extensions\Api;
class PipelineTestResolver
{
private $api;
public function __construct(Api $api)
{
$this->api = $api;
}
public function handle($request, \Closure $next)
{
return $next($request);
}
}
echo $pipeline->send("foo")
->through([$app->make('\Apps\Middleware\PipelineTestResolver')])
->run();
Applying Callback
You may wish to apply some callback at the end of the pipeline.
$pipes = [
function ($request, $next) {
$request .= "-bar";
return $next($request);
}
];
echo $pipeline->send("foo")
->through($pipes)
->then(function ($output) {
return strtoupper($output);
})
->run();
//output: FOO-BAR