masroore/pipeline

Pipeline pattern implementation

0.1.1 2022-03-06 12:17 UTC

This package is auto-updated.

Last update: 2024-12-12 16:53:28 UTC


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.