vendor/symfony/var-dumper/Dumper/CliDumper.php line 64

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