How to use Pipeline in Laravel
Last active
July 22, 2019 05:41
-
-
Save lincolnbrito/776e0ba7ecb1a58a8be8414bc5e89f21 to your computer and use it in GitHub Desktop.
How to use pipelines in Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
]; | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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