Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// ...
/**
* Register a decorator for the command bus.
*
* @param string $decorator
* @return CommandBus
*/
public function decorate($decorator)
{
$this->decorators[] = $decorator;
return $this;
}
/**
* Execute the command
*
* @param $command
* @return mixed
*/
public function execute($command)
{
$this->executeBusDecorators($command);
$handler = $this->commandNameTranslator->toHandler($command);
return $this->app->make($handler)->handle($command);
}
/**
* Execute any provided decorators.
*
* @param object $command
* @return null
*/
protected function executeBusDecorators($command)
{
foreach ($this->decorators as $decorator)
{
$this->app->make($decorator)->execute($command);
}
}
<?php
// This will:
- Channel the command through an input sanitizer
- Then on to a validator
- Then on to the normal processing, where it will trigger a handler to actually process the command.
$this->bus
->decorate('Foo\Bar\RegistrationFormSanitizer')
->decorate('Foo\Bar\RegistrationFormValidator')
->execute($registerUserCommand);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.