stiggg/command-dispatcher

Command dispatcher library

v1.0 2014-07-13 11:38 UTC

This package is not auto-updated.

Last update: 2020-04-27 08:52:49 UTC


README

Command dispatcher/invoker library supporting:

  • pre- and post-command handlers
  • undoable commands
  • chainable commands
  • command queueing
  • command history

Dispatcher classes:

  • Dispatcher (basic dispatcher)
  • QueueDispatcher
  • ChainDispatcher

Motivation

Basic idea behind command dispatcher library is to abstract program logic into separate classes, with logic expressed by class names derived from domain language.

Benefits include:

  • Proper use of domain language describes what the code should do, so it communicates it's intent better for all interested parties
  • Common supporting tasks like logging for each command can be easily added and abstracted away into command classes, so that they do not clutter the main program code
  • Easy unit testing of command logic

Usage

Basic dispatcher

class SomeCommand extends AbstractCommand implements CommandInterface
{
    public function __construct($param1, $param2)
    {
        // add dependencies, construct command "environment"
    }

    public function execute()
    {
        // command logic added here
    }
}

$dispatcher = new Dispatcher();
$dispatcher->handle(
    SomeCommand::create(array($param1, $param2))
);

Dispatch and log same command twice with different contructor parameter:

class SomeCommand extends AbstractCommand implements CommandInterface
{
    public function __construct($parameter)
    {
        // ...
    }

    public function execute()
    {
        //...
    }
    
    public function log($logger)
    {
        // use logger
    }
}

$dispatcher = new Dispatcher();
$dispatcher->addPostCommandHandler(function($command) use ($logger) {
    $command->log($logger);
});
$dispatcher->handleCommands(array(
    SomeCommand::create(array($oneValue)),
    SomeCommand::create(array($anotherValue)),
));

See basic dispatcher example.

Chain dispatcher

With Chain dispatcher, each command takes previous command's output as execute() -method's input:

class FirstCommand extends AbstractCommand implements ChainableCommandInterface
{
    public function execute(array $input)
    {
        //...
    }
}

class SecondCommand extends AbstractCommand implements ChainableCommandInterface
{
    public function execute(array $input)
    {
        //...
    }
}

$dispatcher = new ChainDispatcher();

$firstCommandInput = 'wow';
// $firstCommandInput is FirstCommand's input, $output is SecondCommand's return value
$output = $dispatcher->handleCommands(array(
    FirstCommand::create(),
    SecondCommand::create()
), $firstCommandInput);

See chain dispatcher example.

Installation

To add command-dispatcher library to Composer, see packagist for version information: https://packagist.org/packages/stiggg/command-dispatcher

Requirements

Licence

MIT

Version history

Versioning supports semver: http://semver.org/

See version history