particle/state 问题修复 & 功能扩展

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

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

particle/state

Composer 安装命令:

composer require particle/state

包简介

A rather simplistic but pretty finite state machine implementation

README 文档

README

Particle\State is a very small and simple library, implementing a finite state machine. You can use it in your application whenever you need to account for transitions between multiple states (and that's probably more often than you would imagine!).

Why?

With winzou/statemachine and yohang/finite already in Packagist, why would we go for another package implementing the very same concept?

Well, first of all, Particle\State is very small in comparison, doing next to nothing. That means everything it does do is easy to follow, without having to wade through lots of code. Also, by doing only one thing, it's rather easy to learn how to use this library.

On top of that, we wanted to have a library that is not obtrusive: you can implement Particle\State without having to model your objects in a certain way. Indeed, Particle\State does not have any interface you need to implement.

Nevertheless, check them out. Maybe they'll serve you better.

Small usage example

$initialState = State::withName('pending');

$machine = StateMachine::withStates($initialState, StateCollection::withStates([
    State::withName('pending'),
    State::withName('started'),
    State::withName('accepted'),
    State::withName('rejected'),
    State::withName('completed'),
    State::withName('failed'),
]));

$machine->addTransition(Transition::withStates('start', ['pending'], 'started'));
$machine->addTransition(Transition::withStates('accept', ['started'], 'accepted'));

$machine->addListener(function (Event\TransitionApplied $event) {
    if ($event->wasFromState(State:withName('pending')) {
        echo "The state was pending. Not anymore though :)";
    }
});

var_dump($machine->canApplyTransition('accept')); // bool(false), because not in 'started' state;
var_dump($machine->canApplyTransition('start')); // bool(true);
var_dump($machine->transition('start')); // bool(true);
var_dump($machine->canApplyTransition('accept')); // bool(true);
var_dump($machine->canApplyTransition('start')); // bool(false);

Functional features

  • Validate if a transition of state is possible;
  • Transition the state to a next state;
  • Hook event listeners to state changes as transition callbacks.

Non functional features

  • Easy to write (IDE auto-completion for easy development)
  • Easy to read (improves peer review)

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD
  • 更新时间: 2016-04-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固