Skip to content
Permalink
Browse files

feature #26847 [Console] add support for iterable in output (Tobion)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[Console] add support for iterable in output

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | not really
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Very useful if you just want to print something from a generator. Using array requires everything in memory. Lets make use of the new iterable type.

Commits
-------

5100071 [Console] add support for iterable in output
  • Loading branch information...
Tobion committed Apr 7, 2018
2 parents 368a0c3 + 5100071 commit ec999c791fe0b6688d61ac24511d7495f4edaf45
@@ -16,6 +16,8 @@ Console
* Deprecated the `getHorizontalBorderChar()` method in favor of the `getBorderChars()` method in `TableStyle`.
* Deprecated the `setVerticalBorderChar()` method in favor of the `setVerticalBorderChars()` method in `TableStyle`.
* Deprecated the `getVerticalBorderChar()` method in favor of the `getBorderChars()` method in `TableStyle`.
* Added support for `iterable` messages in `write` and `writeln` methods of `Symfony\Component\Console\Output\OutputInterface`.
If you have a custom implementation of the interface, you should make sure it works with iterable as well.

DependencyInjection
-------------------
@@ -6,6 +6,7 @@ CHANGELOG

* added option to run suggested command if command is not found and only 1 alternative is available
* added option to modify console output and print multiple modifiable sections
* added support for iterable messages in output `write` and `writeln` methods

4.0.0
-----
@@ -137,7 +137,9 @@ public function writeln($messages, $options = self::OUTPUT_NORMAL)
*/
public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
{
$messages = (array) $messages;
if (!is_iterable($messages)) {
$messages = array($messages);
}
$types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN;
$type = $types & $options ?: self::OUTPUT_NORMAL;
@@ -33,17 +33,17 @@ interface OutputInterface
/**
* Writes a message to the output.
*
* @param string|array $messages The message as an array of lines or a single string
* @param bool $newline Whether to add a newline
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
* @param string|iterable $messages The message as an iterable of lines or a single string
* @param bool $newline Whether to add a newline
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
*/
public function write($messages, $newline = false, $options = 0);
/**
* Writes a message to the output and adds a newline at the end.
*
* @param string|array $messages The message as an array of lines of a single string
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
* @param string|iterable $messages The message as an iterable of lines of a single string
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
*/
public function writeln($messages, $options = 0);
@@ -306,6 +306,9 @@ public function askQuestion(Question $question)
*/
public function writeln($messages, $type = self::OUTPUT_NORMAL)
{
if ($messages instanceof \Traversable) {
$messages = iterator_to_array($messages, false);
}
parent::writeln($messages, $type);
$this->bufferedOutput->writeln($this->reduceBuffer($messages), $type);
}
@@ -315,6 +318,9 @@ public function writeln($messages, $type = self::OUTPUT_NORMAL)
*/
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
{
if ($messages instanceof \Traversable) {
$messages = iterator_to_array($messages, false);
}
parent::write($messages, $newline, $type);
$this->bufferedOutput->write($this->reduceBuffer($messages), $newline, $type);
}
@@ -81,6 +81,19 @@ public function testWriteAnArrayOfMessages()
$this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an array of messages to output');
}
public function testWriteAnIterableOfMessages()
{
$output = new TestOutput();
$output->writeln($this->generateMessages());
$this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an iterable of messages to output');
}
private function generateMessages(): iterable
{
yield 'foo';
yield 'bar';
}
/**
* @dataProvider provideWriteArguments
*/

0 comments on commit ec999c7

Please sign in to comment.
You can’t perform that action at this time.