Skip to content

[Console] Make DialogHelper respect interaction settings #8452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -895,6 +896,12 @@ protected function configureIO(InputInterface $input, OutputInterface $output)
*/
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);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Console/Helper/DialogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class DialogHelper extends Helper
class DialogHelper extends InputAwareHelper
{
private $inputStream;
private static $shell;
Expand Down Expand Up @@ -98,6 +98,10 @@ public function select(OutputInterface $output, $question, $choices, $default =
*/
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;
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Console/Helper/HelperSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class HelperSet
class HelperSet implements \IteratorAggregate
{
private $helpers;
private $command;
Expand Down Expand Up @@ -101,4 +101,9 @@ public function getCommand()
{
return $this->command;
}

public function getIterator()
{
return new \ArrayIterator($this->helpers);
}
}
33 changes: 33 additions & 0 deletions src/Symfony/Component/Console/Helper/InputAwareHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <waldio.webdesign@gmail.com>
*/
abstract class InputAwareHelper extends Helper implements InputAwareInterface
{
protected $input;

/**
* {@inheritDoc}
*/
public function setInput(InputInterface $input)
{
$this->input = $input;
}
}
28 changes: 28 additions & 0 deletions src/Symfony/Component/Console/Input/InputAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lang: depend

* Console Input.
*
* @author Wouter J <waldio.webdesign@gmail.com>
*/
interface InputAwareInterface
{
/**
* Sets the Console Input.
*
* @param InputInterface
*/
public function setInput(InputInterface $input);
}
13 changes: 13 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -153,6 +154,18 @@ public function testAskAndValidate()
}
}

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);
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/HelperSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ public function testGetCommand()
$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.
Expand Down