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

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

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

carlescliment/state-machine

Composer 安装命令:

composer create-project carlescliment/state-machine

包简介

A simple finite state machine with minimal dependencies

README 文档

README

This is a simple, no-deps PHP state machine.

Installation

Update your composer.json file adding the following dependency and run php composer.phar update carlescliment/state-machine:

    "carlescliment/state-machine": "0.0.4"

Usage

Implementing a state machine is straight-forward. You need four components; a statable object, states, transitions and the state machine.

<?php

use carlescliment\StateMachine\Model\Statable,
    carlescliment\StateMachine\Model\StateMachine,
    carlescliment\StateMachine\Model\Transition;


class SemaphoreStates
{
    const OPEN = 'semaphore.open';
    const WARNING = 'semaphore.warning';
    const LOCKED = 'semaphore.locked';

    public static function all()
    {
        return array(self::OPEN, self::WARNING, self::LOCKED);
    }
}


class SemaphoreTransitions
{
    const GIVE_PASS = 'semaphore.give_pass';
    const WARN = 'semaphore.warn';
    const LOCK = 'semaphore.lock';
}


class Semaphore implements Statable
{
    private $state = SemaphoreStates::LOCKED;

    public function getState()
    {
        return $this->state;
    }

    public function setState($state)
    {
        $this->state = $state;
        return $this;
    }
}


class SemaphoreStateMachine extends StateMachine
{
    public function __construct()
    {
        parent::construct();

        foreach (SemaphoreStates::all() as $state) {
            $this->addState($state);
        }

        $transitions = array(
            new Transition(AuctionTransitions::GIVE_PASS, AuctionStates::LOCKED, AuctionStates::OPEN),
            new Transition(AuctionTransitions::GIVE_PASS, AuctionStates::WARNING, AuctionStates::OPEN),
            new Transition(AuctionTransitions::WARN, AuctionStates::OPEN, AuctionStates::WARNING),
            new Transition(AuctionTransitions::WARN, AuctionStates::LOCKED, AuctionStates::WARNING),
            new Transition(AuctionTransitions::LOCK, AuctionStates::OPEN, AuctionStates::LOCKED),
            new Transition(AuctionTransitions::LOCK, AuctionStates::WARNING, AuctionStates::LOCKED),
            );
        foreach ($transitions as $transition) {
            $this->addTransition($transition);
        }
    }
}

Now, you can operate with your state machine:

<?php

$semaphore = new Semaphore;

$machine = new SemaphoreStateMachine;
$machine->execute(SemaphoreTransitions::GIVE_PASS, $semaphore);
$machine->execute(SemaphoreTransitions::WARN, $semaphore);
$machine->execute(SemaphoreTransitions::LOCK, $semaphore);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-08-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固