8000 merged branch WouterJ/issue_8079 (PR #8452) · symfony/symfony@4b383c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b383c7

Browse files
committed
merged branch WouterJ/issue_8079 (PR #8452)
This PR was squashed before being merged into the master branch (closes #8452). Discussion ---------- [Console] Make DialogHelper respect interaction settings | Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #8079 | License | MIT | Doc PR | - This is based on @cordoval's #8366, but it tries to not break BC and to be a little more userfriendly. @stof I can't seem to follow the infinite loop you talked about in #8366 . `DialogHelper::ask` will return the default, which is `null`, that breaks the while loop and it returns the default. Commits ------- 1cde723 [Console] Make DialogHelper respect interaction settings
2 parents a0721af + 1cde723 commit 4b383c7

File tree

7 files changed

+109
-2
lines changed

7 files changed

+109
-2
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Input\InputDefinition;
2020
use Symfony\Component\Console\Input\InputOption;
2121
use Symfony\Component\Console\Input\InputArgument;
22+
use Symfony\Component\Console\Input\InputAwareInterface;
2223
use Symfony\Component\Console\Output\OutputInterface;
2324
use Symfony\Component\Console\Output\ConsoleOutput;
2425
use Symfony\Component\Console\Output\ConsoleOutputInterface;
@@ -893,6 +894,12 @@ protected function configureIO(InputInterface $input, OutputInterface $output)
893894
*/
894895
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
895896
{
897+
foreach ($command->getHelperSet() as $helper) {
898+
if ($helper instanceof InputAwareInterface) {
899+
$helper->setInput($input);
900+
}
901+
}
902+
896903
if (null === $this->dispatcher) {
897904
return $command->run($input, $output);
898905
}

src/Symfony/Component/Console/Helper/DialogHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Fabien Potencier <fabien@symfony.com>
2121
*/
22-
class DialogHelper extends Helper
22+
class DialogHelper extends InputAwareHelper
2323
{
2424
private $inputStream;
2525
private static $shell;
@@ -98,6 +98,10 @@ public function select(OutputInterface $output, $question, $choices, $default =
9898
*/
9999
public function ask(OutputInterface $output, $question, $default = null, array $autocomplete = null)
100100
{
101+
if ($this->input && !$this->input->isInteractive()) {
102+
return $default;
103+
}
104+
101105
$output->write($question);
102106

103107
$inputStream = $this->inputStream ?: STDIN;

src/Symfony/Component/Console/Helper/HelperSet.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Fabien Potencier <fabien@symfony.com>
2020
*/
21-
class HelperSet
21+
class HelperSet implements \IteratorAggregate
2222
{
2323
private $helpers;
2424
private $command;
@@ -101,4 +101,9 @@ public function getCommand()
101101
{
102102
return $this->command;
103103
}
104+
105+
public function getIterator()
106+
{
107+
return new \ArrayIterator($this->helpers);
108+
}
104109
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Helper;
13+
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Input\InputAwareInterface;
16+
17+
/**
18+
* An implementation of InputAwareInterface for Helpers.
19+
*
20+
* @author Wouter J <waldio.webdesign@gmail.com>
21+
*/
22+
abstract class InputAwareHelper extends Helper implements InputAwareInterface
23+
{
24+
protected $input;
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function setInput(InputInterface $input)
30+
{
31+
$this->input = $input;
32+
}
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Input;
13+
14+
/**
15+
* InputAwareInterface should be implemented by classes that depends on the
16+
* Console Input.
17+
*
18+
* @author Wouter J <waldio.webdesign@gmail.com>
19+
*/
20+
interface InputAwareInterface
21+
{
22+
/**
23+
* Sets the Console Input.
24+
*
25+
* @param InputInterface
26+
*/
27+
public function setInput(InputInterface $input);
28+
}

src/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php

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

1212
namespace Symfony\Component\Console\Tests\Helper;
1313

14+
use Symfony\Component\Console\Input\ArrayInput;
1415
use Symfony\Component\Console\Helper\DialogHelper;
1516
use Symfony\Component\Console\Helper\HelperSet;
1617
use Symfony\Component\Console\Helper\FormatterHelper;
@@ -153,6 +154,18 @@ public function testAskAndValidate()
153154
}
154155
}
155156

157+
public function testNoInteration()
158+
{
159+
$dialog = new DialogHelper();
160+
161+
$input = new ArrayInput(array());
162+
$input->setInteractive(false);
163+
164+
$dialog->setInput($input);
165+
166+
$this->assertEquals('not yet', $dialog->ask($this->getOutputStream(), 'Do you have a job?', 'not yet'));
167+
}
168+
156169
protected function getInputStream($input)
157170
{
158171
$stream = fopen('php://memory', 'r+', false);

src/Symfony/Component/Console/Tests/Helper/HelperSetTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,23 @@ public function testGetCommand()
111111
$this->assertEquals($cmd, $helperset->getCommand(), '->getCommand() retrieves stored command');
112112
}
113113

114+
/**
115+
* @covers \Symfony\Component\Console\Helper\HelperSet::getIterator
116+
*/
117+
public function testIteration()
118+
{
119+
$helperset = new HelperSet();
120+
$helperset->set($this->getGenericMockHelper('fake_helper_01', $helperset));
121+
$helperset->set($this->getGenericMockHelper('fake_helper_02', $helperset));
122+
123+
$helpers = array('fake_helper_01', 'fake_helper_02');
124+
$i = 0;
125+
126+
foreach ($helperset as $helper) {
127+
$this->assertEquals($helpers[$i++], $helper->getName());
128+
}
129+
}
130+
114131
/**
115132
* Create a generic mock for the helper interface. Optionally check for a call to setHelperSet with a specific
116133
* helperset instance.

0 commit comments

Comments
 (0)
0