| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Helper; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
> |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
final class OutputWrapper |
| 46 |
{ |
| 47 |
private const TAG_OPEN_REGEX_SEGMENT = '[a-z](?:[^\\\\<>]*+ | \\\\.)*'; |
| 48 |
private const TAG_CLOSE_REGEX_SEGMENT = '[a-z][^<>]*+'; |
| 49 |
private const URL_PATTERN = 'https?://\S+'; |
| 50 |
|
| 51 |
public function __construct( |
| 52 |
private bool $allowCutUrls = false, |
| 53 |
) { |
| 54 |
} |
| 55 |
|
| 56 |
public function wrap(string $text, int $width, string $break = "\n"): string |
| 57 |
{ |
| 58 |
if (!$width) { |
| 59 |
return $text; |
| 60 |
} |
| 61 |
|
| 62 |
$tagPattern = \sprintf('<(?:(?:%s)|/(?:%s)?)>', self::TAG_OPEN_REGEX_SEGMENT, self::TAG_CLOSE_REGEX_SEGMENT); |
| 63 |
$limitPattern = "{1,$width}"; |
| 64 |
$patternBlocks = [$tagPattern]; |
| 65 |
if (!$this->allowCutUrls) { |
| 66 |
$patternBlocks[] = self::URL_PATTERN; |
| 67 |
} |
| 68 |
$patternBlocks[] = '.'; |
| 69 |
$blocks = implode('|', $patternBlocks); |
| 70 |
$rowPattern = "(?:$blocks)$limitPattern"; |
| 71 |
$pattern = \sprintf('#(?:((?>(%1$s)((?<=[^\S\r\n])[^\S\r\n]?|(?=\r?\n)|$|[^\S\r\n]))|(%1$s))(?:\r?\n)?|(?:\r?\n|$))#imux', $rowPattern); |
| 72 |
$output = rtrim(preg_replace($pattern, '\\1'.$break, $text), $break); |
| 73 |
|
| 74 |
return str_replace(' '.$break, $break, $output); |
| 75 |
} |
| 76 |
} |