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

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