Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 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\VarDumper\Dumper; |
| 13 | |
| 14 | use Symfony\Component\VarDumper\Cloner\Cursor; |
| 15 | use Symfony\Component\VarDumper\Cloner\Stub; |
| 16 | |
| 17 | /** |
| 18 | * CliDumper dumps variables for command line output. |
| 19 | * |
| 20 | * @author Nicolas Grekas <p@tchwork.com> |
| 21 | */ |
| 22 | class CliDumper extends AbstractDumper |
| 23 | { |
| 24 | public static $defaultColors; |
| 25 | public static $defaultOutput = 'php://stdout'; |
| 26 | |
| 27 | protected $colors; |
| 28 | protected $maxStringWidth = 0; |
| 29 | protected $styles = [ |
| 30 | // See http://en.wikipedia.org/wiki/ANSI_escape_code#graphics |
| 31 | 'default' => '0;38;5;208', |
| 32 | 'num' => '1;38;5;38', |
| 33 | 'const' => '1;38;5;208', |
| 34 | 'str' => '1;38;5;113', |
| 35 | 'note' => '38;5;38', |
| 36 | 'ref' => '38;5;247', |
| 37 | 'public' => '', |
| 38 | 'protected' => '', |
| 39 | 'private' => '', |
| 40 | 'meta' => '38;5;170', |
| 41 | 'key' => '38;5;113', |
| 42 | 'index' => '38;5;38', |
| 43 | ]; |
| 44 | |
| 45 | protected static $controlCharsRx = '/[\x00-\x1F\x7F]+/'; |
| 46 | protected static $controlCharsMap = [ |
| 47 | "\t" => '\t', |
| 48 | "\n" => '\n', |
| 49 | "\v" => '\v', |
| 50 | "\f" => '\f', |
| 51 | "\r" => '\r', |
| 52 | "\033" => '\e', |
| 53 | ]; |
| 54 | |
| 55 | protected $collapseNextHash = false; |
| 56 | protected $expandNextHash = false; |
| 57 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 58 | private array $displayOptions = [ |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 59 | 'fileLinkFormat' => null, |
| 60 | ]; |
| 61 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 62 | private bool $handlesHrefGracefully; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * {@inheritdoc} |
| 66 | */ |
| 67 | public function __construct($output = null, string $charset = null, int $flags = 0) |
| 68 | { |
| 69 | parent::__construct($output, $charset, $flags); |
| 70 | |
| 71 | if ('\\' === \DIRECTORY_SEPARATOR && !$this->isWindowsTrueColor()) { |
| 72 | // Use only the base 16 xterm colors when using ANSICON or standard Windows 10 CLI |
| 73 | $this->setStyles([ |
| 74 | 'default' => '31', |
| 75 | 'num' => '1;34', |
| 76 | 'const' => '1;31', |
| 77 | 'str' => '1;32', |
| 78 | 'note' => '34', |
| 79 | 'ref' => '1;30', |
| 80 | 'meta' => '35', |
| 81 | 'key' => '32', |
| 82 | 'index' => '34', |
| 83 | ]); |
| 84 | } |
| 85 | |
| 86 | $this->displayOptions['fileLinkFormat'] = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: 'file://%f#L%l'; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Enables/disables colored output. |
| 91 | */ |
| 92 | public function setColors(bool $colors) |
| 93 | { |
| 94 | $this->colors = $colors; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Sets the maximum number of characters per line for dumped strings. |
| 99 | */ |
| 100 | public function setMaxStringWidth(int $maxStringWidth) |
| 101 | { |
| 102 | $this->maxStringWidth = $maxStringWidth; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Configures styles. |
| 107 | * |
| 108 | * @param array $styles A map of style names to style definitions |
| 109 | */ |
| 110 | public function setStyles(array $styles) |
| 111 | { |
| 112 | $this->styles = $styles + $this->styles; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Configures display options. |
| 117 | * |
| 118 | * @param array $displayOptions A map of display options to customize the behavior |
| 119 | */ |
| 120 | public function setDisplayOptions(array $displayOptions) |
| 121 | { |
| 122 | $this->displayOptions = $displayOptions + $this->displayOptions; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * {@inheritdoc} |
| 127 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 128 | public function dumpScalar(Cursor $cursor, string $type, string|int|float|bool|null $value) |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 129 | { |
| 130 | $this->dumpKey($cursor); |
| 131 | |
| 132 | $style = 'const'; |
| 133 | $attr = $cursor->attr; |
| 134 | |
| 135 | switch ($type) { |
| 136 | case 'default': |
| 137 | $style = 'default'; |
| 138 | break; |
| 139 | |
| 140 | case 'integer': |
| 141 | $style = 'num'; |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 142 | |
| 143 | if (isset($this->styles['integer'])) { |
| 144 | $style = 'integer'; |
| 145 | } |
| 146 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 147 | break; |
| 148 | |
| 149 | case 'double': |
| 150 | $style = 'num'; |
| 151 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 152 | if (isset($this->styles['float'])) { |
| 153 | $style = 'float'; |
| 154 | } |
| 155 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 156 | switch (true) { |
| 157 | case \INF === $value: $value = 'INF'; break; |
| 158 | case -\INF === $value: $value = '-INF'; break; |
| 159 | case is_nan($value): $value = 'NAN'; break; |
| 160 | default: |
| 161 | $value = (string) $value; |
| 162 | if (!str_contains($value, $this->decimalPoint)) { |
| 163 | $value .= $this->decimalPoint.'0'; |
| 164 | } |
| 165 | break; |
| 166 | } |
| 167 | break; |
| 168 | |
| 169 | case 'NULL': |
| 170 | $value = 'null'; |
| 171 | break; |
| 172 | |
| 173 | case 'boolean': |
| 174 | $value = $value ? 'true' : 'false'; |
| 175 | break; |
| 176 | |
| 177 | default: |
| 178 | $attr += ['value' => $this->utf8Encode($value)]; |
| 179 | $value = $this->utf8Encode($type); |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | $this->line .= $this->style($style, $value, $attr); |
| 184 | |
| 185 | $this->endValue($cursor); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * {@inheritdoc} |
| 190 | */ |
| 191 | public function dumpString(Cursor $cursor, string $str, bool $bin, int $cut) |
| 192 | { |
| 193 | $this->dumpKey($cursor); |
| 194 | $attr = $cursor->attr; |
| 195 | |
| 196 | if ($bin) { |
| 197 | $str = $this->utf8Encode($str); |
| 198 | } |
| 199 | if ('' === $str) { |
| 200 | $this->line .= '""'; |
| 201 | $this->endValue($cursor); |
| 202 | } else { |
| 203 | $attr += [ |
| 204 | 'length' => 0 <= $cut ? mb_strlen($str, 'UTF-8') + $cut : 0, |
| 205 | 'binary' => $bin, |
| 206 | ]; |
| 207 | $str = $bin && false !== strpos($str, "\0") ? [$str] : explode("\n", $str); |
| 208 | if (isset($str[1]) && !isset($str[2]) && !isset($str[1][0])) { |
| 209 | unset($str[1]); |
| 210 | $str[0] .= "\n"; |
| 211 | } |
| 212 | $m = \count($str) - 1; |
| 213 | $i = $lineCut = 0; |
| 214 | |
| 215 | if (self::DUMP_STRING_LENGTH & $this->flags) { |
| 216 | $this->line .= '('.$attr['length'].') '; |
| 217 | } |
| 218 | if ($bin) { |
| 219 | $this->line .= 'b'; |
| 220 | } |
| 221 | |
| 222 | if ($m) { |
| 223 | $this->line .= '"""'; |
| 224 | $this->dumpLine($cursor->depth); |
| 225 | } else { |
| 226 | $this->line .= '"'; |
| 227 | } |
| 228 | |
| 229 | foreach ($str as $str) { |
| 230 | if ($i < $m) { |
| 231 | $str .= "\n"; |
| 232 | } |
| 233 | if (0 < $this->maxStringWidth && $this->maxStringWidth < $len = mb_strlen($str, 'UTF-8')) { |
| 234 | $str = mb_substr($str, 0, $this->maxStringWidth, 'UTF-8'); |
| 235 | $lineCut = $len - $this->maxStringWidth; |
| 236 | } |
| 237 | if ($m && 0 < $cursor->depth) { |
| 238 | $this->line .= $this->indentPad; |
| 239 | } |
| 240 | if ('' !== $str) { |
| 241 | $this->line .= $this->style('str', $str, $attr); |
| 242 | } |
| 243 | if ($i++ == $m) { |
| 244 | if ($m) { |
| 245 | if ('' !== $str) { |
| 246 | $this->dumpLine($cursor->depth); |
| 247 | if (0 < $cursor->depth) { |
| 248 | $this->line .= $this->indentPad; |
| 249 | } |
| 250 | } |
| 251 | $this->line .= '"""'; |
| 252 | } else { |
| 253 | $this->line .= '"'; |
| 254 | } |
| 255 | if ($cut < 0) { |
| 256 | $this->line .= '…'; |
| 257 | $lineCut = 0; |
| 258 | } elseif ($cut) { |
| 259 | $lineCut += $cut; |
| 260 | } |
| 261 | } |
| 262 | if ($lineCut) { |
| 263 | $this->line .= '…'.$lineCut; |
| 264 | $lineCut = 0; |
| 265 | } |
| 266 | |
| 267 | if ($i > $m) { |
| 268 | $this->endValue($cursor); |
| 269 | } else { |
| 270 | $this->dumpLine($cursor->depth); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * {@inheritdoc} |
| 278 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 279 | public function enterHash(Cursor $cursor, int $type, string|int|null $class, bool $hasChild) |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 280 | { |
| 281 | if (null === $this->colors) { |
| 282 | $this->colors = $this->supportsColors(); |
| 283 | } |
| 284 | |
| 285 | $this->dumpKey($cursor); |
| 286 | $attr = $cursor->attr; |
| 287 | |
| 288 | if ($this->collapseNextHash) { |
| 289 | $cursor->skipChildren = true; |
| 290 | $this->collapseNextHash = $hasChild = false; |
| 291 | } |
| 292 | |
| 293 | $class = $this->utf8Encode($class); |
| 294 | if (Cursor::HASH_OBJECT === $type) { |
| 295 | $prefix = $class && 'stdClass' !== $class ? $this->style('note', $class, $attr).(empty($attr['cut_hash']) ? ' {' : '') : '{'; |
| 296 | } elseif (Cursor::HASH_RESOURCE === $type) { |
| 297 | $prefix = $this->style('note', $class.' resource', $attr).($hasChild ? ' {' : ' '); |
| 298 | } else { |
| 299 | $prefix = $class && !(self::DUMP_LIGHT_ARRAY & $this->flags) ? $this->style('note', 'array:'.$class).' [' : '['; |
| 300 | } |
| 301 | |
| 302 | if (($cursor->softRefCount || 0 < $cursor->softRefHandle) && empty($attr['cut_hash'])) { |
| 303 | $prefix .= $this->style('ref', (Cursor::HASH_RESOURCE === $type ? '@' : '#').(0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->softRefTo), ['count' => $cursor->softRefCount]); |
| 304 | } elseif ($cursor->hardRefTo && !$cursor->refIndex && $class) { |
| 305 | $prefix .= $this->style('ref', '&'.$cursor->hardRefTo, ['count' => $cursor->hardRefCount]); |
| 306 | } elseif (!$hasChild && Cursor::HASH_RESOURCE === $type) { |
| 307 | $prefix = substr($prefix, 0, -1); |
| 308 | } |
| 309 | |
| 310 | $this->line .= $prefix; |
| 311 | |
| 312 | if ($hasChild) { |
| 313 | $this->dumpLine($cursor->depth); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * {@inheritdoc} |
| 319 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 320 | public function leaveHash(Cursor $cursor, int $type, string|int|null $class, bool $hasChild, int $cut) |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 321 | { |
| 322 | if (empty($cursor->attr['cut_hash'])) { |
| 323 | $this->dumpEllipsis($cursor, $hasChild, $cut); |
| 324 | $this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : '')); |
| 325 | } |
| 326 | |
| 327 | $this->endValue($cursor); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Dumps an ellipsis for cut children. |
| 332 | * |
| 333 | * @param bool $hasChild When the dump of the hash has child item |
| 334 | * @param int $cut The number of items the hash has been cut by |
| 335 | */ |
| 336 | protected function dumpEllipsis(Cursor $cursor, bool $hasChild, int $cut) |
| 337 | { |
| 338 | if ($cut) { |
| 339 | $this->line .= ' …'; |
| 340 | if (0 < $cut) { |
| 341 | $this->line .= $cut; |
| 342 | } |
| 343 | if ($hasChild) { |
| 344 | $this->dumpLine($cursor->depth + 1); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Dumps a key in a hash structure. |
| 351 | */ |
| 352 | protected function dumpKey(Cursor $cursor) |
| 353 | { |
| 354 | if (null !== $key = $cursor->hashKey) { |
| 355 | if ($cursor->hashKeyIsBinary) { |
| 356 | $key = $this->utf8Encode($key); |
| 357 | } |
| 358 | $attr = ['binary' => $cursor->hashKeyIsBinary]; |
| 359 | $bin = $cursor->hashKeyIsBinary ? 'b' : ''; |
| 360 | $style = 'key'; |
| 361 | switch ($cursor->hashType) { |
| 362 | default: |
| 363 | case Cursor::HASH_INDEXED: |
| 364 | if (self::DUMP_LIGHT_ARRAY & $this->flags) { |
| 365 | break; |
| 366 | } |
| 367 | $style = 'index'; |
| 368 | // no break |
| 369 | case Cursor::HASH_ASSOC: |
| 370 | if (\is_int($key)) { |
| 371 | $this->line .= $this->style($style, $key).' => '; |
| 372 | } else { |
| 373 | $this->line .= $bin.'"'.$this->style($style, $key).'" => '; |
| 374 | } |
| 375 | break; |
| 376 | |
| 377 | case Cursor::HASH_RESOURCE: |
| 378 | $key = "\0~\0".$key; |
| 379 | // no break |
| 380 | case Cursor::HASH_OBJECT: |
| 381 | if (!isset($key[0]) || "\0" !== $key[0]) { |
| 382 | $this->line .= '+'.$bin.$this->style('public', $key).': '; |
| 383 | } elseif (0 < strpos($key, "\0", 1)) { |
| 384 | $key = explode("\0", substr($key, 1), 2); |
| 385 | |
| 386 | switch ($key[0][0]) { |
| 387 | case '+': // User inserted keys |
| 388 | $attr['dynamic'] = true; |
| 389 | $this->line .= '+'.$bin.'"'.$this->style('public', $key[1], $attr).'": '; |
| 390 | break 2; |
| 391 | case '~': |
| 392 | $style = 'meta'; |
| 393 | if (isset($key[0][1])) { |
| 394 | parse_str(substr($key[0], 1), $attr); |
| 395 | $attr += ['binary' => $cursor->hashKeyIsBinary]; |
| 396 | } |
| 397 | break; |
| 398 | case '*': |
| 399 | $style = 'protected'; |
| 400 | $bin = '#'.$bin; |
| 401 | break; |
| 402 | default: |
| 403 | $attr['class'] = $key[0]; |
| 404 | $style = 'private'; |
| 405 | $bin = '-'.$bin; |
| 406 | break; |
| 407 | } |
| 408 | |
| 409 | if (isset($attr['collapse'])) { |
| 410 | if ($attr['collapse']) { |
| 411 | $this->collapseNextHash = true; |
| 412 | } else { |
| 413 | $this->expandNextHash = true; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | $this->line .= $bin.$this->style($style, $key[1], $attr).($attr['separator'] ?? ': '); |
| 418 | } else { |
| 419 | // This case should not happen |
| 420 | $this->line .= '-'.$bin.'"'.$this->style('private', $key, ['class' => '']).'": '; |
| 421 | } |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | if ($cursor->hardRefTo) { |
| 426 | $this->line .= $this->style('ref', '&'.($cursor->hardRefCount ? $cursor->hardRefTo : ''), ['count' => $cursor->hardRefCount]).' '; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Decorates a value with some style. |
| 433 | * |
| 434 | * @param string $style The type of style being applied |
| 435 | * @param string $value The value being styled |
| 436 | * @param array $attr Optional context information |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 437 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 438 | protected function style(string $style, string $value, array $attr = []): string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 439 | { |
| 440 | if (null === $this->colors) { |
| 441 | $this->colors = $this->supportsColors(); |
| 442 | } |
| 443 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 444 | $this->handlesHrefGracefully ??= 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR') |
| 445 | && (!getenv('KONSOLE_VERSION') || (int) getenv('KONSOLE_VERSION') > 201100); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 446 | |
| 447 | if (isset($attr['ellipsis'], $attr['ellipsis-type'])) { |
| 448 | $prefix = substr($value, 0, -$attr['ellipsis']); |
| 449 | if ('cli' === \PHP_SAPI && 'path' === $attr['ellipsis-type'] && isset($_SERVER[$pwd = '\\' === \DIRECTORY_SEPARATOR ? 'CD' : 'PWD']) && str_starts_with($prefix, $_SERVER[$pwd])) { |
| 450 | $prefix = '.'.substr($prefix, \strlen($_SERVER[$pwd])); |
| 451 | } |
| 452 | if (!empty($attr['ellipsis-tail'])) { |
| 453 | $prefix .= substr($value, -$attr['ellipsis'], $attr['ellipsis-tail']); |
| 454 | $value = substr($value, -$attr['ellipsis'] + $attr['ellipsis-tail']); |
| 455 | } else { |
| 456 | $value = substr($value, -$attr['ellipsis']); |
| 457 | } |
| 458 | |
| 459 | $value = $this->style('default', $prefix).$this->style($style, $value); |
| 460 | |
| 461 | goto href; |
| 462 | } |
| 463 | |
| 464 | $map = static::$controlCharsMap; |
| 465 | $startCchr = $this->colors ? "\033[m\033[{$this->styles['default']}m" : ''; |
| 466 | $endCchr = $this->colors ? "\033[m\033[{$this->styles[$style]}m" : ''; |
| 467 | $value = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $startCchr, $endCchr) { |
| 468 | $s = $startCchr; |
| 469 | $c = $c[$i = 0]; |
| 470 | do { |
| 471 | $s .= $map[$c[$i]] ?? sprintf('\x%02X', \ord($c[$i])); |
| 472 | } while (isset($c[++$i])); |
| 473 | |
| 474 | return $s.$endCchr; |
| 475 | }, $value, -1, $cchrCount); |
| 476 | |
| 477 | if ($this->colors) { |
| 478 | if ($cchrCount && "\033" === $value[0]) { |
| 479 | $value = substr($value, \strlen($startCchr)); |
| 480 | } else { |
| 481 | $value = "\033[{$this->styles[$style]}m".$value; |
| 482 | } |
| 483 | if ($cchrCount && str_ends_with($value, $endCchr)) { |
| 484 | $value = substr($value, 0, -\strlen($endCchr)); |
| 485 | } else { |
| 486 | $value .= "\033[{$this->styles['default']}m"; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | href: |
| 491 | if ($this->colors && $this->handlesHrefGracefully) { |
| 492 | if (isset($attr['file']) && $href = $this->getSourceLink($attr['file'], $attr['line'] ?? 0)) { |
| 493 | if ('note' === $style) { |
| 494 | $value .= "\033]8;;{$href}\033\\^\033]8;;\033\\"; |
| 495 | } else { |
| 496 | $attr['href'] = $href; |
| 497 | } |
| 498 | } |
| 499 | if (isset($attr['href'])) { |
| 500 | $value = "\033]8;;{$attr['href']}\033\\{$value}\033]8;;\033\\"; |
| 501 | } |
| 502 | } elseif ($attr['if_links'] ?? false) { |
| 503 | return ''; |
| 504 | } |
| 505 | |
| 506 | return $value; |
| 507 | } |
| 508 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 509 | protected function supportsColors(): bool |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 510 | { |
| 511 | if ($this->outputStream !== static::$defaultOutput) { |
| 512 | return $this->hasColorSupport($this->outputStream); |
| 513 | } |
| 514 | if (null !== static::$defaultColors) { |
| 515 | return static::$defaultColors; |
| 516 | } |
| 517 | if (isset($_SERVER['argv'][1])) { |
| 518 | $colors = $_SERVER['argv']; |
| 519 | $i = \count($colors); |
| 520 | while (--$i > 0) { |
| 521 | if (isset($colors[$i][5])) { |
| 522 | switch ($colors[$i]) { |
| 523 | case '--ansi': |
| 524 | case '--color': |
| 525 | case '--color=yes': |
| 526 | case '--color=force': |
| 527 | case '--color=always': |
| 528 | case '--colors=always': |
| 529 | return static::$defaultColors = true; |
| 530 | |
| 531 | case '--no-ansi': |
| 532 | case '--color=no': |
| 533 | case '--color=none': |
| 534 | case '--color=never': |
| 535 | case '--colors=never': |
| 536 | return static::$defaultColors = false; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | $h = stream_get_meta_data($this->outputStream) + ['wrapper_type' => null]; |
| 543 | $h = 'Output' === $h['stream_type'] && 'PHP' === $h['wrapper_type'] ? fopen('php://stdout', 'w') : $this->outputStream; |
| 544 | |
| 545 | return static::$defaultColors = $this->hasColorSupport($h); |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * {@inheritdoc} |
| 550 | */ |
| 551 | protected function dumpLine(int $depth, bool $endOfValue = false) |
| 552 | { |
| 553 | if ($this->colors) { |
| 554 | $this->line = sprintf("\033[%sm%s\033[m", $this->styles['default'], $this->line); |
| 555 | } |
| 556 | parent::dumpLine($depth); |
| 557 | } |
| 558 | |
| 559 | protected function endValue(Cursor $cursor) |
| 560 | { |
| 561 | if (-1 === $cursor->hashType) { |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | if (Stub::ARRAY_INDEXED === $cursor->hashType || Stub::ARRAY_ASSOC === $cursor->hashType) { |
| 566 | if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth) { |
| 567 | $this->line .= ','; |
| 568 | } elseif (self::DUMP_COMMA_SEPARATOR & $this->flags && 1 < $cursor->hashLength - $cursor->hashIndex) { |
| 569 | $this->line .= ','; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | $this->dumpLine($cursor->depth, true); |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * Returns true if the stream supports colorization. |
| 578 | * |
| 579 | * Reference: Composer\XdebugHandler\Process::supportsColor |
| 580 | * https://github.com/composer/xdebug-handler |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 581 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 582 | private function hasColorSupport(mixed $stream): bool |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 583 | { |
| 584 | if (!\is_resource($stream) || 'stream' !== get_resource_type($stream)) { |
| 585 | return false; |
| 586 | } |
| 587 | |
| 588 | // Follow https://no-color.org/ |
| 589 | if (isset($_SERVER['NO_COLOR']) || false !== getenv('NO_COLOR')) { |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | if ('Hyper' === getenv('TERM_PROGRAM')) { |
| 594 | return true; |
| 595 | } |
| 596 | |
| 597 | if (\DIRECTORY_SEPARATOR === '\\') { |
| 598 | return (\function_exists('sapi_windows_vt100_support') |
| 599 | && @sapi_windows_vt100_support($stream)) |
| 600 | || false !== getenv('ANSICON') |
| 601 | || 'ON' === getenv('ConEmuANSI') |
| 602 | || 'xterm' === getenv('TERM'); |
| 603 | } |
| 604 | |
| 605 | return stream_isatty($stream); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Returns true if the Windows terminal supports true color. |
| 610 | * |
| 611 | * Note that this does not check an output stream, but relies on environment |
| 612 | * variables from known implementations, or a PHP and Windows version that |
| 613 | * supports true color. |
| 614 | */ |
| 615 | private function isWindowsTrueColor(): bool |
| 616 | { |
| 617 | $result = 183 <= getenv('ANSICON_VER') |
| 618 | || 'ON' === getenv('ConEmuANSI') |
| 619 | || 'xterm' === getenv('TERM') |
| 620 | || 'Hyper' === getenv('TERM_PROGRAM'); |
| 621 | |
| 622 | if (!$result) { |
| 623 | $version = sprintf( |
| 624 | '%s.%s.%s', |
| 625 | PHP_WINDOWS_VERSION_MAJOR, |
| 626 | PHP_WINDOWS_VERSION_MINOR, |
| 627 | PHP_WINDOWS_VERSION_BUILD |
| 628 | ); |
| 629 | $result = $version >= '10.0.15063'; |
| 630 | } |
| 631 | |
| 632 | return $result; |
| 633 | } |
| 634 | |
| 635 | private function getSourceLink(string $file, int $line) |
| 636 | { |
| 637 | if ($fmt = $this->displayOptions['fileLinkFormat']) { |
| 638 | return \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $line]) : ($fmt->format($file, $line) ?: 'file://'.$file.'#L'.$line); |
| 639 | } |
| 640 | |
| 641 | return false; |
| 642 | } |
| 643 | } |