From 2f46e2428585bb8ed8bbf11b880e08a37b286164 Mon Sep 17 00:00:00 2001 From: Tim Goudriaan Date: Sat, 15 Dec 2018 00:56:37 +0100 Subject: [PATCH] Add specific replacement for help text in single command applications --- src/Symfony/Component/Console/Application.php | 12 +++++++++++- src/Symfony/Component/Console/Command/Command.php | 3 ++- .../Component/Console/Tests/Command/CommandTest.php | 8 ++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 680036041019b..3e7ba8daebc73 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -74,7 +74,7 @@ class Application private $dispatcher; private $terminal; private $defaultCommand; - private $singleCommand; + private $singleCommand = false; private $initialized; /** @@ -1162,6 +1162,16 @@ public function setDefaultCommand($commandName, $isSingleCommand = false) return $this; } + /** + * Returns whether the application is a single command application or not. + * + * @return bool Whether the application is a single command application or not + */ + public function isSingleCommandApplication() + { + return $this->singleCommand; + } + private function splitStringByWidth($string, $width) { // str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly. diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php index c8dcf3befef88..7ff4d60c264ff 100644 --- a/src/Symfony/Component/Console/Command/Command.php +++ b/src/Symfony/Component/Console/Command/Command.php @@ -533,6 +533,7 @@ public function getHelp() public function getProcessedHelp() { $name = $this->name; + $isSingleCommand = $this->application && $this->application->isSingleCommandApplication(); $placeholders = array( '%command.name%', @@ -540,7 +541,7 @@ public function getProcessedHelp() ); $replacements = array( $name, - $_SERVER['PHP_SELF'].' '.$name, + $isSingleCommand ? $_SERVER['PHP_SELF'] : $_SERVER['PHP_SELF'].' '.$name, ); return str_replace($placeholders, $replacements, $this->getHelp() ?: $this->getDescription()); diff --git a/src/Symfony/Component/Console/Tests/Command/CommandTest.php b/src/Symfony/Component/Console/Tests/Command/CommandTest.php index 6bc3f75b932a6..09c89c4167807 100644 --- a/src/Symfony/Component/Console/Tests/Command/CommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/CommandTest.php @@ -166,6 +166,14 @@ public function testGetProcessedHelp() $command = new \TestCommand(); $command->setHelp(''); $this->assertContains('description', $command->getProcessedHelp(), '->getProcessedHelp() falls back to the description'); + + $command = new \TestCommand(); + $command->setHelp('The %command.name% command does... Example: php %command.full_name%.'); + $application = new Application(); + $application->add($command); + $application->setDefaultCommand('namespace:name', true); + $this->assertContains('The namespace:name command does...', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.name% correctly in single command applications'); + $this->assertNotContains('%command.full_name%', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.full_name% in single command applications'); } public function testGetSetAliases()