定制 vanilo/workflow 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

vanilo/workflow

最新稳定版本:1.2.0

Composer 安装命令:

composer require vanilo/workflow

包简介

Workflow Engine / State Machine

README 文档

README

Tests Packagist Stable Version StyleCI Packagist downloads MIT Software License

This is a PHP engine that can take a subject (entity, document, db record) that has an enum property (either a native PHP enum or Konekt Enum) that represents the state of the subject.

The workflow is defined around the possible states (values of the enum).

Transitions can be defined from one state to another.

Example

class OrderWorkflow extends \Vanilo\Workflow\Draft
{
    private static string $enumClass = OrderStatus::class;
    private static string $property = 'status';

    private static array $graph = [
        'name' => 'Order Workflow',
        'transitions' => [
            'prepare' => [
                'from' => [OrderStatus::NEW, OrderStatus::PENDING],
                'to' => OrderStatus::PROCESSING,
            ],
            'ship' => [
                'from' => [OrderStatus::PROCESSING],
                'to' => OrderStatus::COMPLETED,
            ],
            'cancel' => [
                'from' => ['*'],
                'to' => OrderStatus::CANCELED,
            ],
        ],    
    ];
}

$order = Order::find(1);
echo $order->status->value;
// PROCESSING

$workflow = OrderWorkflow::for($order);
$workflow->can('prepare');
// false
$workflow->can('cancel');
// true

$workflow->allowedTransitions()
// ['ship, 'cancel']

$workflow->execute('ship');
echo $order->status->value;
// COMPLETED

Writing Explicit Transitions

By default, executing transitions will mutate the subject's state to the desired end state.

But it's also possible to explicitly define methods that will be called instead.

class OrderWorkflow extends \Vanilo\Workflow\Draft
{
    private static string $enumClass = OrderStatus::class;
    private static string $property = 'status';

    private static array $graph = [
        'transitions' => [
            'cancel' => [
                'from' => ['*'],
                'to' => OrderStatus::CANCELED,
            ],
        ],
    ];
    
    
    public function cancel(Order $order): void
    {
        foreach ($order->items as $item) {
            Inventory::release($item->sku, $item->quantity)
        }
    
        $order->status = OrderStatus::CANCELED;
        $orders->save();
        
        Event::dispatch(new OrderCanceled($order));  
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-14