Skip to content

Instantly share code, notes, and snippets.

@laracasts
Last active December 29, 2015 05:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save laracasts/6527c6009072c837a77c to your computer and use it in GitHub Desktop.
Save laracasts/6527c6009072c837a77c to your computer and use it in GitHub Desktop.
<?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