Skip to content

[Console] Different approach on merging application definition #20054

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
Aug 13, 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
48 changes: 25 additions & 23 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ class Command
private $hidden = false;
private $help = '';
private $description = '';
private $fullDefinition;
private $ignoreValidationErrors = false;
private $applicationDefinitionMerged = false;
private $applicationDefinitionMergedWithArgs = false;
private $code;
private $synopsis = [];
private $usages = [];
Expand Down Expand Up @@ -98,6 +97,8 @@ public function setApplication(Application $application = null)
} else {
$this->helperSet = null;
}

$this->fullDefinition = null;
}

public function setHelperSet(HelperSet $helperSet)
Expand Down Expand Up @@ -205,16 +206,12 @@ protected function initialize(InputInterface $input, OutputInterface $output)
*/
public function run(InputInterface $input, OutputInterface $output)
{
// force the creation of the synopsis before the merge with the app definition
$this->getSynopsis(true);
$this->getSynopsis(false);

// add the application arguments and options
$this->mergeApplicationDefinition();

// bind the input against the command specific arguments/options
try {
$input->bind($this->definition);
$input->bind($this->getDefinition());
} catch (ExceptionInterface $e) {
if (!$this->ignoreValidationErrors) {
throw $e;
Expand Down Expand Up @@ -302,20 +299,19 @@ public function setCode(callable $code)
*/
public function mergeApplicationDefinition(bool $mergeArgs = true)
{
if (null === $this->application || (true === $this->applicationDefinitionMerged && ($this->applicationDefinitionMergedWithArgs || !$mergeArgs))) {
if (null === $this->application) {
return;
}

$this->definition->addOptions($this->application->getDefinition()->getOptions());

$this->applicationDefinitionMerged = true;
$this->fullDefinition = new InputDefinition();
$this->fullDefinition->setOptions($this->definition->getOptions());
$this->fullDefinition->addOptions($this->application->getDefinition()->getOptions());

if ($mergeArgs) {
$currentArguments = $this->definition->getArguments();
$this->definition->setArguments($this->application->getDefinition()->getArguments());
$this->definition->addArguments($currentArguments);

$this->applicationDefinitionMergedWithArgs = true;
$this->fullDefinition->setArguments($this->application->getDefinition()->getArguments());
$this->fullDefinition->addArguments($this->definition->getArguments());
} else {
$this->fullDefinition->setArguments($this->definition->getArguments());
}
}

Expand All @@ -334,7 +330,7 @@ public function setDefinition($definition)
$this->definition->setDefinition($definition);
}

$this->applicationDefinitionMerged = false;
$this->fullDefinition = null;

return $this;
}
Expand All @@ -346,11 +342,7 @@ public function setDefinition($definition)
*/
public function getDefinition()
{
if (null === $this->definition) {
throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', static::class));
}

return $this->definition;
return $this->fullDefinition ?? $this->getNativeDefinition();
}

/**
Expand All @@ -365,7 +357,11 @@ public function getDefinition()
*/
public function getNativeDefinition()
{
return $this->getDefinition();
if (null === $this->definition) {
throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', static::class));
}

return $this->definition;
}

/**
Expand All @@ -381,6 +377,9 @@ public function getNativeDefinition()
public function addArgument(string $name, int $mode = null, string $description = '', $default = null)
{
$this->definition->addArgument(new InputArgument($name, $mode, $description, $default));
if (null !== $this->fullDefinition) {
$this->fullDefinition->addArgument(new InputArgument($name, $mode, $description, $default));
}

return $this;
}
Expand All @@ -399,6 +398,9 @@ public function addArgument(string $name, int $mode = null, string $description
public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null)
{
$this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
if (null !== $this->fullDefinition) {
$this->fullDefinition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
}

return $this;
}
Expand Down
24 changes: 5 additions & 19 deletions src/Symfony/Component/Console/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -32,7 +31,11 @@ protected function configure()
{
$this
->setName('list')
->setDefinition($this->createDefinition())
->setDefinition([
new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
])
->setDescription('Lists commands')
->setHelp(<<<'EOF'
The <info>%command.name%</info> command lists all commands:
Expand All @@ -55,14 +58,6 @@ protected function configure()
;
}

/**
* {@inheritdoc}
*/
public function getNativeDefinition()
{
return $this->createDefinition();
}

/**
* {@inheritdoc}
*/
Expand All @@ -77,13 +72,4 @@ protected function execute(InputInterface $input, OutputInterface $output)

return 0;
}

private function createDefinition(): InputDefinition
{
return new InputDefinition([
new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'),
new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'),
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'),
]);
}
}
3 changes: 1 addition & 2 deletions src/Symfony/Component/Console/Descriptor/JsonDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,14 @@ private function getInputDefinitionData(InputDefinition $definition): array

