Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0ee2fff

Browse filesBrowse files
committed
[Console] Add SingleCommandApplication to ease creation of Single Command Application
``` <?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): int { $output->writeln(sprintf('Hello %s!', $input->getArgument('who'))); return 0; }) ->run() ; ```
1 parent 5690b97 commit 0ee2fff
Copy full SHA for 0ee2fff

File tree

7 files changed

+204
-0
lines changed
Filter options

7 files changed

+204
-0
lines changed

‎src/Symfony/Component/Console/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/CHANGELOG.md
+5Lines 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

+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
$ret = $application->run($input, $output);
48+
$this->running = false;
49+
50+
return $ret;
51+
}
52+
}
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
--EXPECTF--
30+
Hello You!
+26Lines 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+
--EXPECTF--
26+
Hello World!
+37Lines 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
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+
Standard input code
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
+28Lines 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
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+
--EXPECTF--
28+
My Super Command 1.0.0
+26Lines 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+
--EXPECTF--
26+
Console Tool

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.