1. Go to this page and download the library: Download rasuvaeff/yii3-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/ */
use Rasuvaeff\Yii3Workflow\WorkflowRegistry;
final readonly class ShipOrderHandler
{
public function __construct(private WorkflowRegistry $registry) {}
public function handle(Order $order, string $requestId): void
{
$workflow = $this->registry->get('order');
if (!$workflow->applyOnce($order, 'ship', idempotencyKey: $requestId)) {
return; // already applied for this request — nothing to do
}
$this->orders->save($order);
}
}
use Rasuvaeff\Yii3Workflow\TransitionGuard;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\TransitionBlocker;
final class ShipOnlyWhenPaid extends TransitionGuard
{
protected function workflow(): string
{
return 'order';
}
protected function transitions(): array
{
return ['ship']; // empty array = every transition of the workflow
}
protected function guard(GuardEvent $event): void
{
if (!$event->getSubject()->isPaid()) {
$event->addTransitionBlocker(new TransitionBlocker('Payment is not captured', 'order.unpaid'));
}
}
}
// config/common/di/workflow.php
use Rasuvaeff\Yii3Workflow\Audit\TransitionLog;
return [
TransitionLog::class => App\Infrastructure\DbTransitionLog::class,
];
bash
./yii workflow:dump # list configured workflows
./yii workflow:dump order # Mermaid (default)
./yii workflow:dump order --format=puml # PlantUML
./yii workflow:dump order --format=dot # Graphviz
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.