| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Output; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\Console\Formatter\OutputFormatterInterface; |
| 16 |
|
| 17 |
/** |
| 18 |
* A BufferedOutput that keeps only the last N chars. |
| 19 |
* |
| 20 |
* @author Jérémy Derussé <[email protected]> |
| 21 |
*/ |
| 22 |
class TrimmedBufferOutput extends Output |
| 23 |
{ |
| 24 |
private $maxLength; |
| 25 |
private $buffer = ''; |
| 26 |
|
| 27 |
public function __construct(int $maxLength, ?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, ?OutputFormatterInterface $formatter = null) |
| 28 |
{ |
| 29 |
if ($maxLength <= 0) { |
| 30 |
throw new InvalidArgumentException(sprintf('"%s()" expects a strictly positive maxLength. Got %d.', __METHOD__, $maxLength)); |
| 31 |
} |
| 32 |
|
| 33 |
parent::__construct($verbosity, $decorated, $formatter); |
| 34 |
$this->maxLength = $maxLength; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Empties buffer and returns its content. |
| 39 |
* |
| 40 |
* @return string |
| 41 |
*/ |
| 42 |
public function fetch() |
| 43 |
{ |
| 44 |
$content = $this->buffer; |
| 45 |
$this->buffer = ''; |
| 46 |
|
| 47 |
return $content; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* {@inheritdoc} |
| 52 |
*/ |
| 53 |
protected function doWrite(string $message, bool $newline) |
| 54 |
{ |
| 55 |
$this->buffer .= $message; |
| 56 |
|
| 57 |
if ($newline) { |
| 58 |
$this->buffer .= \PHP_EOL; |
| 59 |
} |
| 60 |
|
| 61 |
$this->buffer = substr($this->buffer, 0 - $this->maxLength); |
| 62 |
} |
| 63 |
} |
| 64 |
|