Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.1.0
-----

* Add `SingleCommandApplication`

5.0.0
-----

Expand Down
55 changes: 55 additions & 0 deletions src/Symfony/Component/Console/SingleCommandApplication.php
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;
}
}
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!
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!
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
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
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';

(new SingleCommandApplication())
->setCode(function (InputInterface $input, OutputInterface $output): int {
return 0;
})
->run()
;
?>
--EXPECT--
Console Tool