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

Skip to content

Commit 0af1676

Browse files
committed
Bind input before executing the COMMAND event
1 parent 58ed076 commit 0af1676

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Console\Descriptor\TextDescriptor;
1515
use Symfony\Component\Console\Descriptor\XmlDescriptor;
16+
use Symfony\Component\Console\Exception\ExceptionInterface;
1617
use Symfony\Component\Console\Helper\DebugFormatterHelper;
1718
use Symfony\Component\Console\Helper\ProcessHelper;
1819
use Symfony\Component\Console\Helper\QuestionHelper;
@@ -880,6 +881,14 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
880881
return $command->run($input, $output);
881882
}
882883

884+
// bind before the console.command event, so the listeners have access to input options/arguments
885+
try {
886+
$command->mergeApplicationDefinition();
887+
$input->bind($command->getDefinition());
888+
} catch (ExceptionInterface $e) {
889+
// ignore invalid options/arguments for now, to allow the event listeners to customize the InputDefinition
890+
}
891+
883892
$event = new ConsoleCommandEvent($command, $input, $output);
884893
$this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
885894

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Console\Descriptor\TextDescriptor;
1515
use Symfony\Component\Console\Descriptor\XmlDescriptor;
16+
use Symfony\Component\Console\Exception\ExceptionInterface;
1617
use Symfony\Component\Console\Input\InputDefinition;
1718
use Symfony\Component\Console\Input\InputOption;
1819
use Symfony\Component\Console\Input\InputArgument;
@@ -231,7 +232,7 @@ public function run(InputInterface $input, OutputInterface $output)
231232
// bind the input against the command specific arguments/options
232233
try {
233234
$input->bind($this->definition);
234-
} catch (\Exception $e) {
235+
} catch (ExceptionInterface $e) {
235236
if (!$this->ignoreValidationErrors) {
236237
throw $e;
237238
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,63 @@ public function testRunWithDispatcherSkippingCommand()
943943
$this->assertEquals(ConsoleCommandEvent::RETURN_CODE_DISABLED, $exitCode);
944944
}
945945

946+
public function testRunWithDispatcherAccessingInputOptions()
947+
{
948+
$noInteractionValue = null;
949+
$quietValue = null;
950+
951+
$dispatcher = $this->getDispatcher();
952+
$dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) use (&$noInteractionValue, &$quietValue) {
953+
$input = $event->getInput();
954+
955+
$noInteractionValue = $input->getOption('no-interaction');
956+
$quietValue = $input->getOption('quiet');
957+
});
958+
959+
$application = new Application();
960+
$application->setDispatcher($dispatcher);
961+
$application->setAutoExit(false);
962+
963+
$application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
964+
$output->write('foo.');
965+
});
966+
967+
$tester = new ApplicationTester($application);
968+
$tester->run(array('command' => 'foo', '--no-interaction' => true));
969+
970+
$this->assertTrue($noInteractionValue);
971+
$this->assertFalse($quietValue);
972+
}
973+
974+
public function testRunWithDispatcherAddingInputOptions()
975+
{
976+
$extraValue = null;
977+
978+
$dispatcher = $this->getDispatcher();
979+
$dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) use (&$extraValue) {
980+
$definition = $event->getCommand()->getDefinition();
981+
$input = $event->getInput();
982+
983+
$definition->addOption(new InputOption('extra', null, InputOption::VALUE_REQUIRED));
984+
$input->bind($definition);
985+
986+
$extraValue = $input->getOption('extra');
987+
});
988+
989+
$application = new Application();
990+
$application->setDispatcher($dispatcher);
991+
$application->setAutoExit(false);
992+
993+
$application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
994+
$output->write('foo.');
995+
});
996+
997+
$tester = new ApplicationTester($application);
998+
$tester->run(array('command' => 'foo', '--extra' => 'some test value'));
999+
1000+
$this->assertEquals('some test value', $extraValue);
1001+
}
1002+
9461003
public function testTerminalDimensions()
9471004
{
9481005
$application = new Application();

0 commit comments

Comments
 (0)