jean-beru/pipeline-bundle

Symfony 的管道包

安装: 468

依赖项: 0

建议者: 0

安全性: 0

星标: 1

关注者: 1

分支: 0

开放问题: 1

类型:symfony-bundle

v1.0.1 2024-03-05 08:38 UTC

This package is auto-updated.

Last update: 2024-09-05 09:10:13 UTC


README

Latest Version Total Downloads Monthly Downloads Software License Static analysis Tests

Pipeline 包用于 Symfony。

安装

安装包

composer require jean-beru/pipeline-bundle

如果你不使用 symfony/flex,你必须将此包添加到你的 config/bundles.php 文件中

<?php

return [
    ...
    JeanBeru\PipelineBundle\PipelineBundle::class => ['all' => true],
];

配置

pipelines 定义了所有管道。键定义了管道的名称。

pipelines.__NAME__.stages 定义了在此管道中用作阶段的服务的列表。

pipelines.__NAME__.processor(可选)定义了用于此管道的特定处理器。请参阅下面的“什么是处理器?”部分。

示例

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'

注入管道服务

使用服务 ID

所有定义的管道都将添加为名为 jeanberu_pipeline.pipeline.__NAME__ 的服务。

使用前面的配置,我们将能够注入 3 个管道

  • jeanberu_pipeline.pipeline.update_stock
  • jeanberu_pipeline.pipeline.export
  • jeanberu_pipeline.pipeline.some_computations

使用自动注入

可以使用其名称 PipelineInterface $__NAME__Pipeline 自动注入所有定义的管道。

使用前面的配置,我们将能够注入 3 个管道

  • 管道 $updateStockPipeline
  • 管道 $exportPipeline
  • 管道 $someComputationsPipeline

使用

每个服务都实现了 League\Pipeline\PipelineInterface 接口。

在这个例子中,之前定义的 some_computations 管道将对负载执行一些操作并返回它。

<?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
        
        // ...
    }
}

什么是管道和阶段?

阶段表示一个带有负载要执行的任务。它必须是一个可调用的。你可以实现 League\Pipeline\StageInterface 来确保你的阶段可以被调用。

管道表示链式阶段。你可以将其视为一个 CLI 命令:stage_1 | stage_2 | stage_3。每个阶段接收先前返回的负载。由于管道实现了 League\Pipeline\PipelineInterface,该接口本身又实现了 League\Pipeline\StageInterface,因此你可以将其用作阶段来创建可重用的管道(也称为 pipeline-ception)。例如:stage_1 | pipeline_1 | stage_3

什么是处理器?

要执行管道,需要使用处理器。它必须实现 League\Pipeline\ProcessorInterface。如果你想使用自己的服务,可以这样做。

如果 symfony/event-dispatcher 可用,则将提供一个 jeanberu_pipeline.processor.event_dispatcher_processor 处理器(请参阅 EventDispatcherProcessor),用于分发一些事件

  • JeanBeru\PipelineBundle\Event\BeforeProcessorEvent
  • JeanBeru\PipelineBundle\Event\BeforeStageEvent
  • JeanBeru\PipelineBundle\Event\AfterStageEvent
  • JeanBeru\PipelineBundle\Event\AfterProcessorEvent

测试

安装依赖项

composer install

运行测试

vendor/bin/simple-phpunit
vendor/bin/php-cs-fixer fix --dry-run
vendor/bin/phpstan