Skip to content

Commit e23eb9b

Browse files
committed
Remove OutputWrapperInterface
1 parent 6131903 commit e23eb9b

File tree

6 files changed

+15
-64
lines changed

6 files changed

+15
-64
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ CHANGELOG
1212

1313
* Add support to display table vertically when calling setVertical()
1414
* Add method `__toString()` to `InputInterface`
15-
* Added `OutputWrapperInterface` and `OutputWrapper` to allow modifying your
16-
wrapping strategy in `SymfonyStyle` or in other `OutputStyle`. Eg: you can
17-
switch off to wrap URLs.
15+
* Added `OutputWrapper` to prevent truncated URL in `SymfonyStyle::createBlock`.
1816
* Deprecate `Command::$defaultName` and `Command::$defaultDescription`, use the `AsCommand` attribute instead
1917
* Add suggested values for arguments and options in input definition, for input completion
2018
* Add `$resumeAt` parameter to `ProgressBar#start()`, so that one can easily 'resume' progress on longer tasks, and still get accurate `getEstimate()` and `getRemaining()` results.

src/Symfony/Component/Console/Formatter/OutputFormatter.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Console\Formatter;
1313

1414
use Symfony\Component\Console\Exception\InvalidArgumentException;
15-
use Symfony\Component\Console\Helper\OutputWrapperInterface;
1615

