-
-
Save jzawadzki/62bfc3fec5afe7a93163deb08ddc1e29 to your computer and use it in GitHub Desktop.
Symfony Console - unable to change input values with event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require "vendor/autoload.php"; | |
use Symfony\Component\Console\Application; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\EventDispatcher\EventDispatcher; | |
use Symfony\Component\Console\Event\ConsoleCommandEvent; | |
use Symfony\Component\Console\ConsoleEvents; | |
class TestCommand extends Command | |
{ | |
public function configure() | |
{ | |
$this->setName('test') | |
->setDescription('test') | |
->addArgument('name', InputArgument::REQUIRED, 'Who do you want to greet?'); | |
} | |
public function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$output->writeln(sprintf("Hello %s!", $input->getArgument('name'))); | |
} | |
} | |
$dispatcher = new EventDispatcher(); | |
$app=new Application(); | |
$app->setDispatcher($dispatcher); | |
$app->add(new TestCommand()); | |
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) { | |
$input = $event->getInput(); | |
if($event->getCommand()->getName()=='test') { | |
$newValue='jerzy'; | |
$event->getOutput()->writeln(sprintf("Changing argument from %s to %s!", $input->getArgument('name'),$newValue)); | |
$input->setArgument('name', $newValue); | |
} | |
}); | |
$app->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name":"foo", | |
"require": { | |
"symfony/console":"~3.2", | |
"symfony/event-dispatcher":"~3.2" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ php app.php test fabien | |
Changing argument from fabien to jerzy! | |
Hello fabien! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment