Skip to content

Commit 130e1bf

Browse files
committed
Merge branch '5.1' into 5.2
* 5.1: Fix test. [PhpUnitBridge] Fix qualification of deprecations triggered by the debug class loader Improve return phpdoc for Normalizer Use a partial buffer in SymfonyStyle Fix console closing tag Fix typo in comment [VarDumper] fix casting resources turned into objects on PHP 8
2 parents d1d8b8f + e5e1476 commit 130e1bf

File tree

6 files changed

+109
-5
lines changed

6 files changed

+109
-5
lines changed

Formatter/OutputFormatter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ class OutputFormatter implements WrappableOutputFormatterInterface
2525
private $styles = [];
2626
private $styleStack;
2727

28+
public function __clone()
29+
{
30+
$this->styleStack = clone $this->styleStack;
31+
foreach ($this->styles as $key => $value) {
32+
$this->styles[$key] = clone $value;
33+
}
34+
}
35+
2836
/**
2937
* Escapes "<" special char in given text.
3038
*

Output/TrimmedBufferOutput.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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(
28+
?int $verbosity = self::VERBOSITY_NORMAL,
29+
bool $decorated = false,
30+
OutputFormatterInterface $formatter = null,
31+
int $maxLength
32+
) {
33+
if ($maxLength <= 0) {
34+
throw new InvalidArgumentException(sprintf('"%s()" expects a strictly positive maxLength. Got %d.', __METHOD__, $maxLength));
35+
}
36+
37+
parent::__construct($verbosity, $decorated, $formatter);
38+
$this->maxLength = $maxLength;
39+
}
40+
41+
/**
42+
* Empties buffer and returns its content.
43+
*
44+
* @return string
45+
*/
46+
public function fetch()
47+
{
48+
$content = $this->buffer;
49+
$this->buffer = '';
50+
51+
return $content;
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
protected function doWrite($message, $newline)
58+
{
59+
$this->buffer .= $message;
60+
61+
if ($newline) {
62+
$this->buffer .= \PHP_EOL;
63+
}
64+
65+
$this->buffer = substr($this->buffer, 0 - $this->maxLength);
66+
}
67+
}

Style/SymfonyStyle.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
use Symfony\Component\Console\Helper\TableCell;
2222
use Symfony\Component\Console\Helper\TableSeparator;
2323
use Symfony\Component\Console\Input\InputInterface;
24-
use Symfony\Component\Console\Output\BufferedOutput;
2524
use Symfony\Component\Console\Output\OutputInterface;
25+
use Symfony\Component\Console\Output\TrimmedBufferOutput;
2626
use Symfony\Component\Console\Question\ChoiceQuestion;
2727
use Symfony\Component\Console\Question\ConfirmationQuestion;
2828
use Symfony\Component\Console\Question\Question;
@@ -46,7 +46,7 @@ class SymfonyStyle extends OutputStyle
4646
public function __construct(InputInterface $input, OutputInterface $output)
4747
{
4848
$this->input = $input;
49-
$this->bufferedOutput = new BufferedOutput($output->getVerbosity(), false, clone $output->getFormatter());
49+
$this->bufferedOutput = new TrimmedBufferOutput($output->getVerbosity(), false, clone $output->getFormatter(), \DIRECTORY_SEPARATOR === '\\' ? 4 : 2);
5050
// Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
5151
$width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;
5252
$this->lineLength = min($width - (int) (\DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
@@ -454,9 +454,8 @@ private function autoPrependText(): void
454454

455455
private function writeBuffer(string $message, bool $newLine, int $type): void
456456
{
457-
// We need to know if the two last chars are PHP_EOL
458-
// Preserve the last 4 chars inserted (PHP_EOL on windows is two chars) in the history buffer
459-
$this->bufferedOutput->write(substr($message, -4), $newLine, $type);
457+
// We need to know if the last chars are PHP_EOL
458+
$this->bufferedOutput->write($message, $newLine, $type);
460459
}
461460

462461
private function createBlock(iterable $messages, string $type = null, string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = false): array
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 that closing tag is applied once
8+
return function (InputInterface $input, OutputInterface $output) {
9+
$output->setDecorated(true);
10+
$output = new SymfonyStyle($input, $output);
11+
$output->write('<question>do you want <comment>something</>');
12+
$output->writeln('?</>');
13+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
do you want something?

Tests/Style/SymfonyStyleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Command\Command;
1616
use Symfony\Component\Console\Formatter\OutputFormatter;
17+
use Symfony\Component\Console\Input\ArrayInput;
1718
use Symfony\Component\Console\Input\InputInterface;
1819
use Symfony\Component\Console\Output\ConsoleOutputInterface;
20+
use Symfony\Component\Console\Output\NullOutput;
1921
use Symfony\Component\Console\Output\OutputInterface;
2022
use Symfony\Component\Console\Style\SymfonyStyle;
2123
use Symfony\Component\Console\Tester\CommandTester;
@@ -115,4 +117,18 @@ public function testGetErrorStyleUsesTheCurrentOutputIfNoErrorOutputIsAvailable(
115117

116118
$this->assertInstanceOf(SymfonyStyle::class, $style->getErrorStyle());
117119
}
120+
121+
public function testMemoryConsumption()
122+
{
123+
$io = new SymfonyStyle(new ArrayInput([]), new NullOutput());
124+
$str = 'teststr';
125+
$io->writeln($str, SymfonyStyle::VERBOSITY_QUIET);
126+
$io->writeln($str, SymfonyStyle::VERBOSITY_QUIET);
127+
$start = memory_get_usage();
128+
for ($i = 0; $i < 100; ++$i) {
129+
$io->writeln($str, SymfonyStyle::VERBOSITY_QUIET);
130+
}
131+
132+
$this->assertSame(0, memory_get_usage() - $start);
133+
}
118134
}

0 commit comments

Comments
 (0)