From 264700584e4c187cce01462aff98f4f0a2986c0f Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Tue, 17 Jan 2017 14:15:00 -0500 Subject: [PATCH 1/2] Stubbing out new @calls annotation. --- src/AnnotatedCommand.php | 25 +++++++++++++++++++ src/Parser/CommandInfo.php | 24 ++++++++++++++++++ .../AbstractCommandDocBlockParser.php | 9 +++++++ 3 files changed, 58 insertions(+) diff --git a/src/AnnotatedCommand.php b/src/AnnotatedCommand.php index 2f98833..61f4dec 100644 --- a/src/AnnotatedCommand.php +++ b/src/AnnotatedCommand.php @@ -29,6 +29,7 @@ */ class AnnotatedCommand extends Command implements HelpDocumentAlter { + protected $calls = []; protected $commandCallback; protected $commandProcessor; protected $annotationData; @@ -120,8 +121,20 @@ public function setTopics($topics) return $this; } + public function getCalls() + { + return $this->calls; + } + + public function setCalls($calls) + { + $this->calls = $calls; + return $this; + } + public function setCommandInfo($commandInfo) { + $this->setCalls($commandInfo->getCalls()); $this->setDescription($commandInfo->getDescription()); $this->setHelp($commandInfo->getHelp()); $this->setAliases($commandInfo->getAliases()); @@ -376,11 +389,23 @@ protected function initialize(InputInterface $input, OutputInterface $output) $this->commandProcessor()->initializeHook($input, $this->getNames(), $this->annotationData); } + protected function executeCallsCommands() { + if ($this->getCalls()) { + foreach ($this->getCalls as $command_name) { + // @todo validate that $command_name exists in this application. + // @todo Execute command. + // @todo On failure, return exit code of called command. + } + } + } + /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { + // @todo Call $this->executeCallsCommands(); + // Validate, run, process, alter, handle results. return $this->commandProcessor()->process( $output, diff --git a/src/Parser/CommandInfo.php b/src/Parser/CommandInfo.php index 75b08cf..f33613e 100644 --- a/src/Parser/CommandInfo.php +++ b/src/Parser/CommandInfo.php @@ -83,6 +83,11 @@ class CommandInfo */ protected $optionParamName; + /** + * @var array + */ + protected $calls = []; + /** * Create a new CommandInfo class for a particular method of a class. * @@ -333,6 +338,25 @@ public function getTopics() return explode(',', trim($topics)); } + /** + * Returns an array of commands that this command calls. + * + * @return string[] + */ + public function getCalls() + { + return $this->calls; + } + + public function setCalls($calls) { + if (is_string($calls)) { + $calls = explode(',', static::convertListToCommaSeparated($calls)); + } + $this->calls = array_filter($calls); + + return $this; + } + /** * Return the list of refleaction parameters. * diff --git a/src/Parser/Internal/AbstractCommandDocBlockParser.php b/src/Parser/Internal/AbstractCommandDocBlockParser.php index 0534a99..f15b05e 100644 --- a/src/Parser/Internal/AbstractCommandDocBlockParser.php +++ b/src/Parser/Internal/AbstractCommandDocBlockParser.php @@ -36,6 +36,7 @@ abstract class AbstractCommandDocBlockParser 'usage' => 'processUsageTag', 'description' => 'processAlternateDescriptionTag', 'desc' => 'processAlternateDescriptionTag', + 'calls' => 'processCalls', ]; public function __construct(CommandInfo $commandInfo, \ReflectionMethod $reflection) @@ -168,6 +169,14 @@ protected function processAliases($tag) $this->commandInfo->setAliases((string)$tag->getDescription()); } + /** + * Process the comma-separated list of commands to call. + */ + protected function processCalls($tag) + { + $this->commandInfo->setCalls((string)$tag->getDescription()); + } + /** * Store the data from a @param annotation in our argument descriptions. */ From 86d29464eab6cf5da958c928e8650dda3d93c2a7 Mon Sep 17 00:00:00 2001 From: Matthew Grasmick Date: Tue, 17 Jan 2017 14:35:32 -0500 Subject: [PATCH 2/2] Making functional. --- src/AnnotatedCommand.php | 43 ++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/AnnotatedCommand.php b/src/AnnotatedCommand.php index 61f4dec..02d0f47 100644 --- a/src/AnnotatedCommand.php +++ b/src/AnnotatedCommand.php @@ -6,6 +6,7 @@ use Consolidation\OutputFormatters\FormatterManager; use Consolidation\OutputFormatters\Options\FormatterOptions; use Consolidation\AnnotatedCommand\Help\HelpDocumentAlter; +use Psr\Log\LoggerInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -389,14 +390,39 @@ protected function initialize(InputInterface $input, OutputInterface $output) $this->commandProcessor()->initializeHook($input, $this->getNames(), $this->annotationData); } - protected function executeCallsCommands() { + protected function executeCallsCommands(InputInterface $input, OutputInterface $output) { if ($this->getCalls()) { - foreach ($this->getCalls as $command_name) { - // @todo validate that $command_name exists in this application. - // @todo Execute command. - // @todo On failure, return exit code of called command. + //$logger = $this->getApplication()->get('logger'); + foreach ($this->getCalls() as $command_name) { + $command = $this->getApplication()->find($command_name); + if (!$command) { + /** @var LoggerInterface $logger */ + //$logger->warning("Command $command_name does not exist. Skipping."); + continue; + } + + // @see http://symfony.com/doc/current/console/calling_commands.html + try { + $exit_code = $command->run($input, $output); + // If command was not successful, return early. + if ($exit_code !== 0) { + return $exit_code; + } + } + catch (\Exception $e) { + //$logger->error("Exception was caught while executing $command_name."); + // Return non-zero exit code. + return 1; + } } } + + // Return 0 exit code for success. + return 0; + } + + protected function executeCommand() { + } /** @@ -404,7 +430,12 @@ protected function executeCallsCommands() { */ protected function execute(InputInterface $input, OutputInterface $output) { - // @todo Call $this->executeCallsCommands(); + $calls_exit_code = $this->executeCallsCommands($input, $output); + // If one of the "@calls" commands returned a non-zero exit code, + // return early. + if ($calls_exit_code !== 0) { + return $calls_exit_code;; + } // Validate, run, process, alter, handle results. return $this->commandProcessor()->process(