kampernet/action-chain

此包最新版本(1.0.2)没有可用的许可信息。

实现命令的命令链助手类

1.0.2 2021-02-11 20:49 UTC

This package is not auto-updated.

Last update: 2024-09-25 12:14:51 UTC


README

这是一个简单的、无依赖的PHP命令模式的实现。适用于创建编排层

用法

这将创建一个具有特定名称的命令组。

    class RegisterAction extends ActionChain {

        public function __construct() {
            $this->add(new RegisterUserAction());
            $this->add(new SendWelcomeEmailAction());
        }

    }

以下是一些单独的命令示例

    class RegisterUserAction extends Command {

        private $request;
        private $response;

        public function execute($request, $response = null) {

            $this->request = $request;
            $this->response = $response;

            // eg: code to take the request data and persist the user
            // probably more likely to call a service or domain model method here.
        }

        public function undo() {

            // eg: code to "undo" anything this command would have done
        }
    }

一旦构建了命令,您就可以从路由中调用它们:(使用Symfony的请求的示例)

    Route::get('/register', function() {

        $response = new Response();
        $reg = new RegisterAction();
        if (!$reg->execute(Request::createFromGlobals(), $response)) {
            $reg->undo();
        }

        return $response;
    });