CliDumper.php 23 KB

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