masroore/pipeline 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

masroore/pipeline

Composer 安装命令:

composer require masroore/pipeline

包简介

Pipeline pattern implementation

README 文档

README

Pipeline

A PHP package to build multi-staged workflows.

Author Maintainer

This package provides a pipeline pattern implementation.

Installation

$ composer require masroore/pipeline

Pipeline Pattern

The pipeline pattern allows you to easily compose sequential stages by chaining stages.

In this particular implementation the interface consists of three parts:

  • PipelineContextInterface
  • PipelineStageInterface
  • PipelineInterface

A pipeline consists of one or multiple stages. A pipeline can process a payload. During the processing the payload will be passed to the first stage. From that moment on the resulting value is passed on from stage to stage.

Concepts

The package has two building blocks to create workflows : Pipeline and Stage and Step . A pipeline is a sequential collection of stages.

The PipelineStageInterface Interface

Stage is the unit of work which can be sequentially executed with other stages. To do that, we need to implement the PipelineStageInterface interface.

interface PipelineStageInterface
{
    public function id(): string;

    public function process(PipelineContextInterface $context): void;
}

To satisfy the interface we need to implement process(), id() methods in the target type. For e.g:

class TimesTwoStage implements PipelineStageInterface
{
    public function process(PipelineContextInterface $context): void
    {
        $val = (int) $context->getPayload();
        $val *= 2;
        $context->setPayload($val);
    }

    public function id(): string
    {
        return __CLASS__;
    }    
}

The current stage receives a PipelineContextInterface value passed on by the previous stage. The PipelineContextInterface type provides a shouldHalt() method which can be used to halt pipeline execution.

Usage

use Kaiju\Pipeline\PipelineBuilder;
use Kaiju\Pipeline\PipelineContextInterface;
use Kaiju\Pipeline\PipelineStageInterface;

class Payload implements PipelineContextInterface
{
    private int $value;
    private bool $halt;

    public function __construct(int $value)
    {
        $this->value = $value;
        $this->halt = false;
    }

    public function shouldHalt(): bool
    {
        return $this->halt;
    }

    public function setHalt(bool $halt): void
    {
        $this->halt = $halt;
    }

    public function getPayload(): int
    {
        return $this->value;
    }

    public function setPayload($payload): void
    {
        $this->value = (int) $payload;
    }
}

class TimesTwoStage implements PipelineStageInterface
{
    public function process(PipelineContextInterface $context): void
    {
        $val = (int) $context->getPayload();
        $val *= 2;
        $context->setPayload($val);
    }

    public function id(): string
    {
        return __CLASS__;
    }
}

class AddOneStage implements PipelineStageInterface
{
    public function process(PipelineContextInterface $context): void
    {
        $val = (int) $context->getPayload();
        $val++;
        $context->setPayload($val);
        $context->setHalt(true);
    }

    public function id(): string
    {
        return __CLASS__;
    }
}

$payload = new Payload(10);
$builder = (new PipelineBuilder())
    ->addStage(new TimesTwoStage())
    ->addStage(new AddOneStage());
$pipeline = $builder->build();
$pipeline($payload);
echo 'Value is: ' . $payload->getPayload();

Check examples directory for more.

统计信息

  • 总下载量: 331
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2022-03-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固