PHP 中的用户状态工作流引擎

发布于 2024-12-08 06:09:51 字数 856 浏览 0 评论 0原文

我希望在 PHP 应用程序中实现用户状态/工作流程处理。

目前有:

  • 基于用户的系统,具有大约 10 种不同的状态,任何用户都可以处于(启用、禁用、预注册、取消注册、删除等)
  • 定义的规则,关于从哪个状态到哪个状态用户可以根据不同的系统事件进入
  • 一个相当混乱(但大部分正常工作)的系统,其中充满了分散在各个地方的 IF 和 SWITCH

想要:

  • 用“聪明”的状态替换当前的 IFfy 用户状态处理机器允许定义这些用户转换规则,
  • 允许可视化这些定义的规则
  • 通过确保用户只能处于合法状态来使系统更加安全和防弹

我的研究:

我检查了 SO 和其他地方的工作流程和状态机的 PHP 实现,有希望的候选者似乎是

我将不胜感激关于任何工作经验的评论上面的任何一个库和/或关于是否适合我需要的意见或关于其他地方寻找的提示。

I'm looking to implement user state / workflow handling in a PHP application.

Currently have:

  • a user based system with around 10 different states any user can be in (enabled, disabled, pre-registered, de-registered, deleted etc.)
  • defined rules regarding from which state to which a user can go based on different system events
  • a rather messy (but mostly working) system full of IFs and SWITCHes scattered all across the place

Want to:

  • replace the current IFfy user state handling with a "clever" state machine that would allow to define those user transition rules
  • allow to visualise those defined rules
  • make the system more secure and bullet proof by making sure the user can only be in legal states

My research:

I checked both SO and other places for a PHP implementation of workflow and state machines and promising candidates seem to be

I'd be grateful for any comments regarding any experience with working with either of the libraries above and / or opinion regarding suitability for what I need or for hints to other places where to look to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘世孤行 2024-12-15 06:09:51

根据您的状态设置方式,听起来您可以只设置一个带有工厂的类系统来优雅地处理所有这些?

您还可以使用状态检查来设置类,这可能会引发异常,并且基本上无法实例化该类(因此无法进入该状态)。

我想这样的东西可能对你有用:

class StateFactory {
    $currentState;

    function __construct(){
        if(!isset($_SESSION['currentState'])){
            $this->currentState = 'StateOne';
        }
        else{
            $this->currentState = $_SESSION['currentState'];
        }

        $this->currentState = new {$this->currentState}->processState(); // I think something like this will work
    }

    function __deconstruct(){
        $_SESSION['currentState'] = $this->currentState;
    }
}

abstract class State{
    abstract function processState();
}

class StateOne extends State{
    function processState(){
        if(<check what is needed for this state>){
            <do what you need to do for this state>
            return 'StateTwo';
        }
        else
        {
            return 'StateWhatever';
        }
    }
}    

class StateTwo extends State{
    function processState(){
        if(<check what is needed for this state>){
            <do what you need to do for this state>
            return 'StateThree';
        }
        else
        {
            return 'StateWhatever';
        }
    }
}    

class StateThree extends State{
    ...
}

显然,这缺少了很多东西,需要做很多事情才能使它成为你可以实际使用的东西,但如果你像这样把东西分开,它就不会很混乱,您将能够知道正在检查每个状态的位置以及正在检查的内容。

Depending on how your states are setup, sounds like you could just setup a class system with a factory to handle all of this elegantly?

You could also setup your classes with state checking, which you could throw exceptions and basically make it impossible to instantiate the class (and therefore impossible to enter that state).

I'm thinking something like this might work for you:

class StateFactory {
    $currentState;

    function __construct(){
        if(!isset($_SESSION['currentState'])){
            $this->currentState = 'StateOne';
        }
        else{
            $this->currentState = $_SESSION['currentState'];
        }

        $this->currentState = new {$this->currentState}->processState(); // I think something like this will work
    }

    function __deconstruct(){
        $_SESSION['currentState'] = $this->currentState;
    }
}

abstract class State{
    abstract function processState();
}

class StateOne extends State{
    function processState(){
        if(<check what is needed for this state>){
            <do what you need to do for this state>
            return 'StateTwo';
        }
        else
        {
            return 'StateWhatever';
        }
    }
}    

class StateTwo extends State{
    function processState(){
        if(<check what is needed for this state>){
            <do what you need to do for this state>
            return 'StateThree';
        }
        else
        {
            return 'StateWhatever';
        }
    }
}    

class StateThree extends State{
    ...
}

Obviously a lot is missing from this and a lot needs to be done to make this into something you can actually work with, but if you split things up like this, it will not be as messy and you'll be able to know where each state is being checked and what is being checked for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文