Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Do not squash input changes made from console.command event #21841

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
Mar 5, 2017
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
4 changes: 4 additions & 0 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,10 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
// ignore invalid options/arguments for now, to allow the event listeners to customize the InputDefinition
}

// don't bind the input again as it would override any input argument/option set from the command event in
// addition to being useless
$command->setInputBound(true);

$event = new ConsoleCommandEvent($command, $input, $output);
$this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);

Expand Down
21 changes: 16 additions & 5 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Command
private $ignoreValidationErrors = false;
private $applicationDefinitionMerged = false;
private $applicationDefinitionMergedWithArgs = false;
private $inputBound = false;
private $code;
private $synopsis = array();
private $usages = array();
Expand Down Expand Up @@ -218,11 +219,13 @@ public function run(InputInterface $input, OutputInterface $output)
$this->mergeApplicationDefinition();

// bind the input against the command specific arguments/options
try {
$input->bind($this->definition);
} catch (ExceptionInterface $e) {
if (!$this->ignoreValidationErrors) {
throw $e;
if (!$this->inputBound) {
try {
$input->bind($this->definition);
} catch (ExceptionInterface $e) {
if (!$this->ignoreValidationErrors) {
throw $e;
}
}
}

Expand Down Expand Up @@ -678,6 +681,14 @@ public function asXml($asDom = false)
return $output->fetch();
}

/**
* @internal
*/
public function setInputBound($inputBound)
{
$this->inputBound = $inputBound;
}

/**
* Validates a command name.
*
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,31 @@ public function testRunWithDispatcherAddingInputOptions()
$this->assertEquals('some test value', $extraValue);
}

public function testUpdateInputFromConsoleCommandEvent()
{
$dispatcher = $this->getDispatcher();
$dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) {
$event->getInput()->setOption('extra', 'overriden');
});

$application = new Application();
$application->setDispatcher($dispatcher);
$application->setAutoExit(false);

$application
->register('foo')
->addOption('extra', null, InputOption::VALUE_REQUIRED)
->setCode(function (InputInterface $input, OutputInterface $output) {
$output->write('foo.');
})
;

$tester = new ApplicationTester($application);
$tester->run(array('command' => 'foo', '--extra' => 'original'));

$this->assertEquals('overriden', $tester->getInput()->getOption('extra'));
}

public function testTerminalDimensions()
{
$application = new Application();
Expand Down