Skip to content

Instantly share code, notes, and snippets.

@ilzrv
Last active April 7, 2024 00:49
Show Gist options
  • Save ilzrv/697dddbcb3dc6560f48cd5976a89b9c9 to your computer and use it in GitHub Desktop.
Save ilzrv/697dddbcb3dc6560f48cd5976a89b9c9 to your computer and use it in GitHub Desktop.
Laravel Pipeline and Hub Example
<?php
class Pipe1
{
public function handle($passable, Closure $next)
{
$passable = ucfirst($passable);
return $next($passable);
}
}
<?php
class Pipe2
{
public function handle($passable, Closure $next)
{
$passable = $passable . ' - Pipe2!';
return $next($passable);
}
}
<?php
Route::get('/hub', function () {
/** @var \Illuminate\Pipeline\Hub $hub */
$hub = app(\Illuminate\Pipeline\Hub::class);
$hub->pipeline('awesome-hub-name', function ($pipeline, $object) {
return $pipeline
->send($object)
->through([
Pipe1::class,
Pipe2::class,
])
->thenReturn();
});
$hub->pipe('stringus', 'awesome-hub-name');
});
Route::get('/pipeline', function () {
/** @var \Illuminate\Pipeline\Pipeline $pipeline */
$pipeline = app(\Illuminate\Pipeline\Pipeline::class);
$pipeline
->send('stringus!')
->through([
Pipe1::class,
Pipe2::class,
])
->thenReturn();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment