webnarmin/automata
Composer 安装命令:
composer require webnarmin/automata
包简介
Framework-agnostic orchestration engine for single-active finite state machines in PHP.
README 文档
README
Archived package. This repository is no longer the primary distribution channel for Automata. Use
chatflowphp/automatainstead:composer require chatflowphp/automata
Framework-agnostic orchestration engine for single-active finite state machines in PHP.
What It Does
- Activates one automaton at a time and drives it through discrete
tick()cycles - Wraps each cycle with optional middleware
- Dispatches commands and events through a lightweight in-memory message bus
- Emits lifecycle events for activation and applied transitions
- Supports snapshot/restore for shared context and serializable automata state
What It Does Not Do
- No statechart semantics
- No parallel or multi-active automata runtime
- No built-in persistence transport or async messaging backend
Requirements
- PHP 8.1 or newer
- Composer
Installation
composer require chatflowphp/automata
Quick Start
<?php use Automata\Contracts\AutomatonInterface; use Automata\Contracts\ContextInterface; use Automata\Contracts\InputInterface; use Automata\Core\Context\ArrayContext; use Automata\Core\CycleRequest; use Automata\Core\CycleResponse; use Automata\Core\Orchestrator; $context = new ArrayContext(); $automaton = new class implements AutomatonInterface { public function getId(): string { return 'demo'; } public function onEnter(ContextInterface $context): void { $context->set('status', 'idle'); } public function process(CycleRequest $request): CycleResponse { $request->getContext()->set('status', 'processed'); return CycleResponse::none(); } public function onLeave(ContextInterface $context): void { } }; $orchestrator = new Orchestrator($context); $orchestrator->registerAutomaton($automaton); $orchestrator->activate('demo'); $orchestrator->tick(new class implements InputInterface {});
Lifecycle
activate()resolves the target automaton and callsonEnter()tick()runs middleware in registration order and then callsprocess()on the active automaton- Transition commands call
onLeave()on the current automaton andonEnter()on the next one - Self-transition is a no-op for lifecycle hooks
AutomatonActivatedEventis emitted after activationTransitionAppliedEventis emitted after a real transition changes the active automaton
Use the introspection helpers when needed:
getActiveAutomatonId(): ?stringhasActiveAutomaton(): boolhasAutomaton(string $id): bool
Transition And Dispatch Semantics
For each completed tick() the orchestrator applies results in this order:
- Process all commands returned by
CycleResponse - Apply transitions before publishing transition commands to the message bus
- Dispatch lifecycle events for applied transitions and activation
- Dispatch domain events from
CycleResponse - Dispatch
CycleCompletedEventlast
This means event listeners observe the final active automaton and final context state for the tick.
Snapshot Semantics
Capture state:
$snapshot = $orchestrator->snapshot();
Restore state:
$restoredContext = new ArrayContext(); $restoredOrchestrator = new Orchestrator($restoredContext); $restoredOrchestrator->registerAutomaton($automaton); $restoredOrchestrator->activateFromSnapshot($snapshot);
Serialize externally:
use Automata\Core\State\JsonSnapshotSerializer; $serializer = new JsonSnapshotSerializer(); $payload = $serializer->serialize($snapshot); $restored = $serializer->deserialize($payload);
Snapshot restore is strict:
- missing active automaton id causes an exception
- missing registered automata state handlers cause an exception
- invalid snapshot structure causes an exception
State Serialization Rules
Context state and serializable automata state must contain only:
nullboolintfloatstring- arrays composed from the same allowed values
BackedEnum, which is normalized to its scalar value
Non-backed enums and arbitrary objects are rejected.
Extension Points
CycleMiddlewareInterfacefor per-tick cross-cutting behaviorMessageBusInterfacefor custom message deliverySerializableAutomatonInterfacefor automata that need snapshot supportSnapshotSerializerInterfacefor custom snapshot wire formatsTransitionCommandInterfacefor custom transition commands
Traffic Light Example
The examples/traffic-light directory contains a CLI demo that shows:
- three automata connected by transitions
- middleware-driven tick counting
- direct domain-event listeners observing state after transitions
- snapshot save/restore through
StateSnapshot
Run it with:
php examples/traffic-light/run.php
Testing
composer test
Full local quality gate:
composer check
License
This project is released under the MIT License. See LICENSE for details.
webnarmin/automata 适用场景与选型建议
webnarmin/automata 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 10 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「workflow」 「fsm」 「automata」 「event-driven」 「state-machine」 「orchestrator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webnarmin/automata 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webnarmin/automata 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webnarmin/automata 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Workflow for NetCommons Plugin
FormalTheory is a library that allows for manipulation and conversion of NFAs, DFAs and Regular Expressions.
Finite state machine for php.
Workflow logger
A state Machine library for business processes
State machines for Eloquent models with Enums
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-10