Skip to content

TomWright/Commander

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Commander

Build Status Latest Stable Version Total Downloads Monthly Downloads Daily Downloads License

Usage

You need a Command and a CommandHandler.

Let's say we have a command class stored in app/commanding/command/RegisterUserCommand.php.

namespace App\Commanding\Command;

class RegisterUserCommand extends \TomWright\Commander\Command\Command
{
	protected $username;
    protected $password;
    
    public function setUsername($username)
    {
    	$this->username = $username;
    }
    
    public function getUsername()
    {
    	return $this->username;
    }
    
    public function setPassword($password)
    {
        $this->password = $password;
    }
    
    public function getPassword()
    {
        return $this->password;
    }
}

Let's also assume we have a command handler class stored in app/commanding/handler/RegisterUserHandler.php.

namespace App\Commanding\Handler;

class RegisterUserHandler implements \TomWright\Commander\Handler\HandlerInterface
{   
    public function handle(\TomWright\Commander\Command\CommandInterface $command)
    {
    	echo "Registering user \"{$command->getUsername()}\" with password \"{$command->getPassword()}\".";
    }
}

Now we need to add a Command Handler namespace so as the CommandBus knows where to look for the handlers.

$bus = \TomWright\Commander\CommandBus::getInstance();
$bus->addHandlerNamespace('\\App\\Commanding\\Handler');

Now whenever we want to register a new user, all we have to do is the following:

$bus = \TomWright\Commander\CommandBus::getInstance();
$command = new \App\Commanding\Command\RegisterUserCommand();
$command->setUsername('Some user');
$command->setPassword('Somepassword123');
$bus->handle($command);

Remember - a Command is an action and not a notification. If you are looking for a notification/event handler see TomWright/Eventing.

About

A PHP implementation of the Command Bus Design Pattern.

Resources

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages