Skip to content

Commit

Permalink
Add @ignored-command annotation. (#211)
Browse files Browse the repository at this point in the history
Move the alter for command info to earlier moments of the life
of the object.
  • Loading branch information
Rodrigo committed Sep 30, 2020
1 parent 5b8fef7 commit 008e669
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/AnnotatedCommandFactory.php
Expand Up @@ -241,23 +241,24 @@ public function getDataStore()
return $this->dataStore;
}

protected function createCommandInfoListFromClass($classNameOrInstance, $cachedCommandInfoList)
protected function createCommandInfoListFromClass($commandFileInstance, $cachedCommandInfoList)
{
$commandInfoList = [];

// Ignore special functions, such as __construct and __call, which
// can never be commands.
$commandMethodNames = array_filter(
get_class_methods($classNameOrInstance) ?: [],
function ($m) use ($classNameOrInstance) {
$reflectionMethod = new \ReflectionMethod($classNameOrInstance, $m);
get_class_methods($commandFileInstance) ?: [],
function ($m) use ($commandFileInstance) {
$reflectionMethod = new \ReflectionMethod($commandFileInstance, $m);
return !$reflectionMethod->isStatic() && !preg_match('#^_#', $m);
}
);

foreach ($commandMethodNames as $commandMethodName) {
if (!array_key_exists($commandMethodName, $cachedCommandInfoList)) {
$commandInfo = CommandInfo::create($classNameOrInstance, $commandMethodName);
$commandInfo = CommandInfo::create($commandFileInstance, $commandMethodName);
$this->alterCommandInfo($commandInfo, $commandFileInstance);
if (!static::isCommandOrHookMethod($commandInfo, $this->getIncludeAllPublicMethods())) {
$commandInfo->invalidate();
}
Expand All @@ -268,9 +269,11 @@ function ($m) use ($classNameOrInstance) {
return $commandInfoList;
}

public function createCommandInfo($classNameOrInstance, $commandMethodName)
public function createCommandInfo($commandFileInstance, $commandMethodName)
{
return CommandInfo::create($classNameOrInstance, $commandMethodName);
$commandInfo = CommandInfo::create($commandFileInstance, $commandMethodName);
$this->alterCommandInfo($commandInfo, $commandFileInstance);
return $commandInfo;
}

public function createCommandsFromClassInfo($commandInfoList, $commandFileInstance, $includeAllPublicMethods = null)
Expand Down Expand Up @@ -320,6 +323,10 @@ public static function isCommandMethod($commandInfo, $includeAllPublicMethods)
if (static::isHookMethod($commandInfo)) {
return false;
}
// Ignore everything labeled @ignored-command
if ($commandInfo->hasAnnotation('ignored-command')) {
return false;
}
// Include everything labeled @command
if ($commandInfo->hasAnnotation('command')) {
return true;
Expand Down Expand Up @@ -400,7 +407,6 @@ protected function getNthWord($string, $n, $default = '', $delimiter = ' ')

public function createCommand(CommandInfo $commandInfo, $commandFileInstance)
{
$this->alterCommandInfo($commandInfo, $commandFileInstance);
$command = new AnnotatedCommand($commandInfo->getName());
$commandCallback = [$commandFileInstance, $commandInfo->getMethodName()];
$command->setCommandCallback($commandCallback);
Expand Down
10 changes: 10 additions & 0 deletions tests/AnnotatedCommandFactoryTest.php
Expand Up @@ -153,6 +153,15 @@ function testGlobalOptionsOnly()
$this->assertRunCommandViaApplicationEquals($command, $input, "Arg is test, options[help] is false");
}

function testIgnoredCommand()
{
$this->commandFileInstance = new \Consolidation\TestUtils\ExampleCommandFile;
$this->commandFactory = new AnnotatedCommandFactory();

$commandList = $this->commandFactory->createCommandsFromClass($this->commandFileInstance);
$this->assertArrayNotHasKey('ignoredCommand', $commandList);
}

function testOptionWithOptionalValue()
{
$this->commandFileInstance = new \Consolidation\TestUtils\ExampleCommandFile;
Expand Down Expand Up @@ -308,6 +317,7 @@ function testAnnotatedCommandInfoAlteration()

$this->commandFactory->addCommandInfoAlterer(new ExampleCommandInfoAlterer());

$commandInfo = $this->commandFactory->createCommandInfo($this->commandFileInstance, 'myEcho');
$command = $this->commandFactory->createCommand($commandInfo, $this->commandFileInstance);

$annotationData = $command->getAnnotationData();
Expand Down
7 changes: 7 additions & 0 deletions tests/src/ExampleCommandFile.php
Expand Up @@ -538,4 +538,11 @@ public function globalOptionsOnly($arg, array $options = [])
{
return "Arg is $arg, options[help] is " . var_export($options['help'], true) . "\n";
}

/**
* @ignored-command
*/
public function ignoredCommand()
{
}
}

0 comments on commit 008e669

Please sign in to comment.