定制 webnarmin/automata 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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/automata instead:

composer require chatflowphp/automata

CI PHPStan PHPUnit License

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 calls onEnter()
  • tick() runs middleware in registration order and then calls process() on the active automaton
  • Transition commands call onLeave() on the current automaton and onEnter() on the next one
  • Self-transition is a no-op for lifecycle hooks
  • AutomatonActivatedEvent is emitted after activation
  • TransitionAppliedEvent is emitted after a real transition changes the active automaton

Use the introspection helpers when needed:

  • getActiveAutomatonId(): ?string
  • hasActiveAutomaton(): bool
  • hasAutomaton(string $id): bool

Transition And Dispatch Semantics

For each completed tick() the orchestrator applies results in this order:

  1. Process all commands returned by CycleResponse
  2. Apply transitions before publishing transition commands to the message bus
  3. Dispatch lifecycle events for applied transitions and activation
  4. Dispatch domain events from CycleResponse
  5. Dispatch CycleCompletedEvent last

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:

  • null
  • bool
  • int
  • float
  • string
  • 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

  • CycleMiddlewareInterface for per-tick cross-cutting behavior
  • MessageBusInterface for custom message delivery
  • SerializableAutomatonInterface for automata that need snapshot support
  • SnapshotSerializerInterface for custom snapshot wire formats
  • TransitionCommandInterface for 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 webnarmin/automata 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-10