jean-beru / pipeline-bundle
Pipeline bundle for Symfony
Installs: 550
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 1
Type:symfony-bundle
Requires
- php: ^7.4|^8.0
- league/pipeline: ^1.0
- symfony/config: ^5.0|^6.0|^7.0
- symfony/dependency-injection: ^5.0|^6.0|^7.0
- symfony/http-kernel: ^5.0|^6.0|^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.5
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.3
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-symfony: ^1.0
- symfony/phpunit-bridge: ^5.0|6.0|^7.0
Suggests
- symfony/event-dispatcher: Allows event subscription on the stage execution
This package is auto-updated.
Last update: 2024-11-05 09:34:49 UTC
README
Pipeline bundle for Symfony.
Install
Install bundle :
composer require jean-beru/pipeline-bundle
If you do not use symfony/flex, you have to add this bundle to your
config/bundles.php
file :
<?php return [ ... JeanBeru\PipelineBundle\PipelineBundle::class => ['all' => true], ];
Configuration
pipelines
defines all pipelines. The key defines pipeline's name.
pipelines.__NAME__.stages
defines services to use as stages in this pipeline.
pipelines.__NAME__.processor
(optional) defines a specific processor to use with this pipeline. See "What is a
processor ?" section below.
Example :
jeanberu_pipeline: pipelines: update_stock: processor: 'jeanberu_pipeline.processor.event_dispatcher_processor' stages: - 'App\Stages\RetrieveProduct' - 'App\Stages\UpdateStockProduct' - 'App\Stages\PersistProduct' export: stages: - 'App\Stages\RetrieveProduct' - 'App\Stages\ExportProduct' some_computations: stages: - 'App\Stages\AddOne' - 'App\Stages\AddThree' - 'App\Stages\MultiplyByFour'
Inject pipeline services
Using service ID
All defined pipeline is added as a service named jeanberu_pipeline.pipeline.__NAME__
.
With the previous configuration, we will be able to inject 3 pipelines :
- jeanberu_pipeline.pipeline.update_stock
- jeanberu_pipeline.pipeline.export
- jeanberu_pipeline.pipeline.some_computations
Using autowiring
All defined pipeline can be autowired using its name PipelineInterface $__NAME__Pipeline
.
With the previous configuration, we will be able to inject 3 pipelines :
- Pipeline $updateStockPipeline
- Pipeline $exportPipeline
- Pipeline $someComputationsPipeline
Usage
Each service implements the League\Pipeline\PipelineInterface
interface.
In this example, some_computations
pipeline defined before will make some operations on the payload and will return
it.
<?php use League\Pipeline\PipelineInterface; final class MyService { private PipelineInterface $someComputationsPipeline; public function __construct(PipelineInterface $someComputationsPipeline) { $this->someComputationsPipeline = $someComputationsPipeline; } public function __invoke(int $base) { // $base = 2 $result = $this->someComputationsPipeline($base); // $result = (((2 + 1) + 3) * 4) = 24 // ... } }
What are pipeline and stage ?
A stage represents a task to execute with a payload. It must be a callable. You can implement
League\Pipeline\StageInterface
to ensure that your stage can be called.
A pipeline represents chained stages. You can see it like a CLI command: stage_1 | stage_2 | stage_3
. Each stage
receives the previously returned payload.
Since pipelines implements League\Pipeline\PipelineInterface
which implements itself League\Pipeline\StageInterface
,
you can use it as a stage to make re-usable pipelines (a.k.a. pipeline-ception). Ex: stage_1 | pipeline_1 | stage_3
.
What is a processor ?
To execute a pipeline, a processor is used. It must implement League\Pipeline\ProcessorInterface
. You can use your
own service if you want to.
If symfony/event-dispatcher
is available, a jeanberu_pipeline.processor.event_dispatcher_processor
processor
will be available (see EventDispatcherProcessor) to dispatch some
events :
- JeanBeru\PipelineBundle\Event\BeforeProcessorEvent
- JeanBeru\PipelineBundle\Event\BeforeStageEvent
- JeanBeru\PipelineBundle\Event\AfterStageEvent
- JeanBeru\PipelineBundle\Event\AfterProcessorEvent
Test
Install dependencies :
composer install
Run tests :
vendor/bin/simple-phpunit vendor/bin/php-cs-fixer fix --dry-run vendor/bin/phpstan