master

laravel/framework

Last updated at: 29/12/2023 09:20

BufferedConsoleOutput.php

TLDR

This file contains the BufferedConsoleOutput class, which extends the ConsoleOutput class from the Symfony\Component\Console\Output namespace. The BufferedConsoleOutput class provides a buffer that accumulates output messages and allows fetching the content.

Methods

fetch

This method empties the buffer and returns its content as a string.

doWrite

This method overrides the doWrite method from the parent class. It appends the message and a newline character to the buffer, and then calls the parent doWrite method to output the message.

Classes

BufferedConsoleOutput

This class extends the ConsoleOutput class and provides a buffer for accumulating output messages. It has a property $buffer to hold the current buffer. The class includes the fetch and doWrite methods as described above.

<?php

namespace Illuminate\Console;

use Symfony\Component\Console\Output\ConsoleOutput;

class BufferedConsoleOutput extends ConsoleOutput
{
    /**
     * The current buffer.
     *
     * @var string
     */
    protected $buffer = '';

    /**
     * Empties the buffer and returns its content.
     *
     * @return string
     */
    public function fetch()
    {
        return tap($this->buffer, function () {
            $this->buffer = '';
        });
    }

    /**
     * {@inheritdoc}
     */
    #[\Override]
    protected function doWrite(string $message, bool $newline): void
    {
        $this->buffer .= $message;

        if ($newline) {
            $this->buffer .= \PHP_EOL;
        }

        return parent::doWrite($message, $newline);
    }
}