8000 [Console] Support iterable in SymfonyStyle::write/writeln · symfony/symfony@d66827e · GitHub
[go: up one dir, main page]

Skip to content

Commit d66827e

Browse files
committed
[Console] Support iterable in SymfonyStyle::write/writeln
1 parent a59ef8f commit d66827e

File tree

3 files changed

+82
-12
lines changed

3 files changed

+82
-12
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -306,23 +306,29 @@ 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);
309+
if (!is_iterable($messages)) {
310+
$messages = array($messages);
311+
}
312+
313+
foreach ($messages as $message) {
314+
parent::writeln($message, $type);
315+
$this->writeBuffer($message, true, $type);
311316
}
312-
parent::writeln($messages, $type);
313-
$this->bufferedOutput->writeln($this->reduceBuffer($messages), $type);
314317
}
315318

316319
/**
317320
* {@inheritdoc}
318321
*/
319322
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
320323
{
321-
if ($messages instanceof \Traversable) {
322-
$messages = iterator_to_array($messages, false);
324+
if (!is_iterable($messages)) {
325+
$messages = array($messages);
326+
}
327+
328+
foreach ($messages as $message) {
329+
parent::write($message, $newline, $type);
330+
$this->writeBuffer($message, $newline, $type);
323331
}
324-
parent::write($messages, $newline, $type);
325-
$this->bufferedOutput->write($this->reduceBuffer($messages), $newline, $type);
326332
}
327333

328334
/**
@@ -375,13 +381,11 @@ private function autoPrependText(): void
375381
}
376382
}
377383

378-
private function reduceBuffer($messages): array
384+
private function writeBuffer(string $message, bool $newLine, int $type): void
379385
{
380386
// We need to know if the two last chars are PHP_EOL
381387
// Preserve the last 4 chars inserted (PHP_EOL on windows is two chars) in the history buffer
382-
return array_map(function ($value) {
383-
return substr($value, -4);
384-
}, array_merge(array($this->bufferedOutput->fetch()), (array) $messages));
388+
$this->bufferedOutput->write(substr($message, -4), $newLine, $type);
385389
}
386390

387391
private function createBlock(iterable $messages, string $type = null, string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = false)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Input\InputInterface;
4+
use Symfony\Component\Console\Output\OutputInterface;
5+
use Symfony\Component\Console\Style\SymfonyStyle;
6+
7+
//Ensure has single blank line after any text and a title
8+
return function (InputInterface $input, OutputInterface $output) {
9+
$output = new SymfonyStyle($input, $output);
10+
11+
$output->write('Lorem ipsum dolor sit amet');
12+
$output->title('First title');
13+
14+
$output->writeln('Lorem ipsum dolor sit amet');
15+
$output->title('Second title');
16+
17+
$output->write('Lorem ipsum dolor sit amet');
18+
$output->write('');
19+
$output->title('Third title');
20+
21+
//Ensure edge case by appending empty strings to history:
22+
$output->write('Lorem ipsum dolor sit amet');
23+
$output->write(new \ArrayIterator(array('', '', '')));
24+
$output->title('Fourth title');
25+
26+
//Ensure have manual control over number of blank lines:
27+
$output->writeln('Lorem ipsum dolor sit amet');
28+
$output->writeln(new \ArrayIterator(array('', ''))); //Should append an extra blank line
29+
$output->title('Fifth title');
30+
31+
$output->writeln('Lorem ipsum dolor sit amet');
32+
$output->newLine(2); //Should append an extra blank line
33+
$output->title('Fifth title');
34+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Lorem ipsum dolor sit amet
2+
3+
First title
4+
===========
5+
6+
Lorem ipsum dolor sit amet
7+
8+
Second title
9+
============
10+
11+
Lorem ipsum dolor sit amet
12+
13+
Third title
14+
===========
15+
16+
Lorem ipsum dolor sit amet
17+
18+
Fourth title
19+
============
20+
21+
Lorem ipsum dolor sit amet
22+
23+
24+
Fifth title
25+
===========
26+
27+
Lorem ipsum dolor sit amet
28+
29+
30+
Fifth title
31+
===========
32+

0 commit comments

Comments
 (0)
0