8000 Centralize input stream in base Input class · symfony/symfony@f47684c · GitHub
[go: up one dir, main page]

Skip to content

Commit f47684c

Browse files
committed
Centralize input stream in base Input class
Add StreamableInputInterface
1 parent ce28a86 commit f47684c

File tree

7 files changed

+135
-55
lines changed

7 files changed

+135
-55
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,8 @@ protected function configureIO(InputInterface $input, OutputInterface $output)
769769

770770
if (true === $input->hasParameterOption(array('--no-interaction', '-n'), true)) {
771771
$input->setInteractive(false);
772-
} elseif (function_exists('posix_isatty') && $this->getHelperSet()->has('question')) {
773-
$inputStream = $this->getHelperSet()->get('question')->getInputStream();
772+
} elseif (function_exists('posix_isatty')) {
773+
$inputStream = $input->getStream();
774774
if (!@posix_isatty($inputStream) && false === getenv('SHELL_INTERACTIVE')) {
775775
$input->setInteractive(false);
776776
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
5252
return $question->getDefault();
5353
}
5454

55+
if ($stream = $input->getStream()) {
56+
$this->inputStream = $stream;
57+
}
58+
5559
if (!$question->getValidator()) {
5660
return $this->doAsk($output, $question);
5761
}
@@ -74,6 +78,8 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
7478
*/
7579
public function setInputStream($stream)
7680
{
81+
@trigger_error(sprintf('The setInputStream() method is deprecated since version 3.1 and will be removed in 4.0. Use %s:setStream() instead.', InputInterface::class), E_USER_DEPRECATED);
82+
7783
if (!is_resource($stream)) {
7884
throw new InvalidArgumentException('Input stream must be a valid resource.');
7985
}
@@ -88,6 +94,8 @@ public function setInputStream($stream)
8894
*/
8995
public function getInputStream()
9096
{
97+
@trigger_error(sprintf('The getInputStream() method is deprecated since version 3.1 and will be removed in 4.0. Use %s:getStream() instead.', InputInterface::class), E_USER_DEPRECATED);
98+
9199
return $this->inputStream;
92100
}
93101

src/Symfony/Component/Console/Input/Input.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
*
2626
* @author Fabien Potencier <fabien@symfony.com>
2727
*/
28-
abstract class Input implements InputInterface
28+
abstract class Input implements InputInterface, StreamableInputInterface
2929
{
3030
/**
3131
* @var InputDefinition
3232
*/
3333
protected $definition;
34+
protected $stream;
3435
protected $options = array();
3536
protected $arguments = array();
3637
protected $interactive = true;
@@ -233,4 +234,20 @@ public function escapeToken($token)
233234
{
234235
return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
235236
}
237+
238+
/**
239+
* {@inheritdoc}
240+
*/
241+
public function setStream($stream)
242+
{
243+
$this->stream = $stream;
244+
}
245+
246+
/**
247+
* {@inheritdoc}
248+
*/
249+
public function getStream()
250+
{
251+
return $this->stream;
252+
}
236253
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
* StreamableInputInterface is the interface implemented by all input classes
16+
* that have an input stream.
17+
*
18+
* @author Robin Chalas <robin.chalas@gmail.com>
19+
*/
20+
interface StreamableInputInterface extends InputInterface
21+
{
22+
/**
23+
* Sets the input stream to read from when interacting with the user.
24+
*
25+
* This is mainly useful for testing purpose.
26+
*
27+
* @param resource $stream The input stream
28+
*/
29+
public function setStream($stream);
30+
31+
/**
32+
* Returns the input stream.
33+
*
34+
* @return resource|null
35+
*/
36+
public function getStream();
37+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ public function testCanCheckIfTerminalIsInteractive()
10991099

11001100
$this->assertFalse($tester->getInput()->hasParameterOption(array('--no-interaction', '-n')));
11011101

1102-
$inputStream = $application->getHelperSet()->get('question')->getInputStream();
1102+
$inputStream = $tester->getInput()->getStream();
11031103
$this->assertEquals($tester->getInput()->isInteractive(), @posix_isatty($inputStream));
11041104
}
11051105
}

0 commit comments

Comments
 (0)
0