From 4af513d4499b85d08d066eb58bb0d10873b80294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Wed, 4 Dec 2019 17:31:32 +0100 Subject: [PATCH] [Console] Add SingleCommandApplication to ease creation of Single Command Application ``` setName('My Super Command') // Optional ->setVersion('1.0.0') // Optional ->setProcessTitle('my_proc_title') // Optional ->addArgument('who', InputArgument::OPTIONAL, 'Who', 'World') // Optional ->setCode(function(InputInterface $input, OutputInterface $output): int { $output->writeln(sprintf('Hello %s!', $input->getArgument('who'))); return 0; }) ->run() ; ``` --- src/Symfony/Component/Console/CHANGELOG.md | 5 ++ .../Console/SingleCommandApplication.php | 55 +++++++++++++++++++ .../Tests/phpt/single_application/arg.phpt | 30 ++++++++++ .../phpt/single_application/default.phpt | 26 +++++++++ .../phpt/single_application/help_name.phpt | 37 +++++++++++++ .../version_default_name.phpt | 28 ++++++++++ .../phpt/single_application/version_name.phpt | 26 +++++++++ 7 files changed, 207 insertions(+) create mode 100644 src/Symfony/Component/Console/SingleCommandApplication.php create mode 100644 src/Symfony/Component/Console/Tests/phpt/single_application/arg.phpt create mode 100644 src/Symfony/Component/Console/Tests/phpt/single_application/default.phpt create mode 100644 src/Symfony/Component/Console/Tests/phpt/single_application/help_name.phpt create mode 100644 src/Symfony/Component/Console/Tests/phpt/single_application/version_default_name.phpt create mode 100644 src/Symfony/Component/Console/Tests/phpt/single_application/version_name.phpt diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index 3273efbdea..902ef67fc9 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +5.1.0 +----- + + * Add `SingleCommandApplication` + 5.0.0 ----- diff --git a/src/Symfony/Component/Console/SingleCommandApplication.php b/src/Symfony/Component/Console/SingleCommandApplication.php new file mode 100644 index 0000000000..ffa176fbd0 --- /dev/null +++ b/src/Symfony/Component/Console/SingleCommandApplication.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * @author Grégoire Pineau + */ +class SingleCommandApplication extends Command +{ + private $version = 'UNKNOWN'; + private $running = false; + + public function setVersion(string $version): self + { + $this->version = $version; + + return $this; + } + + public function run(InputInterface $input = null, OutputInterface $output = null): int + { + if ($this->running) { + return parent::run($input, $output); + } + + // We use the command name as the application name + $application = new Application($this->getName() ?: 'UNKNOWN', $this->version); + // Fix the usage of the command displayed with "--help" + $this->setName($_SERVER['argv'][0]); + $application->add($this); + $application->setDefaultCommand($this->getName(), true); + + $this->running = true; + try { + $ret = $application->run($input, $output); + } finally { + $this->running = false; + } + + return $ret ?? 1; + } +} diff --git a/src/Symfony/Component/Console/Tests/phpt/single_application/arg.phpt b/src/Symfony/Component/Console/Tests/phpt/single_application/arg.phpt new file mode 100644 index 0000000000..049776dc87 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/phpt/single_application/arg.phpt @@ -0,0 +1,30 @@ +--TEST-- +Single Application can be executed +--ARGS-- +You +--FILE-- +addArgument('who', InputArgument::OPTIONAL, 'Who', 'World') + ->setCode(function (InputInterface $input, OutputInterface $output): int { + $output->writeln(sprintf('Hello %s!', $input->getArgument('who'))); + + return 0; + }) + ->run() +; +?> +--EXPECT-- +Hello You! diff --git a/src/Symfony/Component/Console/Tests/phpt/single_application/default.phpt b/src/Symfony/Component/Console/Tests/phpt/single_application/default.phpt new file mode 100644 index 0000000000..bb0387ea43 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/phpt/single_application/default.phpt @@ -0,0 +1,26 @@ +--TEST-- +Single Application can be executed +--FILE-- +setCode(function (InputInterface $input, OutputInterface $output): int { + $output->writeln('Hello World!'); + + return 0; + }) + ->run() +; +?> +--EXPECT-- +Hello World! diff --git a/src/Symfony/Component/Console/Tests/phpt/single_application/help_name.phpt b/src/Symfony/Component/Console/Tests/phpt/single_application/help_name.phpt new file mode 100644 index 0000000000..1ade3188b1 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/phpt/single_application/help_name.phpt @@ -0,0 +1,37 @@ +--TEST-- +Single Application can be executed +--ARGS-- +--help --no-ansi +--FILE-- +setName('My Super Command') + ->setCode(function (InputInterface $input, OutputInterface $output): int { + return 0; + }) + ->run() +; +?> +--EXPECTF-- +Usage: + %s + +Options: + -h, --help Display this help message + -q, --quiet Do not output any message + -V, --version Display this application version + --ansi Force ANSI output + --no-ansi Disable ANSI output + -n, --no-interaction Do not ask any interactive question + -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug diff --git a/src/Symfony/Component/Console/Tests/phpt/single_application/version_default_name.phpt b/src/Symfony/Component/Console/Tests/phpt/single_application/version_default_name.phpt new file mode 100644 index 0000000000..3e40fa3b84 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/phpt/single_application/version_default_name.phpt @@ -0,0 +1,28 @@ +--TEST-- +Single Application can be executed +--ARGS-- +--version --no-ansi +--FILE-- +setName('My Super Command') + ->setVersion('1.0.0') + ->setCode(function (InputInterface $input, OutputInterface $output): int { + return 0; + }) + ->run() +; +?> +--EXPECT-- +My Super Command 1.0.0 diff --git a/src/Symfony/Component/Console/Tests/phpt/single_application/version_name.phpt b/src/Symfony/Component/Console/Tests/phpt/single_application/version_name.phpt new file mode 100644 index 0000000000..4f1b7395de --- /dev/null +++ b/src/Symfony/Component/Console/Tests/phpt/single_application/version_name.phpt @@ -0,0 +1,26 @@ +--TEST-- +Single Application can be executed +--ARGS-- +--version +--FILE-- +setCode(function (InputInterface $input, OutputInterface $output): int { + return 0; + }) + ->run() +; +?> +--EXPECT-- +Console Tool