laravel / ideas

Issues board used for Laravel internals discussions.
939 stars 28 forks source link

Proposal: Pipeline middleware #2072

Open dmitryuk opened 4 years ago

dmitryuk commented 4 years ago

The proposal is to add middleware feature to pipeline stages. Handle should be called for each stage in pipeline. For example, if you want to track performance of each stage in a pipeline, you should kindly do this:

class PipelineBenchmarkMiddleware
{
  public function handle($parameters,  Closure $next)
{
    $start = microtime(true);
    $next($parameters);
    $end = microtime(true);
   // Log this $end-$start
}
}
$pipes = [
    StrToLowerAction::class,
    SetUserNameAction::class
];

$data = 'Hello world';

$finalData = app(Pipeline::class)
    ->send($data) 
    ->through($pipes)
    ->middleware(new PipelineBenchmarkMiddleware())
    ->then(function ($changedData) {
        return $changedData;
    });

var_dump($finalData); 
ryangjchandler commented 4 years ago

I don't think this is necessary. There are already ways you could do this in the pipes themselves. The first pipe will receive a $next closure, just do the $start, $next() $end bit in the first pipe.

ollieread commented 4 years ago

@dmitryuk Pipes are essentially middleware, so having a pipeline of middleware to run on a pipeline seems somewhat recursive and redundant.