From d66827eca436fae63b48c96d3731e544be0d7a6b Mon Sep 17 00:00:00 2001 From: Maxime Steinhausser Date: Sun, 8 Apr 2018 12:38:20 +0200 Subject: [PATCH] [Console] Support iterable in SymfonyStyle::write/writeln --- .../Component/Console/Style/SymfonyStyle.php | 28 ++++++++------- .../command/command_4_with_iterators.php | 34 +++++++++++++++++++ .../output/output_4_with_iterators.txt | 32 +++++++++++++++++ 3 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_4_with_iterators.php create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_4_with_iterators.txt diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 6be6578476ac1..08f86225c1b3c 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -306,11 +306,14 @@ public function askQuestion(Question $question) */ public function writeln($messages, $type = self::OUTPUT_NORMAL) { - if ($messages instanceof \Traversable) { - $messages = iterator_to_array($messages, false); + if (!is_iterable($messages)) { + $messages = array($messages); + } + + foreach ($messages as $message) { + parent::writeln($message, $type); + $this->writeBuffer($message, true, $type); } - parent::writeln($messages, $type); - $this->bufferedOutput->writeln($this->reduceBuffer($messages), $type); } /** @@ -318,11 +321,14 @@ 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); + if (!is_iterable($messages)) { + $messages = array($messages); + } + + foreach ($messages as $message) { + parent::write($message, $newline, $type); + $this->writeBuffer($message, $newline, $type); } - parent::write($messages, $newline, $type); - $this->bufferedOutput->write($this->reduceBuffer($messages), $newline, $type); } /** @@ -375,13 +381,11 @@ private function autoPrependText(): void } } - private function reduceBuffer($messages): array + private function writeBuffer(string $message, bool $newLine, int $type): void { // We need to know if the two last chars are PHP_EOL // Preserve the last 4 chars inserted (PHP_EOL on windows is two chars) in the history buffer - return array_map(function ($value) { - return substr($value, -4); - }, array_merge(array($this->bufferedOutput->fetch()), (array) $messages)); + $this->bufferedOutput->write(substr($message, -4), $newLine, $type); } private function createBlock(iterable $messages, string $type = null, string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = false) diff --git a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_4_with_iterators.php b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_4_with_iterators.php new file mode 100644 index 0000000000000..a644fb416e20c --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/command_4_with_iterators.php @@ -0,0 +1,34 @@ +write('Lorem ipsum dolor sit amet'); + $output->title('First title'); + + $output->writeln('Lorem ipsum dolor sit amet'); + $output->title('Second title'); + + $output->write('Lorem ipsum dolor sit amet'); + $output->write(''); + $output->title('Third title'); + + //Ensure edge case by appending empty strings to history: + $output->write('Lorem ipsum dolor sit amet'); + $output->write(new \ArrayIterator(array('', '', ''))); + $output->title('Fourth title'); + + //Ensure have manual control over number of blank lines: + $output->writeln('Lorem ipsum dolor sit amet'); + $output->writeln(new \ArrayIterator(array('', ''))); //Should append an extra blank line + $output->title('Fifth title'); + + $output->writeln('Lorem ipsum dolor sit amet'); + $output->newLine(2); //Should append an extra blank line + $output->title('Fifth title'); +}; diff --git a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_4_with_iterators.txt b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_4_with_iterators.txt new file mode 100644 index 0000000000000..2646d858e7cd3 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/output_4_with_iterators.txt @@ -0,0 +1,32 @@ +Lorem ipsum dolor sit amet + +First title +=========== + +Lorem ipsum dolor sit amet + +Second title +============ + +Lorem ipsum dolor sit amet + +Third title +=========== + +Lorem ipsum dolor sit amet + +Fourth title +============ + +Lorem ipsum dolor sit amet + + +Fifth title +=========== + +Lorem ipsum dolor sit amet + + +Fifth title +=========== +