From 5100071caf5be10a3def75a2386544349025eb90 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Fri, 6 Apr 2018 18:17:35 +0200 Subject: [PATCH] [Console] add support for iterable in output --- UPGRADE-4.1.md | 2 ++ src/Symfony/Component/Console/CHANGELOG.md | 1 + src/Symfony/Component/Console/Output/Output.php | 4 +++- .../Component/Console/Output/OutputInterface.php | 10 +++++----- .../Component/Console/Style/SymfonyStyle.php | 6 ++++++ .../Component/Console/Tests/Output/OutputTest.php | 13 +++++++++++++ 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/UPGRADE-4.1.md b/UPGRADE-4.1.md index 807c028e5d0b6..6294722ad7c3d 100644 --- a/UPGRADE-4.1.md +++ b/UPGRADE-4.1.md @@ -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 ------------------- diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index 46d7f40453f63..7682feb593b86 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Component/Console/Output/Output.php b/src/Symfony/Component/Console/Output/Output.php index 20635a5f730d3..ab92dd830af28 100644 --- a/src/Symfony/Component/Console/Output/Output.php +++ b/src/Symfony/Component/Console/Output/Output.php @@ -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; diff --git a/src/Symfony/Component/Console/Output/OutputInterface.php b/src/Symfony/Component/Console/Output/OutputInterface.php index cddfbb49e075f..5742e553a9310 100644 --- a/src/Symfony/Component/Console/Output/OutputInterface.php +++ b/src/Symfony/Component/Console/Output/OutputInterface.php @@ -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); diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 311d0675b552e..6be6578476ac1 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -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); } diff --git a/src/Symfony/Component/Console/Tests/Output/OutputTest.php b/src/Symfony/Component/Console/Tests/Output/OutputTest.php index d8330d0bd3992..6ee2dbdb5cd4a 100644 --- a/src/Symfony/Component/Console/Tests/Output/OutputTest.php +++ b/src/Symfony/Component/Console/Tests/Output/OutputTest.php @@ -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 */