jean-beru/pipeline-bundle

Symfony 的管道组件

安装: 469

依赖者: 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,因此您可以使用它作为阶段来制作可重用的管道(又称管道-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