8000 [Console] add support for iterable in output · symfony/symfony@5100071 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5100071

Browse files
committed
[Console] add support for iterable in output
1 parent 368a0c3 commit 5100071

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

UPGRADE-4.1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Console
1616
* Deprecated the `getHorizontalBorderChar()` method in favor of the `getBorderChars()` method in `TableStyle`.
1717
* Deprecated the `setVerticalBorderChar()` method in favor of the `setVerticalBorderChars()` method in `TableStyle`.
1818
* Deprecated the `getVerticalBorderChar()` method in favor of the `getBorderChars()` method in `TableStyle`.
19+
* Added support for `iterable` messages in `write` and `writeln` methods of `Symfony\Component\Console\Output\OutputInterface`.
20+
If you have a custom implementation of the interface, you should make sure it works with iterable as well.
1921

2022
DependencyInjection
2123
-------------------

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

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

1011
4.0.0
1112
-----

src/Symfony/Component/Console/Output/Output.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ public function writeln($messages, $options = self::OUTPUT_NORMAL)
137137
*/
138138
public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
139139
{
140-
$messages = (array) $messages;
140+
if (!is_iterable($messages)) {
141+
$messages = array($messages);
142+
}
141143

142144
$types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN;
143145
$type = $types & $options ?: self::OUTPUT_NORMAL;

src/Symfony/Component/Console/Output/OutputInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ interface OutputInterface
3333
/**
3434
* Writes a message to the output.
3535
*
36-
* @param string|array $messages The message as an array of lines or a single string
37-
* @param bool $newline Whether to add a newline
38-
* @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
36+
* @param string|iterable $messages The message as an iterable of lines or a single string
37+
* @param bool $newline Whether to add a newline
38+
* @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
3939
*/
4040
public function write($messages, $newline = false, $options = 0);
4141

4242
/**
4343
* Writes a message to the output and adds a newline at the end.
4444
*
45-
* @param string|array $messages The message as an array of lines of a single string
46-
* @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
45+
* @param string|iterable $messages The message as an iterable of lines of a single string
46+
* @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
4747
*/
4848
public function writeln($messages, $options = 0);
4949

src/Symfony/Component/Console/Style/SymfonyStyle.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ public function askQuestion(Question $question)
306306
*/
307307
public function writeln($messages, $type = self::OUTPUT_NORMAL)
308308
{
309+
if ($messages instanceof \Traversable) {
310+
$messages = iterator_to_array($messages, false);
311+
}
309312
parent::writeln($messages, $type);
310313
$this->bufferedOutput->writeln($this->reduceBuffer($messages), $type);
311314
}
@@ -315,6 +318,9 @@ public function writeln($messages, $type = self::OUTPUT_NORMAL)
315318
*/
316319
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
317320
{
321+
if ($messages instanceof \Traversable) {
322+
$messages = iterator_to_array($messages, false);
323+
}
318324
parent::write($messages, $newline, $type);
319325
$this->bufferedOutput->write($this->reduceBuffer($messages), $newline, $type);
320326
}

src/Symfony/Component/Console/Tests/Output/OutputTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ public function testWriteAnArrayOfMessages()
8181
$this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an array of messages to output');
8282
}
8383

84+
public function testWriteAnIterableOfMessages()
85+
{
86+
$output = new TestOutput();
87+
$output->writeln($this->generateMessages());
88+
$this->assertEquals("foo\nbar\n", $output->output, '->writeln() can take an iterable of messages to output');
89+
}
90+
91+
private function generateMessages(): iterable
92+
{
93+
yield 'foo';
94+
yield 'bar';
95+
}
96+
8497
/**
8598
* @dataProvider provideWriteArguments
8699
*/

0 commit comments

Comments
 (0)
0