-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Centralize input stream in base Input class #18999
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
use Symfony\Component\Console\Exception\RuntimeException; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\StreamableInputInterface; | ||
use Symfony\Component\Console\Output\ConsoleOutputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Formatter\OutputFormatterStyle; | ||
|
@@ -52,6 +53,10 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu | |
return $question->getDefault(); | ||
} | ||
|
||
if ($input instanceof StreamableInputInterface && $stream = $input->getStream()) { | ||
$this->inputStream = $stream; | ||
} | ||
|
||
if (!$question->getValidator()) { | ||
return $this->doAsk($output, $question); | ||
} | ||
|
@@ -68,12 +73,17 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu | |
* | ||
* This is mainly useful for testing purpose. | ||
* | ||
* @deprecated since version 3.2, to be removed in 4.0. Use | ||
* StreamableInputInterface::setStream() instead. | ||
* | ||
* @param resource $stream The input stream | ||
* | ||
* @throws InvalidArgumentException In case the stream is not a resource | ||
*/ | ||
public function setInputStream($stream) | ||
{ | ||
@trigger_error(sprintf('The %s() method is deprecated since version 3.2 and will be removed in 4.0. Use %s:setStream() instead.', __METHOD__, StreamableInputInterface::class), E_USER_DEPRECATED); | ||
|
||
if (!is_resource($stream)) { | ||
throw new InvalidArgumentException('Input stream must be a valid resource.'); | ||
} | ||
|
@@ -84,10 +94,17 @@ public function setInputStream($stream) | |
/** | ||
* Returns the helper's input stream. | ||
8000 * | ||
* @deprecated since version 3.2, to be removed in 4.0. Use | ||
* StreamableInputInterface::getStream() instead. | ||
* | ||
* @return resource | ||
*/ | ||
public function getInputStream() | ||
public function getInputStream($triggerDeprecationError = true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding a new argument here is a BC break There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #19072 |
||
{ | ||
if ($triggerDeprecationError) { | ||
@trigger_error(sprintf('The %s() method is deprecated since version 3.2 and will be removed in 4.0. Use %s:getStream() instead.', __METHOD__, StreamableInputInterface::class), E_USER_DEPRECATED); | ||
} | ||
|
||
return $this->inputStream; | ||
} | ||
|
||
8000
|
This file contains hidden or 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
37 changes: 37 additions & 0 deletions
37
src/Symfony/Component/Console/Input/StreamableInputInterface.php
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?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; | ||
|
||
/** | ||
* StreamableInputInterface is the interface implemented by all input classes | ||
* that have an input stream. | ||
* | ||
* @author Robin Chalas <robin.chalas@gmail.com> | ||
*/ | ||
interface StreamableInputInterface extends InputInterface | ||
{ | ||
/** | ||
* Sets the input stream to read from when interacting with the user. | ||
* | ||
* This is mainly useful for testing purpose. | ||
* | ||
* @param resource $stream The input stream | ||
*/ | ||
public function setStream($stream); | ||
|
||
/** | ||
* Returns the input stream. | ||
* | ||
* @return resource|null | ||
*/ | ||
public function getStream(); | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably not be removed entirely yet (for BC reasons) ?
Or should
QuestionHelper::setInputStream
&&QuestionHelper::getInputStream
methods call$input->setStream
&&$input->getStream
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.
Detecting a possible null helper then null inputStream here would be much logic.
I would prefer callingInput::setStream
inQuestionHelper::setInputStream
while setting theQuestionHelper::$inputStream
(for BC reasons too) then return it inQuestionHelper::getInputStream()
assuming that if it is set in the QuestionHelper, it will not be from the Input in the meantime.We can't access the Input from QuestionHelper
setInputStream
/getInputStream
, it don't take the input as member but only asask()
argument. Will write some ugly checks...