CliDumper.php 23 KB

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