From c5a8d2831aebc4ebb987ce6ca80f3898daed8e7a Mon Sep 17 00:00:00 2001 From: aarongustafson Date: Wed, 13 Aug 2014 14:55:45 -0400 Subject: [PATCH 1/5] =?UTF-8?q?Making=20sure=20decorators=20don=E2=80=99t?= =?UTF-8?q?=20bleed=20from=20command=20to=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Laracasts/Commander/CommanderTrait.php | 11 +++++++++-- src/Laracasts/Commander/DefaultCommandBus.php | 11 +++++++++++ src/Laracasts/Commander/ValidationCommandBus.php | 11 +++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Laracasts/Commander/CommanderTrait.php b/src/Laracasts/Commander/CommanderTrait.php index 8a328f7..f464b01 100644 --- a/src/Laracasts/Commander/CommanderTrait.php +++ b/src/Laracasts/Commander/CommanderTrait.php @@ -26,9 +26,16 @@ public function execute($command, array $input = null, $decorators = []) // filter through and register them // with the CommandBus, so that they // are executed first. - foreach ($decorators as $decorator) + if (count($decorators)) { - $bus->decorate($decorator); + foreach ($decorators as $decorator) + { + $bus->decorate($decorator); + } + } + else + { + $bus->undecorate(); } return $bus->execute($command); diff --git a/src/Laracasts/Commander/DefaultCommandBus.php b/src/Laracasts/Commander/DefaultCommandBus.php index 79f8e1c..987c5bd 100644 --- a/src/Laracasts/Commander/DefaultCommandBus.php +++ b/src/Laracasts/Commander/DefaultCommandBus.php @@ -43,6 +43,17 @@ public function decorate($className) $this->decorators[] = $className; } + /** + * Remove decorations from the command bus + * + * @param $className + * @return null + */ + public function undecorate() + { + $this->decorators = []; + } + /** * Execute the command * diff --git a/src/Laracasts/Commander/ValidationCommandBus.php b/src/Laracasts/Commander/ValidationCommandBus.php index d33ddd9..59275ca 100644 --- a/src/Laracasts/Commander/ValidationCommandBus.php +++ b/src/Laracasts/Commander/ValidationCommandBus.php @@ -46,6 +46,17 @@ public function decorate($className) } /** + * Remove decorations from the command bus + * + * @param $className + * @return null + */ + public function undecorate() + { + $this->decorators = []; + } + + /** * Execute a command with validation. * * @param $command From d88d8fff1917aca9d0a6cd98bd25efe2a7faa0f4 Mon Sep 17 00:00:00 2001 From: Aaron Gustafson Date: Sun, 7 Dec 2014 11:00:11 -0500 Subject: [PATCH 2/5] Changing the name of the method to `clearDecorators` --- src/Laracasts/Commander/DefaultCommandBus.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Laracasts/Commander/DefaultCommandBus.php b/src/Laracasts/Commander/DefaultCommandBus.php index 987c5bd..ba60d8c 100644 --- a/src/Laracasts/Commander/DefaultCommandBus.php +++ b/src/Laracasts/Commander/DefaultCommandBus.php @@ -49,7 +49,7 @@ public function decorate($className) * @param $className * @return null */ - public function undecorate() + public function clearDecorators() { $this->decorators = []; } @@ -92,4 +92,4 @@ protected function executeDecorators($command) } } -} \ No newline at end of file +} From 9ddcd08a7769756a3a78219846f0d38957a328be Mon Sep 17 00:00:00 2001 From: Aaron Gustafson Date: Sun, 7 Dec 2014 11:00:55 -0500 Subject: [PATCH 3/5] Changing the method use for `clearDecorators` --- src/Laracasts/Commander/CommanderTrait.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Laracasts/Commander/CommanderTrait.php b/src/Laracasts/Commander/CommanderTrait.php index f464b01..ee167de 100644 --- a/src/Laracasts/Commander/CommanderTrait.php +++ b/src/Laracasts/Commander/CommanderTrait.php @@ -26,7 +26,7 @@ public function execute($command, array $input = null, $decorators = []) // filter through and register them // with the CommandBus, so that they // are executed first. - if (count($decorators)) + if ($decorators) { foreach ($decorators as $decorator) { @@ -35,7 +35,7 @@ public function execute($command, array $input = null, $decorators = []) } else { - $bus->undecorate(); + $bus->clearDecorators(); } return $bus->execute($command); @@ -88,4 +88,4 @@ protected function mapInputToCommand($command, array $input) return $class->newInstanceArgs($dependencies); } -} \ No newline at end of file +} From 50ce155f12685439857f1780a13c5a5fcf5133bc Mon Sep 17 00:00:00 2001 From: Aaron Gustafson Date: Sun, 7 Dec 2014 11:01:37 -0500 Subject: [PATCH 4/5] Changing the name of the method to `clearDecorators` --- .../Commander/ValidationCommandBus.php | 43 +++++-------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/src/Laracasts/Commander/ValidationCommandBus.php b/src/Laracasts/Commander/ValidationCommandBus.php index 59275ca..ba60d8c 100644 --- a/src/Laracasts/Commander/ValidationCommandBus.php +++ b/src/Laracasts/Commander/ValidationCommandBus.php @@ -3,12 +3,7 @@ use Illuminate\Foundation\Application; use InvalidArgumentException; -class ValidationCommandBus implements CommandBus { - - /** - * @var CommandBus - */ - protected $bus; +class DefaultCommandBus implements CommandBus { /** * @var Application @@ -27,9 +22,12 @@ class ValidationCommandBus implements CommandBus { */ protected $decorators = []; - function __construct(CommandBus $bus, Application $app, CommandTranslator $commandTranslator) + /** + * @param Application $app + * @param CommandTranslator $commandTranslator + */ + function __construct(Application $app, CommandTranslator $commandTranslator) { - $this->bus = $bus; $this->app = $app; $this->commandTranslator = $commandTranslator; } @@ -51,43 +49,24 @@ public function decorate($className) * @param $className * @return null */ - public function undecorate() + public function clearDecorators() { $this->decorators = []; } - /** - * Execute a command with validation. + /** + * Execute the command * * @param $command * @return mixed */ public function execute($command) { - // If a validator is "registered," we will - // first trigger it, before moving forward. - $this->validateCommand($command); - - // Next, we'll execute any registered decorators. $this->executeDecorators($command); - // And finally pass through to the handler class. - return $this->bus->execute($command); - } - - /** - * If appropriate, validate command data. - * - * @param $command - */ - protected function validateCommand($command) - { - $validator = $this->commandTranslator->toValidator($command); + $handler = $this->commandTranslator->toCommandHandler($command); - if (class_exists($validator)) - { - $this->app->make($validator)->validate($command); - } + return $this->app->make($handler)->handle($command); } /** From a09a81d1b9d151624e40fc7fac9ccd0cf80feb1e Mon Sep 17 00:00:00 2001 From: Aaron Gustafson Date: Sun, 7 Dec 2014 11:07:12 -0500 Subject: [PATCH 5/5] Not sure what happened there. Fixing it. --- .../Commander/ValidationCommandBus.php | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/Laracasts/Commander/ValidationCommandBus.php b/src/Laracasts/Commander/ValidationCommandBus.php index ba60d8c..8dcfb45 100644 --- a/src/Laracasts/Commander/ValidationCommandBus.php +++ b/src/Laracasts/Commander/ValidationCommandBus.php @@ -3,7 +3,12 @@ use Illuminate\Foundation\Application; use InvalidArgumentException; -class DefaultCommandBus implements CommandBus { +class ValidationCommandBus implements CommandBus { + + /** + * @var CommandBus + */ + protected $bus; /** * @var Application @@ -22,12 +27,9 @@ class DefaultCommandBus implements CommandBus { */ protected $decorators = []; - /** - * @param Application $app - * @param CommandTranslator $commandTranslator - */ - function __construct(Application $app, CommandTranslator $commandTranslator) + function __construct(CommandBus $bus, Application $app, CommandTranslator $commandTranslator) { + $this->bus = $bus; $this->app = $app; $this->commandTranslator = $commandTranslator; } @@ -44,7 +46,7 @@ public function decorate($className) } /** - * Remove decorations from the command bus + * Remove decorations from the command bus * * @param $className * @return null @@ -55,18 +57,37 @@ public function clearDecorators() } /** - * Execute the command + * Execute a command with validation. * * @param $command * @return mixed */ public function execute($command) { + // If a validator is "registered," we will + // first trigger it, before moving forward. + $this->validateCommand($command); + + // Next, we'll execute any registered decorators. $this->executeDecorators($command); - $handler = $this->commandTranslator->toCommandHandler($command); + // And finally pass through to the handler class. + return $this->bus->execute($command); + } - return $this->app->make($handler)->handle($command); + /** + * If appropriate, validate command data. + * + * @param $command + */ + protected function validateCommand($command) + { + $validator = $this->commandTranslator->toValidator($command); + + if (class_exists($validator)) + { + $this->app->make($validator)->validate($command); + } } /**