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

[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 1 commit into from
Jan 9, 2020
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
[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
lyrixx committed Jan 8, 2020
commit 4af513d4499b85d08d066eb58bb0d10873b80294
5 changes: 5 additions & 0 deletions 5 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 55 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')
fabpot marked this conversation as resolved.
Show resolved Hide resolved
->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';
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.