Download the PHP package rockett/pipeline without Composer
On this page you can find all versions of the php package rockett/pipeline. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rockett/pipeline
More information about rockett/pipeline
Files in rockett/pipeline
Package pipeline
Short Description A plug and play pipeline implementation.
License MIT
Informations about the package pipeline
Rockett\Pipeline
Built atop League's excellent package, Rockett\Pipeline
provides an implementation of the pipeline pattern with additional processors for conditional interruption and stage tapping.
Requirements
- PHP 8.3+
Installation
Quick Start
Table of Contents
- Pipeline Pattern
- Immutability
- Usage
- Class-based stages
- Re-usability
- Pipeline Builders
- Processors
- Handling Exceptions
Pipeline Pattern
The pipeline pattern allows you to easily compose sequential operations by chaining stages. A pipeline consists of zero, one or more stages. A pipeline can process a payload, known as a traveler. When the pipeline is processed, the traveler will be passed to the first stage. From that moment on, the resulting output is passed on from stage to stage.
In the simplest form, the execution-chain can be represented as a foreach
loop:
Effectively, this is the equivalent of:
Immutability
Pipelines are implemented as immutable stage-chains, contracted by the PipelineContract
interface. When you add a new stage, the pipeline will be cloned with the new stage added in. This makes pipelines easy to re-use, and minimizes side-effects.
Usage
Operations in a pipeline (stages) can accept anything from the pipeline that satisfies the callable
type-hint. So closures and anything that's invokable will work.
Class-based stages
As stages accept callables, class-based stages are also possible. The StageContract
can be implemented on each stage, ensuring that you have the correct method-signature for the __invoke
method.
You are free to create your own stage contract, should you wish to type-hint the traveler and set a return-type (this is useful, and recommended, when the type of data being injected into and returned from a stage must always remain the same).
Re-usability
Because the PipelineContract
is an extension of the StageContract
, pipelines can be re-used as stages. This creates a highly-composable model to create complex execution-patterns, whilst keeping the cognitive-load low.
For example, if you want to compose a pipeline to process API calls, you would create something like this:
Here, we create a pipeline that processes an API request (single-responsibility), and compose it into a pipeline that deletes a news article.
Pipeline Builders
Because pipelines themselves are immutable, pipeline builders are introduced to facilitate distributed-composition of a pipeline. These builders collect stages in advance and then allow you to create a pipeline at any given time.
Processors
This is where Rockett\Pipeline extends League's package – when stages are piped through a pipeline, they are done so using a processor, which is responsible for iterating through each stage and piping it into the owning pipeline. There are four available processors:
FingersCrossedProcessor
(this is the default)InterruptibleProcessor
– Exit pipelines early based on conditionsTapProcessor
– Execute callbacks before/after each stage (requires at least one callback)InterruptibleTapProcessor
– Combines both interruption and tapping (requires at least one tap callback)
The default processor only iterates and pipes stages. It does nothing else, and there is no way to exit the pipeline without throwing an exception.
Exiting pipelines early
The InterruptibleProcessor
provides a mechanism that allows you to exit the pipeline early, if so required. This is done by way of a callable
that is invoked at every stage as a condition to continuing the pipeline:
In this example, the callable will check if the traveler has an error and, if so, it will return true
, causing the processor to exit the pipeline early and return the current traveler as the output.
Helper methods:
Invoking actions on each stage
Using the TapProcessor
, you can invoke an action before and/or after a stage is piped through a pipeline. This is useful for cross-cutting concerns like logging, metrics, or debugging.
At least one callback is required. You can also use fluent methods:
Combining interruption and tapping
The InterruptibleTapProcessor
combines both features:
[!NOTE] This will likely become the default processor in a future release.
[!TIP] The
InterruptibleTapProcessor
is particularly useful for complex pipelines where you need both conditional logic and observability.
Handling Exceptions
This package is completely transparent when it comes exceptions and other throwables – it will not catch an exception or silence an error.
You need to catch these in your code, either inside a stage or at the time the pipeline is called to process a payload.
Testing
License
Pipeline is licensed under the permissive MIT license.
Contributing
Contributions are welcome – if you have something to add to this package, or have found a bug, feel free to submit a merge request for review.