private function getCommandData(Command $command): array
{
$command->getSynopsis();
$command->mergeApplicationDefinition(false);

return [
'name' => $command->getName(),
'usage' => array_merge([$command->getSynopsis()], $command->getUsages(), $command->getAliases()),
'description' => $command->getDescription(),
'help' => $command->getProcessedHelp(),
'definition' => $this->getInputDefinitionData($command->getNativeDefinition()),
'definition' => $this->getInputDefinitionData($command->getDefinition()),
'hidden' => $command->isHidden(),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
*/
protected function describeCommand(Command $command, array $options = [])
{
$command->getSynopsis();
$command->mergeApplicationDefinition(false);

$this->write(
Expand All @@ -136,9 +135,10 @@ protected function describeCommand(Command $command, array $options = [])
$this->write($help);
}

if ($command->getNativeDefinition()) {
$definition = $command->getDefinition();
if ($definition->getOptions() || $definition->getArguments()) {
$this->write("\n\n");
$this->describeInputDefinition($command->getNativeDefinition());
$this->describeInputDefinition($definition);
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Component/Console/Descriptor/TextDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
*/
protected function describeCommand(Command $command, array $options = [])
{
$command->getSynopsis(true);
$command->getSynopsis(false);
$command->mergeApplicationDefinition(false);

if ($description = $command->getDescription()) {
Expand All @@ -154,7 +152,7 @@ protected function describeCommand(Command $command, array $options = [])
}
$this->writeText("\n");

$definition = $command->getNativeDefinition();
$definition = $command->getDefinition();
if ($definition->getOptions() || $definition->getArguments()) {
$this->writeText("\n");
$this->describeInputDefinition($definition, $options);
Expand Down
3 changes: 1 addition & 2 deletions src/Symfony/Component/Console/Descriptor/XmlDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function getCommandDocument(Command $command): \DOMDocument
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($commandXML = $dom->createElement('command'));

$command->getSynopsis();
$command->mergeApplicationDefinition(false);

$commandXML->setAttribute('id', $command->getName());
Expand All @@ -68,7 +67,7 @@ public function getCommandDocument(Command $command): \DOMDocument
$commandXML->appendChild($helpXML = $dom->createElement('help'));
$helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $command->getProcessedHelp())));

$definitionXML = $this->getInputDefinitionDocument($command->getNativeDefinition());
$definitionXML = $this->getInputDefinitionDocument($command->getDefinition());
$this->appendDocument($commandXML, $definitionXML->getElementsByTagName('definition')->item(0));

return $dom;
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,12 @@ public function testRun()
$tester->run(['command' => 'list', '-vvv' => true]);
$this->assertSame(Output::VERBOSITY_DEBUG, $tester->getOutput()->getVerbosity(), '->run() sets the output to verbose if -v is passed');

$tester->run(['command' => 'help', '--help' => true], ['decorated' => false]);
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run5.txt', $tester->getDisplay(true), '->run() displays the help if --help is passed');

$tester->run(['command' => 'help', '-h' => true], ['decorated' => false]);
$this->assertStringEqualsFile(self::$fixturesPath.'/application_run5.txt', $tester->getDisplay(true), '->run() displays the help if -h is passed');

$application = new Application();
$application->setAutoExit(false);
$application->setCatchExceptions(false);
Expand Down
63 changes: 63 additions & 0 deletions src/Symfony/Component/Console/Tests/Fixtures/application_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,69 @@
"is_multiple": false,
"description": "The output format (txt, xml, json, or md)",
"default": "txt"
},
"help": {
"name": "--help",
"shortcut": "-h",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Display this help message",
"default": false
},
"quiet": {
"name": "--quiet",
"shortcut": "-q",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Do not output any message",
"default": false
},
"verbose": {
"name": "--verbose",
"shortcut": "-v|-vv|-vvv",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug",
"default": false
},
"version": {
"name": "--version",
"shortcut": "-V",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Display this application version",
"default": false
},
"ansi": {
"name": "--ansi",
"shortcut": "",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Force ANSI output",
"default": false
},
"no-ansi": {
"name": "--no-ansi",
"shortcut": "",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Disable ANSI output",
"default": false
},
"no-interaction": {
"name": "--no-interaction",
"shortcut": "-n",
"accept_value": false,
"is_value_required": false,
"is_multiple": false,
"description": "Do not ask any interactive question",
"default": false
}
}
}
Expand Down
63 changes: 63 additions & 0 deletions src/Symfony/Component/Console/Tests/Fixtures/application_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,66 @@ The output format (txt, xml, json, or md)
* Is value required: yes
* Is multiple: no
* Default: `'txt'`

#### `--help|-h`

Display this help message

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--quiet|-q`

Do not output any message

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--verbose|-v|-vv|-vvv`

Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--version|-V`

Display this application version

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--ansi`

Force ANSI output

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--no-ansi`

Disable ANSI output

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`

#### `--no-interaction|-n`

Do not ask any interactive question

* Accept value: no
* Is value required: no
* Is multiple: no
* Default: `false`
Loading