Skip to content

Commit f3851b7

Browse files
committed
Remove OutputWrapperInterface
1 parent 5edfa27 commit f3851b7

File tree

6 files changed

+17
-67
lines changed

6 files changed

+17
-67
lines changed

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.

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] ?? '';

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) {

Helper/OutputWrapperInterface.php

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

Style/SymfonyStyle.php

Lines changed: 3 additions & 17 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,18 @@ class SymfonyStyle extends OutputStyle
4645
private ProgressBar $progressBar;
4746
private int $lineLength;
4847
private TrimmedBufferOutput $bufferedOutput;
49-
private OutputWrapperInterface $outputWrapper;
5048

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

6057
parent::__construct($this->output = $output);
6158
}
6259

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-
7560
/**
7661
* Formats a message as a block of text.
7762
*/
@@ -477,14 +462,15 @@ private function createBlock(iterable $messages, string $type = null, string $st
477462
}
478463

479464
// wrap and add newlines for each element
465+
$outputWrapper = new OutputWrapper();
480466
foreach ($messages as $key => $message) {
481467
if ($escape) {
482468
$message = OutputFormatter::escape($message);
483469
}
484470

485471
$lines = array_merge(
486472
$lines,
487-
explode(\PHP_EOL, $this->outputWrapper->wrap(
473+
explode(\PHP_EOL, $outputWrapper->wrap(
488474
$message,
489475
$this->lineLength - $prefixLength - $indentLength,
490476
\PHP_EOL

Tests/Helper/OutputWrapperTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ class OutputWrapperTest extends TestCase
1919
/**
2020
* @dataProvider textProvider
2121
*/
22-
public function testBasicWrap(string $text, int $width, ?bool $allowCutUrls, string $expected)
22+
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
}
@@ -36,7 +33,7 @@ public static function textProvider(): iterable
3633
yield 'Default URL cut' => [
3734
$baseTextWithUtf8AndUrl,
3835
20,
39-
null,
36+
false,
4037
<<<'EOS'
4138
Árvíztűrőtükörfúrógé
4239
p https://github.com/symfony/symfony Lorem ipsum

0 commit comments

Comments
 (0)