Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #65: Allow commands to call other commands #65

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/AnnotatedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,7 @@
*/
class AnnotatedCommand extends Command implements HelpDocumentAlter
{
protected $calls = [];
protected $commandCallback;
protected $commandProcessor;
protected $annotationData;
Expand Down Expand Up @@ -120,8 +122,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());
Expand Down Expand Up @@ -376,11 +390,53 @@ protected function initialize(InputInterface $input, OutputInterface $output)
$this->commandProcessor()->initializeHook($input, $this->getNames(), $this->annotationData);
}

protected function executeCallsCommands(InputInterface $input, OutputInterface $output) {
if ($this->getCalls()) {
//$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.");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a logging mechanism available?

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() {

}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$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(
$output,
Expand Down
24 changes: 24 additions & 0 deletions src/Parser/CommandInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class CommandInfo
*/
protected $optionParamName;

/**
* @var array
*/
protected $calls = [];

/**
* Create a new CommandInfo class for a particular method of a class.
*
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe array-merge these together, so folks can use multiple @calls in addition to csv, if they prefer?

Copy link
Contributor Author

@grasmash grasmash Jan 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I'd prefer that. I briefly attempted this but wasn't quite sure how to access multiple @calls values, each usage appeared to overwrite the previous.


return $this;
}

/**
* Return the list of refleaction parameters.
*
Expand Down
9 changes: 9 additions & 0 deletions src/Parser/Internal/AbstractCommandDocBlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract class AbstractCommandDocBlockParser
'usage' => 'processUsageTag',
'description' => 'processAlternateDescriptionTag',
'desc' => 'processAlternateDescriptionTag',
'calls' => 'processCalls',
];

public function __construct(CommandInfo $commandInfo, \ReflectionMethod $reflection)
Expand Down Expand Up @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are multiple @Calls annotations, this function will be called multiple times. Currently, this implementation overwrites via setCalls(). It could also call getCalls() first and merge. Might want to move where the csv is converted to an array here, maybe.

}

/**
* Store the data from a @param annotation in our argument descriptions.
*/
Expand Down