1716
/**
1817
* Formatter class for console output.
@@ -145,8 +144,8 @@ public function formatAndWrap(?string $message, int $width)
145144

146145
$offset = 0;
147146
$output = '';
148-
$openTagRegex = OutputWrapperInterface::TAG_OPEN_REGEX_SEGMENT;
149-
$closeTagRegex = OutputWrapperInterface::TAG_CLOSE_REGEX_SEGMENT;
147+
$openTagRegex = '[a-z](?:[^\\\\<>]*+ | \\\\.)*';
148+
$closeTagRegex = '[a-z][^<>]*+';
150149
$currentLineLength = 0;
151150
preg_match_all("#<(($openTagRegex) | /($closeTagRegex)?)>#ix", $message, $matches, \PREG_OFFSET_CAPTURE);
152151
foreach ($matches[0] as $i => $match) {
@@ -162,7 +161,7 @@ public function formatAndWrap(?string $message, int $width)
162161
$offset = $pos + \strlen($text);
163162

164163
// opening tag?
165-
if ($open = ('/' !== $text[1])) {
164+
if ($open = '/' !== $text[1]) {
166165
$tag = $matches[1][$i][0];
167166
} else {
168167
$tag = $matches[3][$i][0] ?? '';

src/Symfony/Component/Console/Helper/OutputWrapper.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,15 @@
4242
*
4343
* @see https://stackoverflow.com/a/20434776/1476819
4444
*/
45-
final class OutputWrapper implements OutputWrapperInterface
45+
final class OutputWrapper
4646
{
47+
private const TAG_OPEN_REGEX_SEGMENT = '[a-z](?:[^\\\\<>]*+ | \\\\.)*';
48+
private const TAG_CLOSE_REGEX_SEGMENT = '[a-z][^<>]*+';
4749
private const URL_PATTERN = 'https?://\S+';
4850

49-
private bool $allowCutUrls = false;
50-
51-
public function isAllowCutUrls(): bool
52-
{
53-
return $this->allowCutUrls;
54-
}
55-
56-
public function setAllowCutUrls(bool $allowCutUrls)
57-
{
58-
$this->allowCutUrls = $allowCutUrls;
59-
60-
return $this;
51+
public function __construct(
52+
private bool $allowCutUrls = false
53+
) {
6154
}
6255

6356
public function wrap(string $text, int $width, string $break = "\n"): string
@@ -66,7 +59,7 @@ public function wrap(string $text, int $width, string $break = "\n"): string
6659
return $text;
6760
}
6861

69-
$tagPattern = sprintf('<(?:(?:%s)|/(?:%s)?)>', OutputWrapperInterface::TAG_OPEN_REGEX_SEGMENT, OutputWrapperInterface::TAG_CLOSE_REGEX_SEGMENT);
62+
$tagPattern = sprintf('<(?:(?:%s)|/(?:%s)?)>', self::TAG_OPEN_REGEX_SEGMENT, self::TAG_CLOSE_REGEX_SEGMENT);
7063
$limitPattern = "{1,$width}";
7164
$patternBlocks = [$tagPattern];
7265
if (!$this->allowCutUrls) {

src/Symfony/Component/Console/Helper/OutputWrapperInterface.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\Console\Formatter\OutputFormatter;
1717
use Symfony\Component\Console\Helper\Helper;
1818
use Symfony\Component\Console\Helper\OutputWrapper;
19-
use Symfony\Component\Console\Helper\OutputWrapperInterface;
2019
use Symfony\Component\Console\Helper\ProgressBar;
2120
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
2221
use Symfony\Component\Console\Helper\Table;
@@ -46,32 +45,19 @@ class SymfonyStyle extends OutputStyle
4645
private ProgressBar $progressBar;
4746
private int $lineLength;
4847
private TrimmedBufferOutput $bufferedOutput;
49-
private OutputWrapperInterface $outputWrapper;
48+
private OutputWrapper $outputWrapper;
5049

51-
public function __construct(InputInterface $input, OutputInterface $output, OutputWrapperInterface $outputWrapper = null)
50+
public function __construct(InputInterface $input, OutputInterface $output)
5251
{
5352
$this->input = $input;
5453
$this->bufferedOutput = new TrimmedBufferOutput(\DIRECTORY_SEPARATOR === '\\' ? 4 : 2, $output->getVerbosity(), false, clone $output->getFormatter());
5554
// Windows cmd wraps lines as soon as the terminal width is reached, whether there are following chars or not.
5655
$width = (new Terminal())->getWidth() ?: self::MAX_LINE_LENGTH;
5756
$this->lineLength = min($width - (int) (\DIRECTORY_SEPARATOR === '\\'), self::MAX_LINE_LENGTH);
58-
$this->outputWrapper = $outputWrapper ?: new OutputWrapper();
5957

6058
parent::__construct($this->output = $output);
6159
}
6260

63-
public function getOutputWrapper(): OutputWrapperInterface
64-
{
65-
return $this->outputWrapper;
66-
}
67-
68-
public function setOutputWrapper(OutputWrapperInterface $outputWrapper)
69-
{
70-
$this->outputWrapper = $outputWrapper;
71-
72-
return $this;
73-
}
74-
7561
/**
7662
* Formats a message as a block of text.
7763
*/
@@ -466,6 +452,7 @@ private function writeBuffer(string $message, bool $newLine, int $type): void
466452

467453
private function createBlock(iterable $messages, string $type = null, string $style = null, string $prefix = ' ', bool $padding = false, bool $escape = false): array
468454
{
455+
$this->outputWrapper ??= new OutputWrapper();
469456
$indentLength = 0;
470457
$prefixLength = Helper::width(Helper::removeDecoration($this->getFormatter(), $prefix));
471458
$lines = [];

src/Symfony/Component/Console/Tests/Helper/OutputWrapperTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ class OutputWrapperTest extends TestCase
2121
*/
2222
public function testBasicWrap(string $text, int $width, ?bool $allowCutUrls, string $expected)
2323
{
24-
$wrapper = new OutputWrapper();
25-
if (\is_bool($allowCutUrls)) {
26-
$wrapper->setAllowCutUrls($allowCutUrls);
27-
}
24+
$wrapper = new OutputWrapper($allowCutUrls);
2825
$result = $wrapper->wrap($text, $width);
2926
$this->assertEquals($expected, $result);
3027
}

0 commit comments

Comments
 (0)