8000 [Console] add support for iterable in output by Tobion · Pull Request #26847 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] add support for iterable in output #26847

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 1 commit into from
Apr 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UPGRADE-4.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Console/Output/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Console/Output/OutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Console/Style/SymfonyStyle.php
ED30
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Console/Tests/Output/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
0