sbooker/workflow

Simple workflow for FSM implementation

1.1.0 2021-02-02 10:04 UTC

This package is auto-updated.

Last update: 2021-12-30 12:09:33 UTC


README

Latest Version Software License PHP Version Total Downloads

Installation

pecl install ds
composer require sbooker/workflow

Usage

// Step 1. Create Status Enum

use Sbooker\Workflow\Status;

final class ConcreteStatus extends Status
{
    // define enum, see litgroup/enumerable
    public static function first(): self { 
    // ... 
    }

    public static function second(): self { 
    // ... 
    }

}

// Step 2. Define workflow

use Sbooker\Workflow\Workflow;
use Ds\Map;
use Ds\Set;

final class ConcreteWorkflow extends Workflow
{
    public function __construct()
    {
        parent::__construct(ConcreteStatus::first());
    }


    protected function buildTransitionMap(): Map
    {
        $map = new Map();
        $map->put(ConcreteStatus::first(), new Set([ConcreteStatus::second()]));
        return $map;
    }

    protected function getStatusClass(): string
    {
        return ConcreteStatus::class;
    }    
}

// Step 3. Use workflow in your entity for state control
class ConcreteEntity {
    // ...
    private ConcreteWorkflow $workflow;
    // ...

    public function __construct() {
        // ...
        $this->workflow = new ConcreteWorkflow();
        // ...
    }
    
    public function doSecond(): void {
        // ...
        $this->workflow->transitTo(ConcreteStatus::second());
        // ...
    }
} 

License

See LICENSE file.