8000 feature #34819 [Console] Add SingleCommandApplication to ease creatio… · symfony/symfony@1443b43 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1443b43

Browse files
committed
feature #34819 [Console] Add SingleCommandApplication to ease creation of Single Command Application (lyrixx)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Console] Add SingleCommandApplication to ease creation of Single Command Application | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #34293 | License | MIT | Doc PR | --- ```php <?php require __DIR__.'/vendor/autoload.php'; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\SingleCommandApplication; (new SingleCommandApplication()) ->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) { $output->writeln(sprintf('Hello %s!', $input->getArgument('who'))); }) ->run() ; ``` --- Note: I tried this too, and it works as expected: ```php class MyCommand extends SingleCommandApplication { public function execute(InputInterface $input, OutputInterface $output) { $output->writeln('hello'); return 0; } } new MyCommand(); ``` Commits ------- 4af513d [Console] Add SingleCommandApplication to ease creation of Single Command Application
2 parents 8acfd3a + 4af513d commit 1443b43

File tree

7 files changed

+207
-0
lines changed

7 files changed

+207
-0
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* Add `SingleCommandApplication`
8+
49
5.0.0
510
-----
611

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console;
13+
14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
18+
/**
19+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
20+
*/
21+
class SingleCommandApplication extends Command
22+
{
23+
private $version = 'UNKNOWN';
24+
private $running = false;
25+
26+
public function setVersion(string $version): self
27+
{
28+
$this->version = $version;
29+
30+
return $this;
31+
}
32+
33+
public function run(InputInterface $input = null, OutputInterface $output = null): int
34+
{
35+
if ($this->running) {
36+
return parent::run($input, $output);
37+
}
38+
39+
// We use the command name as the application name
40+
$application = new Application($this->getName() ?: 'UNKNOWN', $this->version);
41+
// Fix the usage of the command displayed with "--help"
42+
$this->setName($_SERVER['argv'][0]);
43+
$application->add($this);
44+
$application->setDefaultCommand($this->getName(), true);
45+
46+
$this->running = true;
47+
try {
48+
$ret = $application->run($input, $output);
49+
} finally {
50+
$this->running = false;
51+
}
52+
53+
return $ret ?? 1;
54+
}
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numbe 6D40 rDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Single Application can be executed
3+
--ARGS--
4+
You
5+
--FILE--
6+
<?php
7+
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
use Symfony\Component\Console\SingleCommandApplication;
12+
13+
$vendor = __DIR__;
14+
while (!file_exists($vendor.'/vendor')) {
15+
$vendor = dirname($vendor);
16+
}
17+
require $vendor.'/vendor/autoload.php';
18+
19+
(new SingleCommandApplication())
20+
->addArgument('who', InputArgument::OPTIONAL, 'Who', 'World')
21+
->setCode(function (InputInterface $input, OutputInterface $output): int {
22+
$output->writeln(sprintf('Hello %s!', $input->getArgument('who')));
23+
24+
return 0;
25+
})
26+
->run()
27+
;
28+
?>
29+
--EXPECT--
30+
Hello You!
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Single Application can be executed
3+
--FILE--
4+
<?php
5+
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\SingleCommandApplication;
9+
10+
$vendor = __DIR__;
11+
while (!file_exists($vendor.'/vendor')) {
12+
$vendor = dirname($vendor);
13+
}
14+
require $vendor.'/vendor/autoload.php';
15+
16+
(new SingleCommandApplication())
17+
->setCode(function (InputInterface $input, OutputInterface $output): int {
18+
$output->writeln('Hello World!');
19+
20+
return 0;
21+
})
22+
->run()
23+
;
24+
?>
25+
--EXPECT--
26+
Hello World!
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
Single Application can be executed
3+
--ARGS--
4+
--help --no-ansi
5+
--FILE--
6+
<?php
7+
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Console\SingleCommandApplication;
11+
12+
$vendor = __DIR__;
13+
while (!file_exists($vendor.'/vendor')) {
14+
$vendor = dirname($vendor);
15+
}
16+
require $vendor.'/vendor/autoload.php';
17+
18+
(new SingleCommandApplication())
19+
->setName('My Super Command')
20+
->setCode(function (InputInterface $input, OutputInterface $output): int {
21+
return 0;
22+
})
23+
->run()
24+
;
25+
?>
26+
--EXPECTF--
27+
Usage:
28+
%s
29+
30+
Options:
31+
-h, --help Display this help message
32+
-q, --quiet Do not output any message
33+
-V, --version Display this application version
34+
--ansi Force ANSI output
35+
--no-ansi Disable ANSI output
36+
-n, --no-interaction Do not ask any interactive question
37+
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Single Application can be executed
3+
--ARGS--
4+
--version --no-ansi
5+
--FILE--
6+
<?php
7+
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Console\SingleCommandApplication;
11+
12+
$vendor = __DIR__;
13+
while (!file_exists($vendor.'/vendor')) {
14+
$vendor = dirname($vendor);
15+
}
16+
require $vendor.'/vendor/autoload.php';
17+
18+
(new SingleCommandApplication())
19+
->setName('My Super Command')
20+
->setVersion('1.0.0')
21+
->setCode(function (InputInterface $input, OutputInterface $output): int {
22+
return 0;
23+
})
24+
->run()
25+
;
26+
?>
27+
--EXPECT--
28+
My Super Command 1.0.0
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Single Application can be executed
3+
--ARGS--
4+
--version
5+
--FILE--
6+
<?php
7+
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Console\SingleCommandApplication;
11+
12+
$vendor = __DIR__;
13+
while (!file_exists($vendor.'/vendor')) {
14+
$vendor = dirname($vendor);
15+
}
16+
require $vendor.'/vendor/autoload.php';
17+
18+
(new SingleCommandApplication())
19+
->setCode(function (InputInterface $input, OutputInterface $output): int {
20+
return 0;
21+
})
22+
->run()
23+
;
24+
?>
25+
--EXPECT--
26+
Console Tool

0 commit comments

Comments
 (0)
0