Skip to content

Commit

Permalink
Move implementation to AnnotatedCommand class and be more consistent …
Browse files Browse the repository at this point in the history
…about initializing it.
  • Loading branch information
greg-1-anderson committed Aug 27, 2020
1 parent b28007e commit 6dc904a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 49 deletions.
50 changes: 48 additions & 2 deletions src/AnnotatedCommand.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php
namespace Consolidation\AnnotatedCommand;

use Consolidation\AnnotatedCommand\Help\HelpDocumentAlter;
use Consolidation\AnnotatedCommand\Help\HelpDocumentBuilder;
use Consolidation\AnnotatedCommand\Hooks\HookManager;
use Consolidation\AnnotatedCommand\Output\OutputAwareInterface;
use Consolidation\AnnotatedCommand\Parser\CommandInfo;
use Consolidation\AnnotatedCommand\Help\HelpDocumentAlter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputAwareInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Consolidation\AnnotatedCommand\Help\HelpDocumentBuilder;

/**
* AnnotatedCommands are created automatically by the
Expand Down Expand Up @@ -274,6 +276,7 @@ public function optionsHookForHookAnnotations($commandInfoList)
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$this->injectIntoCommandfileInstance($input, $output);
$this->commandProcessor()->interact(
$input,
$output,
Expand All @@ -284,6 +287,7 @@ protected function interact(InputInterface $input, OutputInterface $output)

protected function initialize(InputInterface $input, OutputInterface $output)
{
$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);
Expand All @@ -294,6 +298,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->injectIntoCommandfileInstance($input, $output);
// Validate, run, process, alter, handle results.
return $this->commandProcessor()->process(
$output,
Expand All @@ -311,6 +316,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
public function processResults(InputInterface $input, OutputInterface $output, $results)
{
$this->injectIntoCommandfileInstance($input, $output);
$commandData = $this->createCommandData($input, $output);
$commandProcessor = $this->commandProcessor();
$names = $this->getNames();
Expand Down Expand Up @@ -347,4 +353,44 @@ protected function createCommandData(InputInterface $input, OutputInterface $out

return $commandData;
}

/**
* Inject $input and $output into the command instance if it is set up to receive them.
*
* @param callable $commandCallback
* @param CommandData $commandData
*/
protected function injectIntoCommandfileInstance(InputInterface $input, OutputInterface $output)
{
$commandfileInstance = $this->recoverCommandfileInstance();
if (!$commandfileInstance) {
return;
}

if ($commandfileInstance instanceof InputAwareInterface) {
$commandfileInstance->setInput($input);
}
if ($commandfileInstance instanceof OutputAwareInterface) {
$commandfileInstance->setOutput($output);
}
}

/**
* If the command callback is a method of an object, return the object.
*
* @param callable $commandCallback
* @return object|bool
*/
protected function recoverCommandfileInstance()
{
if (!is_array($this->commandCallback)) {
return false;
}

if (!is_object($this->commandCallback[0])) {
return false;
}

return $this->commandCallback[0];
}
}
47 changes: 0 additions & 47 deletions src/CommandProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ protected function runCommandCallback($commandCallback, CommandData $commandData
{
$result = false;
try {
$this->injectIntoCommandInstance($commandCallback, $commandData);
$args = $this->parameterInjection()->args($commandData);
$result = call_user_func_array($commandCallback, $args);
} catch (\Exception $e) {
Expand All @@ -262,54 +261,8 @@ protected function runCommandCallback($commandCallback, CommandData $commandData
return $result;
}

/**
* Use the parameter injection manager to provide injected classes to command data.
*
* @param CommandData $commandData
* @param array $injectedClasses
*/
public function injectIntoCommandData($commandData, $injectedClasses)
{
$this->parameterInjection()->injectIntoCommandData($commandData, $injectedClasses);
}

/**
* Inject $input and $output into the command instance if it is set up to receive them.
*
* @param callable $commandCallback
* @param CommandData $commandData
*/
protected function injectIntoCommandInstance($commandCallback, CommandData $commandData)
{
$commandInstance = $this->recoverObject($commandCallback);
if (!$commandInstance) {
return;
}

if ($commandInstance instanceof InputAwareInterface) {
$commandInstance->setInput($commandData->input());
}
if ($commandInstance instanceof OutputAwareInterface) {
$commandInstance->setInput($commandData->output());
}
}

/**
* If the command callback is a method of an object, return the object.
*
* @param callable $commandCallback
* @return object|bool
*/
protected function recoverObject($commandCallback)
{
if (!is_array($commandCallback)) {
return false;
}

if (!is_object($commandCallback[0])) {
return false;
}

return $commandCallback[0];
}
}

0 comments on commit 6dc904a

Please sign in to comment.