Skip to content

Commit b8b6774

Browse files
committed
bug #21841 [Console] Do not squash input changes made from console.command event (chalasr)
This PR was merged into the 2.8 branch. Discussion ---------- [Console] Do not squash input changes made from console.command event | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #19441 | License | MIT | Doc PR | n/a Setting arguments/options from the `console.command` event is expected to work since #15938 Commits ------- c8d364b [Console] Do not squash input changes made from console.command event
2 parents 8279055 + c8d364b commit b8b6774

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy