Skip to content

Implementation of the Command Pattern that allows for single commands as well as chains of commands. Undo / rollback is supported in both single commands and chains of commands

License

Notifications You must be signed in to change notification settings

kampernet/action-chain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kampernet Actions

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

About

Implementation of the Command Pattern that allows for single commands as well as chains of commands. Undo / rollback is supported in both single commands and chains of commands

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages