-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Add SingleCommandApplication to ease creation of Single Command Application #34819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Console] Add SingleCommandApplication to ease creation of Single Com…
…mand 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() ; ```
- Loading branch information
commit 4af513d4499b85d08d066eb58bb0d10873b80294
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.1.0 | ||
----- | ||
|
||
* Add `SingleCommandApplication` | ||
|
||
5.0.0 | ||
----- | ||
|
||
|
55 changes: 55 additions & 0 deletions
55
src/Symfony/Component/Console/SingleCommandApplication.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 <lyrixx@lyrixx.info> | ||
*/ | ||
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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Symfony/Component/Console/Tests/phpt/single_application/arg.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--TEST-- | ||
Single Application can be executed | ||
--ARGS-- | ||
You | ||
--FILE-- | ||
<?php | ||
|
||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\SingleCommandApplication; | ||
|
||
$vendor = __DIR__; | ||
while (!file_exists($vendor.'/vendor')) { | ||
$vendor = dirname($vendor); | ||
} | ||
require $vendor.'/vendor/autoload.php'; | ||
|
||
(new SingleCommandApplication()) | ||
->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! |
26 changes: 26 additions & 0 deletions
26
src/Symfony/Component/Console/Tests/phpt/single_application/default.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--TEST-- | ||
Single Application can be executed | ||
--FILE-- | ||
<?php | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\SingleCommandApplication; | ||
|
||
$vendor = __DIR__; | ||
while (!file_exists($vendor.'/vendor')) { | ||
$vendor = dirname($vendor); | ||
} | ||
require $vendor.'/vendor/autoload.php'; | ||
|
||
(new SingleCommandApplication()) | ||
->setCode(function (InputInterface $input, OutputInterface $output): int { | ||
$output->writeln('Hello World!'); | ||
|
||
return 0; | ||
}) | ||
->run() | ||
; | ||
?> | ||
--EXPECT-- | ||
Hello World! |
37 changes: 37 additions & 0 deletions
37
src/Symfony/Component/Console/Tests/phpt/single_application/help_name.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--TEST-- | ||
Single Application can be executed | ||
--ARGS-- | ||
--help --no-ansi | ||
--FILE-- | ||
<?php | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\SingleCommandApplication; | ||
|
||
$vendor = __DIR__; | ||
while (!file_exists($vendor.'/vendor')) { | ||
$vendor = dirname($vendor); | ||
} | ||
require $vendor.'/vendor/autoload.php'; | ||
|
||
(new SingleCommandApplication()) | ||
->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 |
28 changes: 28 additions & 0 deletions
28
src/Symfony/Component/Console/Tests/phpt/single_application/version_default_name.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--TEST-- | ||
Single Application can be executed | ||
--ARGS-- | ||
--version --no-ansi | ||
--FILE-- | ||
<?php | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\SingleCommandApplication; | ||
|
||
$vendor = __DIR__; | ||
while (!file_exists($vendor.'/vendor')) { | ||
$vendor = dirname($vendor); | ||
} | ||
require $vendor.'/vendor/autoload.php'; | ||
|
||
(new SingleCommandApplication()) | ||
->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 |
26 changes: 26 additions & 0 deletions
26
src/Symfony/Component/Console/Tests/phpt/single_application/version_name.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--TEST-- | ||
Single Application can be executed | ||
--ARGS-- | ||
--version | ||
--FILE-- | ||
<?php | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\SingleCommandApplication; | ||
|
||
$vendor = __DIR__; | ||
while (!file_exists($vendor.'/vendor')) { | ||
$vendor = dirname($vendor); | ||
} | ||
require $vendor.'/vendor/autoload.php'; | ||
lyrixx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
(new SingleCommandApplication()) | ||
->setCode(function (InputInterface $input, OutputInterface $output): int { | ||
return 0; | ||
}) | ||
->run() | ||
; | ||
?> | ||
--EXPECT-- | ||
Console Tool |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.