kampernet/action-chain

There is no license information available for the latest version (1.0.2) of this package.

Action chain helper classes for implementing commands

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

This is a simple, dependency free implementation of the Command Pattern in PHP. Useful for creating an orchestration layer

Usage

This will create a group of Commands with a certain name.

    class RegisterAction extends ActionChain {

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

    }

These are the individual command examples

    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
        }
    }

Once your commands are built, you can just call them from the routes: ( example using Symfony's Request )

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

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

        return $response;
    });