From 1cde7234c882429be28acfb309ea039f36e999a5 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Tue, 9 Jul 2013 11:28:35 +0200 Subject: [PATCH] [Console] Make DialogHelper respect interaction settings --- src/Symfony/Component/Console/Application.php | 7 ++++ .../Component/Console/Helper/DialogHelper.php | 6 +++- .../Component/Console/Helper/HelperSet.php | 7 +++- .../Console/Helper/InputAwareHelper.php | 33 +++++++++++++++++++ .../Console/Input/InputAwareInterface.php | 28 ++++++++++++++++ .../Console/Tests/Helper/DialogHelperTest.php | 13 ++++++++ .../Console/Tests/Helper/HelperSetTest.php | 17 ++++++++++ 7 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/Console/Helper/InputAwareHelper.php create mode 100644 src/Symfony/Component/Console/Input/InputAwareInterface.php diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index a3998a82e7..6cc3997d68 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -19,6 +19,7 @@ use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputAwareInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\ConsoleOutputInterface; @@ -895,6 +896,12 @@ class Application */ protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) { + foreach ($command->getHelperSet() as $helper) { + if ($helper instanceof InputAwareInterface) { + $helper->setInput($input); + } + } + if (null === $this->dispatcher) { return $command->run($input, $output); } diff --git a/src/Symfony/Component/Console/Helper/DialogHelper.php b/src/Symfony/Component/Console/Helper/DialogHelper.php index 7fc2b3a739..3d9aa88a06 100644 --- a/src/Symfony/Component/Console/Helper/DialogHelper.php +++ b/src/Symfony/Component/Console/Helper/DialogHelper.php @@ -19,7 +19,7 @@ use Symfony\Component\Console\Formatter\OutputFormatterStyle; * * @author Fabien Potencier */ -class DialogHelper extends Helper +class DialogHelper extends InputAwareHelper { private $inputStream; private static $shell; @@ -98,6 +98,10 @@ class DialogHelper extends Helper */ public function ask(OutputInterface $output, $question, $default = null, array $autocomplete = null) { + if ($this->input && !$this->input->isInteractive()) { + return $default; + } + $output->write($question); $inputStream = $this->inputStream ?: STDIN; diff --git a/src/Symfony/Component/Console/Helper/HelperSet.php b/src/Symfony/Component/Console/Helper/HelperSet.php index d95c9a3036..c317293d04 100644 --- a/src/Symfony/Component/Console/Helper/HelperSet.php +++ b/src/Symfony/Component/Console/Helper/HelperSet.php @@ -18,7 +18,7 @@ use Symfony\Component\Console\Command\Command; * * @author Fabien Potencier */ -class HelperSet +class HelperSet implements \IteratorAggregate { private $helpers; private $command; @@ -101,4 +101,9 @@ class HelperSet { return $this->command; } + + public function getIterator() + { + return new \ArrayIterator($this->helpers); + } } diff --git a/src/Symfony/Component/Console/Helper/InputAwareHelper.php b/src/Symfony/Component/Console/Helper/InputAwareHelper.php new file mode 100644 index 0000000000..0d91202029 --- /dev/null +++ b/src/Symfony/Component/Console/Helper/InputAwareHelper.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Helper; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputAwareInterface; + +/** + * An implementation of InputAwareInterface for Helpers. + * + * @author Wouter J + */ +abstract class InputAwareHelper extends Helper implements InputAwareInterface +{ + protected $input; + + /** + * {@inheritDoc} + */ + public function setInput(InputInterface $input) + { + $this->input = $input; + } +} diff --git a/src/Symfony/Component/Console/Input/InputAwareInterface.php b/src/Symfony/Component/Console/Input/InputAwareInterface.php new file mode 100644 index 0000000000..d2e6810b28 --- /dev/null +++ b/src/Symfony/Component/Console/Input/InputAwareInterface.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Console\Input; + +/** + * InputAwareInterface should be implemented by classes that depends on the + * Console Input. + * + * @author Wouter J + */ +interface InputAwareInterface +{ + /** + * Sets the Console Input. + * + * @param InputInterface + */ + public function setInput(InputInterface $input); +} diff --git a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php index 108f709bf4..849eba6ba8 100644 --- a/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Console\Tests\Helper; +use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Helper\DialogHelper; use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\FormatterHelper; @@ -153,6 +154,18 @@ class DialogHelperTest extends \PHPUnit_Framework_TestCase } } + public function testNoInteration() + { + $dialog = new DialogHelper(); + + $input = new ArrayInput(array()); + $input->setInteractive(false); + + $dialog->setInput($input); + + $this->assertEquals('not yet', $dialog->ask($this->getOutputStream(), 'Do you have a job?', 'not yet')); + } + protected function getInputStream($input) { $stream = fopen('php://memory', 'r+', false); diff --git a/src/Symfony/Component/Console/Tests/Helper/HelperSetTest.php b/src/Symfony/Component/Console/Tests/Helper/HelperSetTest.php index c9832736a2..a6ccde2d23 100644 --- a/src/Symfony/Component/Console/Tests/Helper/HelperSetTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/HelperSetTest.php @@ -111,6 +111,23 @@ class HelperSetTest extends \PHPUnit_Framework_TestCase $this->assertEquals($cmd, $helperset->getCommand(), '->getCommand() retrieves stored command'); } + /** + * @covers \Symfony\Component\Console\Helper\HelperSet::getIterator + */ + public function testIteration() + { + $helperset = new HelperSet(); + $helperset->set($this->getGenericMockHelper('fake_helper_01', $helperset)); + $helperset->set($this->getGenericMockHelper('fake_helper_02', $helperset)); + + $helpers = array('fake_helper_01', 'fake_helper_02'); + $i = 0; + + foreach ($helperset as $helper) { + $this->assertEquals($helpers[$i++], $helper->getName()); + } + } + /** * Create a generic mock for the helper interface. Optionally check for a call to setHelperSet with a specific * helperset instance.