From 008e669fcdb0b2f62a667a2049a3d7060b16a353 Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Wed, 30 Sep 2020 23:24:58 +0200 Subject: [PATCH] Add @ignored-command annotation. (#211) Move the alter for command info to earlier moments of the life of the object. --- src/AnnotatedCommandFactory.php | 22 ++++++++++++++-------- tests/AnnotatedCommandFactoryTest.php | 10 ++++++++++ tests/src/ExampleCommandFile.php | 7 +++++++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/AnnotatedCommandFactory.php b/src/AnnotatedCommandFactory.php index 9f67ef6..790b69b 100644 --- a/src/AnnotatedCommandFactory.php +++ b/src/AnnotatedCommandFactory.php @@ -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(); } @@ -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) @@ -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; @@ -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); diff --git a/tests/AnnotatedCommandFactoryTest.php b/tests/AnnotatedCommandFactoryTest.php index b8764c9..2950619 100644 --- a/tests/AnnotatedCommandFactoryTest.php +++ b/tests/AnnotatedCommandFactoryTest.php @@ -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; @@ -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(); diff --git a/tests/src/ExampleCommandFile.php b/tests/src/ExampleCommandFile.php index f0d04bc..c0dd996 100644 --- a/tests/src/ExampleCommandFile.php +++ b/tests/src/ExampleCommandFile.php @@ -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() + { + } }