pwm/s-flow 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

pwm/s-flow

Composer 安装命令:

composer require pwm/s-flow

包简介

A lightweight library for defining state machines

README 文档

README

Build Status codecov Maintainability Test Coverage License: MIT

S-Flow is a lightweight library for defining finite state machines (FSM). Once defined the machine can be run by giving it a start state and a sequence of events to derive some end state. One of the main design goals of S-Flow was to be able to define FSMs declaratively as a single top level definition. This makes the structure of the underlying graph clear and explicit which in turn helps with understanding and maintenance. S-Flow can be used for many things, eg. to define workflows or to build event sourced systems.

Table of Contents

Why

If you ever named a variable, object property or database field "status" or "state" then read on...

Claim #1:

Much grief in software development arises from our inability to control state.

Evidence:

Q: What do we do when our code breaks?

A: We debug it.

Q: What does debugging mean?

A: Observing our program's internal state trying to figure out where it went wrong.

Claim #2:

If we could better control state in our programs we would have less bugs and as a result we would spend less time debugging.

S-Flow can help controlling state by making it easy to build state machines.

Requirements

PHP 7.2+

Installation

$ composer require pwm/s-flow

Usage

There is a fully worked example under tests/unit/ShoppingCart that simulates the process of purchasing items from an imaginary shop. Below is the definition of the FSM from it:

// S, E and T are short for State, Event and Transition

// A list of state names that identify the states
$stateNames = [
    S\NoItems::name(),
    S\HasItems::name(),
    S\NoCard::name(),
    S\CardSelected::name(),
    S\CardConfirmed::name(),
    S\OrderPlaced::name(),
];

// A list of arrows labelled by event names
// An arrow goes from a start state via a transition to an end state
$arrows = [
    (new Arrow(E\Select::name()))->from(S\NoItems::name())->via(new T\AddFirstItem),
    (new Arrow(E\Select::name()))->from(S\HasItems::name())->via(new T\AddItem),
    (new Arrow(E\Checkout::name()))->from(S\HasItems::name())->via(new T\DoCheckout),
    (new Arrow(E\SelectCard::name()))->from(S\NoCard::name())->via(new T\DoSelectCard),
    (new Arrow(E\Confirm::name()))->from(S\CardSelected::name())->via(new T\ConfirmCard),
    (new Arrow(E\PlaceOrder::name()))->from(S\CardConfirmed::name())->via(new T\DoPlaceOrder),
    (new Arrow(E\Cancel::name()))->from(S\NoCard::name())->via(new T\DoCancel),
    (new Arrow(E\Cancel::name()))->from(S\CardSelected::name())->via(new T\DoCancel),
    (new Arrow(E\Cancel::name()))->from(S\CardConfirmed::name())->via(new T\DoCancel),
];

// Build a graph from the above
$graph = (new Graph(...$stateNames))->drawArrows(...$arrows);

// Build an FSM using the graph
$shoppingCartFSM = new FSM($graph);

// Run a simulation of purchasing 3 items
$result = $shoppingCartFSM->run(
    new S\NoItems,
    new Events(
        new E\Select(new Item('foo')),
        new E\Select(new Item('bar')),
        new E\Select(new Item('baz')),
        new E\Checkout,
        new E\SelectCard(new Card('Visa', '1234567812345678')),
        new E\Confirm,
        new E\PlaceOrder
    )
);

// Observe the results
assert($result->isSuccess() === true);
assert($result->getState() instanceof S\OrderPlaced);
assert($result->getLastEvent() instanceof E\PlaceOrder);

How it works

A state machine is defined as a directed graph. Vertices of this graph are called states and arrows between them are called transitions. Transitions are labelled so that they can be identified. We call those labels events.

Running the machine, ie. deriving an end state given a start state and a sequence of events, means walking the graph from the start state via a sequence of transitions leading to the desired end state. In the end we either reach it or stop when there is no way forward.

Transitions, acting as the arrows of the graph, are functions of type (State, Event) -> State. They are uniquely identified by a (StateName, EventName) pair, ie. given a state name and an event name (which is the label of the arrow) we can get the corresponding transition function, if it exists. The the absence of the transition function automatically results in a failed transition, keeping the current state.

Success and failure is captured using the TransitionOp type. It also keeps track of the current state as well as the sequence of events leading up to it.

Tests

$ composer phpunit
$ composer phpcs
$ composer phpstan
$ composer psalm
$ composer infection

Todo

Once return type covariance lands in PHP (as part of this RFC) we will be able to specify the actual return type of __invoke in Transition implementations. This would enable to easily dump the FSM into various text formats, eg. as a DOT file, etc...

Changelog

Click here

Licence

MIT

pwm/s-flow 适用场景与选型建议

pwm/s-flow 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.49k 次下载、GitHub Stars 达 74, 最近一次更新时间为 2018 年 05 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「workflow」 「fsm」 「state-machine」 「event-sourcing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 pwm/s-flow 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pwm/s-flow 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.49k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 74
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 74
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-05-18