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

Skip to content

Commit c8d364b

Browse files
committed
[Console] Do not squash input changes made from console.command event
1 parent cadc313 commit c8d364b

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,10 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
859859
// ignore invalid options/arguments for now, to allow the event listeners to customize the InputDefinition
860860
}
861861

862+
// don't bind the input again as it would override any input argument/option set from the command event in
863+
// addition to being useless
864+
$command->setInputBound(true);
865+
862866
$event = new ConsoleCommandEvent($command, $input, $output);
863867
$this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
864868

src/Symfony/Component/Console/Command/Command.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Command
4242
private $ignoreValidationErrors = false;
4343
private $applicationDefinitionMerged = false;
4444
private $applicationDefinitionMergedWithArgs = false;
45+
private $inputBound = false;
4546
private $code;
4647
private $synopsis = array();
4748
private $usages = array();
@@ -218,11 +219,13 @@ public function run(InputInterface $input, OutputInterface $output)
218219
$this->mergeApplicationDefinition();
219220

220221
// bind the input against the command specific arguments/options
221-
try {
222-
$input->bind($this->definition);
223-
} catch (ExceptionInterface $e) {
224-
if (!$this->ignoreValidationErrors) {
225-
throw $e;
222+
if (!$this->inputBound) {
223+
try {
224+
$input->bind($this->definition);
225+
} catch (ExceptionInterface $e) {
226+
if (!$this->ignoreValidationErrors) {
227+
throw $e;
228+
}
226229
}
227230
}
228231

@@ -678,6 +681,14 @@ public function asXml($asDom = false)
678681
return $output->fetch();
679682
}
680683

684+
/**
685+
* @internal
686+
*/
687+
public function setInputBound($inputBound)
688+
{
689+
$this->inputBound = $inputBound;
690+
}
691+
681692
/**
682693
* Validates a command name.
683694
*

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,31 @@ public function testRunWithDispatcherAddingInputOptions()
11131113
$this->assertEquals('some test value', $extraValue);
11141114
}
11151115

1116+
public function testUpdateInputFromConsoleCommandEvent()
1117+
{
1118+
$dispatcher = $this->getDispatcher();
1119+
$dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) {
1120+
$event->getInput()->setOption('extra', 'overriden');
1121+
});
1122+
1123+
$application = new Application();
1124+
$application->setDispatcher($dispatcher);
1125+
$application->setAutoExit(false);
1126+
1127+
$application
1128+
->register('foo')
1129+
->addOption('extra', null, InputOption::VALUE_REQUIRED)
1130+
->setCode(function (InputInterface $input, OutputInterface $output) {
1131+
$output->write('foo.');
1132+
})
1133+
;
1134+
1135+
$tester = new ApplicationTester($application);
1136+
$tester->run(array('command' => 'foo', '--extra' => 'original'));
1137+
1138+
$this->assertEquals('overriden', $tester->getInput()->getOption('extra'));
1139+
}
1140+
11161141
public function testTerminalDimensions()
11171142
{
11181143
$application = new Application();

0 commit comments

Comments
 (0)