league/pipeline
League\Pipeline provides a lightweight, immutable implementation of the Pipeline pattern for PHP. Compose sequential processing stages (any callable or invokable class) and pass a payload through each stage in order, producing a final result.
Install via composer require league/pipeline. Begin by defining stages as invokable classes or closures, then chain them with Pipeline::pipe(). The core workflow is simple:
(new Pipeline)->pipe($stage1)->pipe($stage2)->process($payload)Start with the Basic Example in the README – it demonstrates transforming data through sequential stages with zero boilerplate. For Laravel integration, inject stages via the service container and use pipelines in service classes or console commands.
handle() methods with staged pipelines (e.g., ValidateRequest → Authorize → Execute → FormatResponse). Keeps each concern isolated and testable.$apiRequestPipeline = (new Pipeline)->pipe(new FormatUrl)->pipe(new AddHeaders)->pipe(new ExecuteRequest)). Nest them in higher-level workflows.NormalizePayload → EnrichData → PersistToDb → NotifyExternal).PipelineBuilder to assemble pipelines dynamically from config (e.g., in config/pipelines.php), enabling environment-specific stage toggling without code changes.pipe() always returns a new pipeline instance. Many devs accidentally forget $pipeline = $pipeline->pipe($stage) and wonder why stages vanish. Add strict types (declare(strict_types=1)) in stage classes to catch method signature mismatches early.process() calls in try-catch blocks outside the pipeline, or add a TryCatchStage class that rethrows inside a try {} block if needed.callable works, using typed StageInterface implementations (or PSR-4 autoloading with strict PHPDocs) prevents runtime errors. Avoid closures for complex logic – they’re hard to unit test and mock.->pipe($subPipeline)) creates a new composite stage. Verify expected payload types change between stages (e.g., OrderDto → Command → Result) to avoid confusion. Use PHPStan/Psalm for deep type checking.__invoke() methods), then test pipelines as integration points. Mock HTTP clients in stages with Prophecy, and assert on payload transformation at each step.How can I help you explore Laravel packages today?