Skip to content

Instantly share code, notes, and snippets.

@lincolnbrito
Last active July 22, 2019 05:41
Show Gist options
  • Save lincolnbrito/776e0ba7ecb1a58a8be8414bc5e89f21 to your computer and use it in GitHub Desktop.
Save lincolnbrito/776e0ba7ecb1a58a8be8414bc5e89f21 to your computer and use it in GitHub Desktop.
How to use pipelines in Laravel

How to use Pipeline in Laravel

<?php
namespace App\Pipelines\Stops;
use Closure;
class FirstStop
{
protected $data;
public function handle($data, Closure $next){
//... process data
//return closure to pipeline
return $next($data);
}
}
<?php
namespace App\Pipelines;
use Illuminate\Pipeline\Pipeline;
class ProcessDataPipeline
{
protected $pipeline;
public function __construct()
{
//resolve by container
$this->pipeline = app(Pipeline::class);
}
public function run($data){
//Data to pass through pipeline
$this->pipeline->send($data);
//pipeline stops
$this->pipeline->through([
'App\Pipelines\Stops\FirstStop',
'App\Pipelines\Stops\SecondStop'
]);
//method on stops to process data
//default: handle
//$this->pipeline->via('handle');
//process pipeline and direct result to a callbakc
$result = $this->pipeline->then(function($data){
return [
'processed' => $data
];
});
}
}
<?php
namespace App\Pipelines\Stops;
use Closure;
class SecondStop
{
protected $data;
public function handle($data, Closure $next){
//... process data
//return closure to pipeline
return $next($data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment