tomwright / commander
A simple command bus.
Fund package maintenance!
TomWright
www.buymeacoffee.com/TomWright
Requires
This package is auto-updated.
Last update: 2024-11-15 07:11:25 UTC
README
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.