vanilo / workflow
Workflow Engine / State Machine
Fund package maintenance!
fulopattila122
Installs: 9 123
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- ext-json: *
- ext-pdo: *
- konekt/enum: ^3.1.1|^4.0
Requires (Dev)
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-11-03 11:49:08 UTC
README
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)); } }