jardissupport/workflow
最新稳定版本:v1.0.0
Composer 安装命令:
composer require jardissupport/workflow
包简介
Directed workflow engine with status-based transitions, automatic data accumulation, and builder API
README 文档
README
Part of the Jardis Business Platform — Enterprise-grade PHP components for Domain-Driven Design
Directed workflow engine for multi-step process orchestration. Define handler graphs with status-based transitions, propagate every step's result through a typed context, and wire it all up with a fluent builder API. Each handler returns a WorkflowResult that determines the next step.
Features
- Directed Handler Graph — connect handlers as nodes with explicit per-status transitions
- Status-Based Transitions —
onSuccess,onFail,onRetry,onSkip,onCancel,onTimeout,onError,onPending - Typed Execution Context —
WorkflowContextcarries every handler invocation as an entry in an ordered execution log;getPrevious()exposes the immediate predecessor's result without the handler needing to know who that was - Lossless History — re-invocations of the same handler (retry loops, cross-branch revisits) append a new entry instead of overwriting;
getAll(Foo::class)returns every invocation,getLatest(Foo::class)the most recent - Fluent Builder API —
WorkflowBuilder+WorkflowNodeBuilderwire the graph without configuration arrays - Handler Factory — inject a closure to resolve handlers from a DI container
- WorkflowResult — typed value object with status constants; no ambiguous truthy/falsy returns
Installation
composer require jardissupport/workflow
Quick Start
use JardisSupport\Workflow\Builder\WorkflowBuilder; use JardisSupport\Workflow\Workflow; use JardisSupport\Workflow\WorkflowResult; // Build a two-step graph $config = (new WorkflowBuilder()) ->node(ValidateOrderHandler::class) ->onSuccess(ChargePaymentHandler::class) ->onFail(RejectOrderHandler::class) ->node(ChargePaymentHandler::class) ->onSuccess(ConfirmOrderHandler::class) ->build(); // Workflow is stateless and single-shot. Per-run input is passed as $data and forwarded // to the handler factory — handlers themselves are invoked with the WorkflowContext only. $workflow = new Workflow( handlerFactory: fn(string $cls, mixed $data): object => new $cls($data), ); $context = $workflow($config, $order); // Inspect the final result and the full chain $lastResult = $context->getPrevious(); // WorkflowResult of last executed handler $chargeResult = $context->getLatest(ChargePaymentHandler::class); // most recent invocation of that handler $allCharges = $context->getAll(ChargePaymentHandler::class); // every invocation in execution order $executed = count($context->getChain()); // total number of handler invocations
Advanced Usage
use JardisSupport\Contract\Workflow\WorkflowContextInterface; use JardisSupport\Workflow\Builder\WorkflowBuilder; use JardisSupport\Workflow\Workflow; use JardisSupport\Workflow\WorkflowResult; // Handler using named transitions (retry loop). All handlers share the same signature: // __invoke(WorkflowContextInterface): WorkflowResultInterface — per-run input is wired in // by the handler factory (e.g. injected via constructor or set as the BoundedContext payload). class ChargePaymentHandler { public function __construct(private readonly Order $order) {} public function __invoke(WorkflowContextInterface $context): WorkflowResult { // Count prior invocations from the chain — every retry has a fresh entry $attempt = count($context->getAll(self::class)) + 1; $result = $this->gateway->charge($this->order->total); if ($result->isTemporaryFailure()) { return new WorkflowResult(WorkflowResult::ON_RETRY, ['attempt' => $attempt]); } if (!$result->isSuccess()) { return new WorkflowResult(WorkflowResult::STATUS_FAIL, ['error' => $result->message]); } return new WorkflowResult(WorkflowResult::STATUS_SUCCESS, ['chargeId' => $result->id]); } } // Wire retry back to the same handler $config = (new WorkflowBuilder()) ->node(ChargePaymentHandler::class) ->onSuccess(FulfillOrderHandler::class) ->onFail(NotifyFailureHandler::class) ->onRetry(ChargePaymentHandler::class) // loop back ->build(); // Inject handlers from a DI container; the factory receives both the FQCN and the // per-run $data passed to $workflow($config, $data). $workflow = new Workflow( fn(string $class, mixed $data): object => $container->get($class)->withOrder($data), ); $context = $workflow($config, $order); // Inspect the chain — flat ordered execution log; every entry is a stamped WorkflowResult foreach ($context->getChain() as $result) { echo "{$result->getHandlerFqcn()}: {$result->getStatus()}\n"; }
Documentation
Full documentation, guides, and API reference:
docs.jardis.io/en/support/workflow
License
This package is licensed under the MIT License.
Jardis · Documentation · Headgent
KI-gestützte Entwicklung
Dieses Package liefert einen Skill für Claude Code, Cursor, Continue und Aider mit. Installation im Konsumentenprojekt:
composer require --dev jardis/dev-skills
Mehr Details: https://docs.jardis.io/skills
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-14