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 7284543

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; }) ; ```
1 parent c732122 commit 7284543
Copy full SHA for 7284543

File tree

7 files changed

+187
-0
lines changed
Filter options

7 files changed

+187
-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

+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
16+
/**
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
class SingleCommandApplication extends Command
20+
{
21+
private $version = 'UNKNOWN';
22+
23+
public function setVersion(string $version): self
24+
{
25+
$this->version = $version;
26+
27+
return $this;
28+
}
29+
30+
public function __destruct()
31+
{
32+
// We use the command name as the application name
33+
$application = new Application($this->getName() ?: 'UNKNOWN', $this->version);
34+
// Fix the usage of the command displayed with "--help"
35+
$this->setName($_SERVER['argv'][0]);
36+
$application->add($this);
37+
$application->setDefaultCommand($this->getName(), true);
38+
$application->run();
39+
}
40+
}
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
;
27+
?>
28+
--EXPECTF--
29+
Hello You!
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
;
23+
?>
24+
--EXPECTF--
25+
Hello World!
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
;
24+
?>
25+
--EXPECTF--
26+
Usage:
27+
Standard input code
28+
29+
Options:
30+
-h, --help Display this help message
31+
-q, --quiet Do not output any message
32+
-V, --version Display this application version
33+
--ansi Force ANSI output
34+
--no-ansi Disable ANSI output
35+
-n, --no-interaction Do not ask any interactive question
36+
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
;
25+
?>
26+
--EXPECTF--
27+
My Super Command 1.0.0
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
;
23+
?>
24+
--EXPECTF--
25+
Console Tool

0 commit comments

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