PHP code example of vanilo / workflow
1. Go to this page and download the library: Download vanilo/workflow library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vanilo / workflow example snippets
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
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));
}
}