承接 libriciel/cakephp-state-machine 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

libriciel/cakephp-state-machine

Composer 安装命令:

composer require libriciel/cakephp-state-machine

包简介

StateMachineBehavior for CakePHP 3

README 文档

README

Build Status Coverage Status Scrutinizer Quality Score Latest Stable Version Total Downloads

Documentation is not finished yet either. See the tests if you want to learn something, as all aspects of the state machine is tested there.

What is a State Machine?

http://en.wikipedia.org/wiki/State_machine

Installation

First you need to alter the tables of the models you want to use StateMachine:

ALTER TABLE `vehicles` ADD `state` VARCHAR(50);
ALTER TABLE `vehicles` ADD `previous_state` VARCHAR(50);

Features

  • Callbacks on states and transitions
  • Custom methods may be added to your model
  • is($entity, $state), can($entity, $transition), on($transition, 'before|after', callback) and when($state, callback) methods allows you to control the whole flow. transition($entity, $transition) is used to move between two states.
  • Graphviz
  • (removed: Roles and rules, feel free to commit it)

Callbacks

You can add callbacks that will fire before/after a transition, and before/after a state change. This can either be done manually with $this->on('mytransition', 'before', funtion() {}), or you can add a method to your model:

public function onBeforeTransition($entity, $currentState, $previousState, $transition) {
    // will fire on all transitions
}

public function onAfterIgnite($entity, $currentState, $previousState, $transition) {
    // will fire after the ignite transition
}

The state callbacks are a little different:

public function onStateChange($entity, $newState) {
    // will fire on all state changes
}

public function onStateIdling($entity, $newState) {
    // will fire on the idling state
}

Naming conventions

  • Transitions and states in $transitions should be lowercased and underscored. The method names are in turn camelized.

    Example:

    shift_up   => can($entity, 'ShiftUp') => transition($entity, 'ShiftUp')
    first_gear => is($entity, 'FirstGear')

How to Use

namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\ORM\Entity;

class VehiclesTable extends Table
{
    public function initialize(array $config)
    {
        $this->addBehavior('StateMachine.StateMachine');
        $this->on('ignite', 'after', function(Entity $entity, $prevState, $nextState, $transition) {
            // the car just ignited!
        });
    }

    public $initialState = 'parked';

    public $transitions = array(
            'ignite' => array(
                    'parked' => 'idling',
                    'stalled' => 'stalled'
            ),
            'park' => array(
                    'idling' => 'parked',
                    'first_gear' => 'parked'
            ),
            'shift_up' => array(
                    'idling' => 'first_gear',
                    'first_gear' => 'second_gear',
                    'second_gear' => 'third_gear'
            ),
            'shift_down' => array(
                    'first_gear' => 'idling',
                    'second_gear' => 'first_gear',
                    'third_gear' => 'second_gear'
            ),
            'crash' => array(
                    'first_gear' => 'stalled',
                    'second_gear' => 'stalled',
                    'third_gear' => 'stalled'
            ),
            'repair' => array(
                    'stalled' => 'parked'
            ),
            'idle' => array(
                    'first_gear' => 'idling'
            ),
            'turn_off' => array(
                    'all' => 'parked'
            )
    );

    // a shortcut method for checking if the vehicle is moving
    public function isMoving(Entity $entity) {
        return in_array($this->getCurrentState($entity), array('first_gear', 'second_gear', 'third_gear'));
    }

}
class Controller .... {
    public function method() {
        $this->loadModel('Vehicles');
        $entity = $this->Vehicles->newEntity();
        $entity->title = 'Toybota';
        $this->Vehicles->save($entity);

        // $this->Vehicles->getCurrentState($entity) == 'parked'
	if ($this->Vehicles->can($entity, 'Ignite')) {
       	 	$this->Vehicles->transition($entity, 'Ignite');
       	 	$this->Vehicles->transition($entity, 'shiftUp');
        	// $this->Vehicles->getCurrentState($entity) == 'first_gear'
        }
    }
}

Graphviz

Here's how to state machine of the Vehicle would look like if you saved:

$table->toDot()

into fsm.gv and ran:

dot -Tpng -ofsm.png fsm.gv

libriciel/cakephp-state-machine 适用场景与选型建议

libriciel/cakephp-state-machine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.81k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 libriciel/cakephp-state-machine 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-24