diff --git a/src/AnnotatedCommand.php b/src/AnnotatedCommand.php index 8df1fa9..4a64951 100644 --- a/src/AnnotatedCommand.php +++ b/src/AnnotatedCommand.php @@ -6,6 +6,8 @@ use Consolidation\AnnotatedCommand\Hooks\HookManager; use Consolidation\AnnotatedCommand\Output\OutputAwareInterface; use Consolidation\AnnotatedCommand\Parser\CommandInfo; +use Consolidation\AnnotatedCommand\State\State; +use Consolidation\AnnotatedCommand\State\StateHelper; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputAwareInterface; @@ -276,21 +278,23 @@ public function optionsHookForHookAnnotations($commandInfoList) */ protected function interact(InputInterface $input, OutputInterface $output) { - $this->injectIntoCommandfileInstance($input, $output); + $state = $this->injectIntoCommandfileInstance($input, $output); $this->commandProcessor()->interact( $input, $output, $this->getNames(), $this->annotationData ); + $state->restore(); } protected function initialize(InputInterface $input, OutputInterface $output) { - $this->injectIntoCommandfileInstance($input, $output); + $state = $this->injectIntoCommandfileInstance($input, $output); // Allow the hook manager a chance to provide configuration values, // if there are any registered hooks to do that. $this->commandProcessor()->initializeHook($input, $this->getNames(), $this->annotationData); + $state->restore(); } /** @@ -298,14 +302,16 @@ protected function initialize(InputInterface $input, OutputInterface $output) */ protected function execute(InputInterface $input, OutputInterface $output) { - $this->injectIntoCommandfileInstance($input, $output); + $state = $this->injectIntoCommandfileInstance($input, $output); // Validate, run, process, alter, handle results. - return $this->commandProcessor()->process( + $result = $this->commandProcessor()->process( $output, $this->getNames(), $this->commandCallback, $this->createCommandData($input, $output) ); + $state->restore(); + return $result; } /** @@ -316,7 +322,7 @@ protected function execute(InputInterface $input, OutputInterface $output) */ public function processResults(InputInterface $input, OutputInterface $output, $results) { - $this->injectIntoCommandfileInstance($input, $output); + $state = $this->injectIntoCommandfileInstance($input, $output); $commandData = $this->createCommandData($input, $output); $commandProcessor = $this->commandProcessor(); $names = $this->getNames(); @@ -325,12 +331,14 @@ public function processResults(InputInterface $input, OutputInterface $output, $ $results, $commandData ); - return $commandProcessor->handleResults( + $status = $commandProcessor->handleResults( $output, $names, $results, $commandData ); + $state->restore(); + return $status; } protected function createCommandData(InputInterface $input, OutputInterface $output) @@ -359,9 +367,10 @@ protected function createCommandData(InputInterface $input, OutputInterface $out * * @param callable $commandCallback * @param CommandData $commandData + * @return State */ public function injectIntoCommandfileInstance(InputInterface $input, OutputInterface $output) { - InjectionHelper::injectIntoCallbackObject($this->commandCallback, $input, $output); + return StateHelper::injectIntoCallbackObject($this->commandCallback, $input, $output); } } diff --git a/src/Hooks/Dispatchers/CommandEventHookDispatcher.php b/src/Hooks/Dispatchers/CommandEventHookDispatcher.php index 5b7f4ee..cc1042f 100644 --- a/src/Hooks/Dispatchers/CommandEventHookDispatcher.php +++ b/src/Hooks/Dispatchers/CommandEventHookDispatcher.php @@ -4,7 +4,8 @@ use Consolidation\AnnotatedCommand\AnnotatedCommand; use Consolidation\AnnotatedCommand\Hooks\HookManager; -use Consolidation\AnnotatedCommand\InjectionHelper; +use Consolidation\AnnotatedCommand\State\State; +use Consolidation\AnnotatedCommand\State\StateHelper; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\ConsoleEvents; use Symfony\Component\Console\Event\ConsoleCommandEvent; @@ -21,14 +22,9 @@ class CommandEventHookDispatcher extends HookDispatcher */ public function callCommandEventHooks(ConsoleCommandEvent $event) { - $command = $event->getCommand(); $input = $event->getInput(); $output = $event->getOutput(); - if ($command instanceof AnnotatedCommand) { - $command->injectIntoCommandfileInstance($input, $output); - } - $hooks = [ HookManager::PRE_COMMAND_EVENT, HookManager::COMMAND_EVENT, @@ -40,8 +36,9 @@ public function callCommandEventHooks(ConsoleCommandEvent $event) $commandEvent->dispatch($event, ConsoleEvents::COMMAND); } if (is_callable($commandEvent)) { - InjectionHelper::injectIntoCallbackObject($commandEvent, $input, $output); + $state = StateHelper::injectIntoCallbackObject($commandEvent, $input, $output); $commandEvent($event); + $state->restore(); } } } diff --git a/src/Hooks/Dispatchers/InitializeHookDispatcher.php b/src/Hooks/Dispatchers/InitializeHookDispatcher.php index 6cc624e..f314af6 100644 --- a/src/Hooks/Dispatchers/InitializeHookDispatcher.php +++ b/src/Hooks/Dispatchers/InitializeHookDispatcher.php @@ -5,7 +5,8 @@ use Consolidation\AnnotatedCommand\AnnotationData; use Consolidation\AnnotatedCommand\Hooks\HookManager; use Consolidation\AnnotatedCommand\Hooks\InitializeHookInterface; -use Consolidation\AnnotatedCommand\InjectionHelper; +use Consolidation\AnnotatedCommand\State\State; +use Consolidation\AnnotatedCommand\State\StateHelper; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; @@ -31,7 +32,14 @@ public function initialize( protected function callInitializeHook($provider, $input, AnnotationData $annotationData) { - InjectionHelper::injectIntoCallbackObject($provider, $input); + $state = StateHelper::injectIntoCallbackObject($provider, $input); + $result = $this->doInitializeHook($provider, $input, $annotationData); + $state->restore(); + return $result; + } + + private function doInitializeHook($provider, $input, AnnotationData $annotationData) + { if ($provider instanceof InitializeHookInterface) { return $provider->initialize($input, $annotationData); } diff --git a/src/Hooks/Dispatchers/InteractHookDispatcher.php b/src/Hooks/Dispatchers/InteractHookDispatcher.php index cefc949..0ded179 100644 --- a/src/Hooks/Dispatchers/InteractHookDispatcher.php +++ b/src/Hooks/Dispatchers/InteractHookDispatcher.php @@ -5,7 +5,8 @@ use Consolidation\AnnotatedCommand\AnnotationData; use Consolidation\AnnotatedCommand\Hooks\HookManager; use Consolidation\AnnotatedCommand\Hooks\InteractorInterface; -use Consolidation\AnnotatedCommand\InjectionHelper; +use Consolidation\AnnotatedCommand\State\State; +use Consolidation\AnnotatedCommand\State\StateHelper; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -32,7 +33,14 @@ public function interact( protected function callInteractor($interactor, $input, $output, AnnotationData $annotationData) { - InjectionHelper::injectIntoCallbackObject($interactor, $input, $output); + $state = StateHelper::injectIntoCallbackObject($interactor, $input, $output); + $result = $this->doInteractor($interactor, $input, $output, $annotationData); + $state->restore(); + return $result; + } + + private function doInteractor($interactor, $input, $output, AnnotationData $annotationData) + { if ($interactor instanceof InteractorInterface) { return $interactor->interact($input, $output, $annotationData); } diff --git a/src/Hooks/Dispatchers/ProcessResultHookDispatcher.php b/src/Hooks/Dispatchers/ProcessResultHookDispatcher.php index b954b97..9284414 100644 --- a/src/Hooks/Dispatchers/ProcessResultHookDispatcher.php +++ b/src/Hooks/Dispatchers/ProcessResultHookDispatcher.php @@ -6,7 +6,8 @@ use Consolidation\AnnotatedCommand\CommandData; use Consolidation\AnnotatedCommand\Hooks\HookManager; use Consolidation\AnnotatedCommand\Hooks\ProcessResultInterface; -use Consolidation\AnnotatedCommand\InjectionHelper; +use Consolidation\AnnotatedCommand\State\State; +use Consolidation\AnnotatedCommand\State\StateHelper; /** * Call hooks @@ -39,7 +40,14 @@ public function process($result, CommandData $commandData) protected function callProcessor($processor, $result, CommandData $commandData) { - InjectionHelper::injectIntoCallbackObject($processor, $commandData->input(), $commandData->output()); + $state = StateHelper::injectIntoCallbackObject($processor, $commandData->input(), $commandData->output()); + $result = $this->doProcessor($processor, $result, $commandData); + $state->restore(); + return $result; + } + + private function doProcessor($processor, $result, CommandData $commandData) + { $processed = null; if ($processor instanceof ProcessResultInterface) { $processed = $processor->process($result, $commandData); diff --git a/src/Hooks/Dispatchers/ValidateHookDispatcher.php b/src/Hooks/Dispatchers/ValidateHookDispatcher.php index aa810c8..a516314 100644 --- a/src/Hooks/Dispatchers/ValidateHookDispatcher.php +++ b/src/Hooks/Dispatchers/ValidateHookDispatcher.php @@ -7,7 +7,8 @@ use Consolidation\AnnotatedCommand\CommandError; use Consolidation\AnnotatedCommand\Hooks\HookManager; use Consolidation\AnnotatedCommand\Hooks\ValidatorInterface; -use Consolidation\AnnotatedCommand\InjectionHelper; +use Consolidation\AnnotatedCommand\State\State; +use Consolidation\AnnotatedCommand\State\StateHelper; /** * Call hooks @@ -37,7 +38,14 @@ public function validate(CommandData $commandData) protected function callValidator($validator, CommandData $commandData) { - InjectionHelper::injectIntoCallbackObject($validator, $commandData->input(), $commandData->output()); + $state = StateHelper::injectIntoCallbackObject($validator, $commandData->input(), $commandData->output()); + $result = $this->doValidator($validator, $commandData); + $state->restore(); + return $result; + } + + private function doValidator($validator, CommandData $commandData) + { if ($validator instanceof ValidatorInterface) { return $validator->validate($commandData); } diff --git a/src/InjectionHelper.php b/src/InjectionHelper.php deleted file mode 100644 index b3b7241..0000000 --- a/src/InjectionHelper.php +++ /dev/null @@ -1,54 +0,0 @@ -setInput($input); - } - if (isset($output) && $callbackObject instanceof OutputAwareInterface) { - $callbackObject->setOutput($output); - } - } - - /** - * If the command callback is a method of an object, return the object. - * - * @param Callable|object $callback - * @return object|bool - */ - protected static function recoverCallbackObject($callback) - { - if (is_object($callback)) { - return $callback; - } - - if (!is_array($callback)) { - return false; - } - - if (!is_object($callback[0])) { - return false; - } - - return $callback[0]; - } -} diff --git a/src/State.php b/src/State.php new file mode 100644 index 0000000..53804ab --- /dev/null +++ b/src/State.php @@ -0,0 +1,10 @@ +currentState(); + + if ($target instanceof InputAwareInterface) { + $target->setInput($input); + } + if (isset($output) && $target instanceof OutputAwareInterface) { + $target->setOutput($output); + } + + return $state; + } + + /** + * If the command callback is a method of an object, return the object. + * + * @param Callable|object $callback + * @return object|bool + */ + protected static function recoverCallbackObject($callback) + { + if (is_object($callback)) { + return $callback; + } + + if (!is_array($callback)) { + return false; + } + + if (!is_object($callback[0])) { + return false; + } + + return $callback[0]; + } +}