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 nullstring $charset nullint $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 $cursorstring $typestring|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 $cursorstring $strbool $binint $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.             $this->endValue($cursor);
  156.         } else {
  157.             $attr += [
  158.                 'length' => <= $cut mb_strlen($str'UTF-8') + $cut 0,
  159.                 'binary' => $bin,
  160.             ];
  161.             $str $bin && str_contains($str"\0") ? [$str] : explode("\n"$str);
  162.             if (isset($str[1]) && !isset($str[2]) && !isset($str[1][0])) {
  163.                 unset($str[1]);
  164.                 $str[0] .= "\n";
  165.             }
  166.             $m \count($str) - 1;
  167.             $i $lineCut 0;
  168.             if (self::DUMP_STRING_LENGTH $this->flags) {
  169.                 $this->line .= '('.$attr['length'].') ';
  170.             }
  171.             if ($bin) {
  172.                 $this->line .= 'b';
  173.             }
  174.             if ($m) {
  175.                 $this->line .= '"""';
  176.                 $this->dumpLine($cursor->depth);
  177.             } else {
  178.                 $this->line .= '"';
  179.             }
  180.             foreach ($str as $str) {
  181.                 if ($i $m) {
  182.                     $str .= "\n";
  183.                 }
  184.                 if ($this->maxStringWidth && $this->maxStringWidth $len mb_strlen($str'UTF-8')) {
  185.                     $str mb_substr($str0$this->maxStringWidth'UTF-8');
  186.                     $lineCut $len $this->maxStringWidth;
  187.                 }
  188.                 if ($m && $cursor->depth) {
  189.                     $this->line .= $this->indentPad;
  190.                 }
  191.                 if ('' !== $str) {
  192.                     $this->line .= $this->style('str'$str$attr);
  193.                 }
  194.                 if ($i++ == $m) {
  195.                     if ($m) {
  196.                         if ('' !== $str) {
  197.                             $this->dumpLine($cursor->depth);
  198.                             if ($cursor->depth) {
  199.                                 $this->line .= $this->indentPad;
  200.                             }
  201.                         }
  202.                         $this->line .= '"""';
  203.                     } else {
  204.                         $this->line .= '"';
  205.                     }
  206.                     if ($cut 0) {
  207.                         $this->line .= '…';
  208.                         $lineCut 0;
  209.                     } elseif ($cut) {
  210.                         $lineCut += $cut;
  211.                     }
  212.                 }
  213.                 if ($lineCut) {
  214.                     $this->line .= '…'.$lineCut;
  215.                     $lineCut 0;
  216.                 }
  217.                 if ($i $m) {
  218.                     $this->endValue($cursor);
  219.                 } else {
  220.                     $this->dumpLine($cursor->depth);
  221.                 }
  222.             }
  223.         }
  224.     }
  225.     public function enterHash(Cursor $cursorint $typestring|int|null $classbool $hasChild)
  226.     {
  227.         $this->colors ??= $this->supportsColors();
  228.         $this->dumpKey($cursor);
  229.         $attr $cursor->attr;
  230.         if ($this->collapseNextHash) {
  231.             $cursor->skipChildren true;
  232.             $this->collapseNextHash $hasChild false;
  233.         }
  234.         $class $this->utf8Encode($class);
  235.         if (Cursor::HASH_OBJECT === $type) {
  236.             $prefix $class && 'stdClass' !== $class $this->style('note'$class$attr).(empty($attr['cut_hash']) ? ' {' '') : '{';
  237.         } elseif (Cursor::HASH_RESOURCE === $type) {
  238.             $prefix $this->style('note'$class.' resource'$attr).($hasChild ' {' ' ');
  239.         } else {
  240.             $prefix $class && !(self::DUMP_LIGHT_ARRAY $this->flags) ? $this->style('note''array:'.$class).' [' '[';
  241.         }
  242.         if (($cursor->softRefCount || $cursor->softRefHandle) && empty($attr['cut_hash'])) {
  243.             $prefix .= $this->style('ref', (Cursor::HASH_RESOURCE === $type '@' '#').($cursor->softRefHandle $cursor->softRefHandle $cursor->softRefTo), ['count' => $cursor->softRefCount]);
  244.         } elseif ($cursor->hardRefTo && !$cursor->refIndex && $class) {
  245.             $prefix .= $this->style('ref''&'.$cursor->hardRefTo, ['count' => $cursor->hardRefCount]);
  246.         } elseif (!$hasChild && Cursor::HASH_RESOURCE === $type) {
  247.             $prefix substr($prefix0, -1);
  248.         }
  249.         $this->line .= $prefix;
  250.         if ($hasChild) {
  251.             $this->dumpLine($cursor->depth);
  252.         }
  253.     }
  254.     public function leaveHash(Cursor $cursorint $typestring|int|null $classbool $hasChildint $cut)
  255.     {
  256.         if (empty($cursor->attr['cut_hash'])) {
  257.             $this->dumpEllipsis($cursor$hasChild$cut);
  258.             $this->line .= Cursor::HASH_OBJECT === $type '}' : (Cursor::HASH_RESOURCE !== $type ']' : ($hasChild '}' ''));
  259.         }
  260.         $this->endValue($cursor);
  261.     }
  262.     /**
  263.      * Dumps an ellipsis for cut children.
  264.      *
  265.      * @param bool $hasChild When the dump of the hash has child item
  266.      * @param int  $cut      The number of items the hash has been cut by
  267.      */
  268.     protected function dumpEllipsis(Cursor $cursorbool $hasChildint $cut)
  269.     {
  270.         if ($cut) {
  271.             $this->line .= ' â€¦';
  272.             if ($cut) {
  273.                 $this->line .= $cut;
  274.             }
  275.             if ($hasChild) {
  276.                 $this->dumpLine($cursor->depth 1);
  277.             }
  278.         }
  279.     }
  280.     /**
  281.      * Dumps a key in a hash structure.
  282.      */
  283.     protected function dumpKey(Cursor $cursor)
  284.     {
  285.         if (null !== $key $cursor->hashKey) {
  286.             if ($cursor->hashKeyIsBinary) {
  287.                 $key $this->utf8Encode($key);
  288.             }
  289.             $attr = ['binary' => $cursor->hashKeyIsBinary];
  290.             $bin $cursor->hashKeyIsBinary 'b' '';
  291.             $style 'key';
  292.             switch ($cursor->hashType) {
  293.                 default:
  294.                 case Cursor::HASH_INDEXED:
  295.                     if (self::DUMP_LIGHT_ARRAY $this->flags) {
  296.                         break;
  297.                     }
  298.                     $style 'index';
  299.                     // no break
  300.                 case Cursor::HASH_ASSOC:
  301.                     if (\is_int($key)) {
  302.                         $this->line .= $this->style($style$key).' => ';
  303.                     } else {
  304.                         $this->line .= $bin.'"'.$this->style($style$key).'" => ';
  305.                     }
  306.                     break;
  307.                 case Cursor::HASH_RESOURCE:
  308.                     $key "\0~\0".$key;
  309.                     // no break
  310.                 case Cursor::HASH_OBJECT:
  311.                     if (!isset($key[0]) || "\0" !== $key[0]) {
  312.                         $this->line .= '+'.$bin.$this->style('public'$key).': ';
  313.                     } elseif (strpos($key"\0"1)) {
  314.                         $key explode("\0"substr($key1), 2);
  315.                         switch ($key[0][0]) {
  316.                             case '+'// User inserted keys
  317.                                 $attr['dynamic'] = true;
  318.                                 $this->line .= '+'.$bin.'"'.$this->style('public'$key[1], $attr).'": ';
  319.                                 break 2;
  320.                             case '~':
  321.                                 $style 'meta';
  322.                                 if (isset($key[0][1])) {
  323.                                     parse_str(substr($key[0], 1), $attr);
  324.                                     $attr += ['binary' => $cursor->hashKeyIsBinary];
  325.                                 }
  326.                                 break;
  327.                             case '*':
  328.                                 $style 'protected';
  329.                                 $bin '#'.$bin;
  330.                                 break;
  331.                             default:
  332.                                 $attr['class'] = $key[0];
  333.                                 $style 'private';
  334.                                 $bin '-'.$bin;
  335.                                 break;
  336.                         }
  337.                         if (isset($attr['collapse'])) {
  338.                             if ($attr['collapse']) {
  339.                                 $this->collapseNextHash true;
  340.                             } else {
  341.                                 $this->expandNextHash true;
  342.                             }
  343.                         }
  344.                         $this->line .= $bin.$this->style($style$key[1], $attr).($attr['separator'] ?? ': ');
  345.                     } else {
  346.                         // This case should not happen
  347.                         $this->line .= '-'.$bin.'"'.$this->style('private'$key, ['class' => '']).'": ';
  348.                     }
  349.                     break;
  350.             }
  351.             if ($cursor->hardRefTo) {
  352.                 $this->line .= $this->style('ref''&'.($cursor->hardRefCount $cursor->hardRefTo ''), ['count' => $cursor->hardRefCount]).' ';
  353.             }
  354.         }
  355.     }
  356.     /**
  357.      * Decorates a value with some style.
  358.      *
  359.      * @param string $style The type of style being applied
  360.      * @param string $value The value being styled
  361.      * @param array  $attr  Optional context information
  362.      */
  363.     protected function style(string $stylestring $value, array $attr = []): string
  364.     {
  365.         $this->colors ??= $this->supportsColors();
  366.         $this->handlesHrefGracefully ??= 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR')
  367.             && (!getenv('KONSOLE_VERSION') || (int) getenv('KONSOLE_VERSION') > 201100);
  368.         if (isset($attr['ellipsis'], $attr['ellipsis-type'])) {
  369.             $prefix substr($value0, -$attr['ellipsis']);
  370.             if ('cli' === \PHP_SAPI && 'path' === $attr['ellipsis-type'] && isset($_SERVER[$pwd '\\' === \DIRECTORY_SEPARATOR 'CD' 'PWD']) && str_starts_with($prefix$_SERVER[$pwd])) {
  371.                 $prefix '.'.substr($prefix\strlen($_SERVER[$pwd]));
  372.             }
  373.             if (!empty($attr['ellipsis-tail'])) {
  374.                 $prefix .= substr($value, -$attr['ellipsis'], $attr['ellipsis-tail']);
  375.                 $value substr($value, -$attr['ellipsis'] + $attr['ellipsis-tail']);
  376.             } else {
  377.                 $value substr($value, -$attr['ellipsis']);
  378.             }
  379.             $value $this->style('default'$prefix).$this->style($style$value);
  380.             goto href;
  381.         }
  382.         $map = static::$controlCharsMap;
  383.         $startCchr $this->colors "\033[m\033[{$this->styles['default']}m" '';
  384.         $endCchr $this->colors "\033[m\033[{$this->styles[$style]}m" '';
  385.         $value preg_replace_callback(static::$controlCharsRx, function ($c) use ($map$startCchr$endCchr) {
  386.             $s $startCchr;
  387.             $c $c[$i 0];
  388.             do {
  389.                 $s .= $map[$c[$i]] ?? sprintf('\x%02X'\ord($c[$i]));
  390.             } while (isset($c[++$i]));
  391.             return $s.$endCchr;
  392.         }, $value, -1$cchrCount);
  393.         if ($this->colors) {
  394.             if ($cchrCount && "\033" === $value[0]) {
  395.                 $value substr($value\strlen($startCchr));
  396.             } else {
  397.                 $value "\033[{$this->styles[$style]}m".$value;
  398.             }
  399.             if ($cchrCount && str_ends_with($value$endCchr)) {
  400.                 $value substr($value0, -\strlen($endCchr));
  401.             } else {
  402.                 $value .= "\033[{$this->styles['default']}m";
  403.             }
  404.         }
  405.         href:
  406.         if ($this->colors && $this->handlesHrefGracefully) {
  407.             if (isset($attr['file']) && $href $this->getSourceLink($attr['file'], $attr['line'] ?? 0)) {
  408.                 if ('note' === $style) {
  409.                     $value .= "\033]8;;{$href}\033\\^\033]8;;\033\\";
  410.                 } else {
  411.                     $attr['href'] = $href;
  412.                 }
  413.             }
  414.             if (isset($attr['href'])) {
  415.                 $value "\033]8;;{$attr['href']}\033\\{$value}\033]8;;\033\\";
  416.             }
  417.         } elseif ($attr['if_links'] ?? false) {
  418.             return '';
  419.         }
  420.         return $value;
  421.     }
  422.     protected function supportsColors(): bool
  423.     {
  424.         if ($this->outputStream !== static::$defaultOutput) {
  425.             return $this->hasColorSupport($this->outputStream);
  426.         }
  427.         if (null !== static::$defaultColors) {
  428.             return static::$defaultColors;
  429.         }
  430.         if (isset($_SERVER['argv'][1])) {
  431.             $colors $_SERVER['argv'];
  432.             $i \count($colors);
  433.             while (--$i 0) {
  434.                 if (isset($colors[$i][5])) {
  435.                     switch ($colors[$i]) {
  436.                         case '--ansi':
  437.                         case '--color':
  438.                         case '--color=yes':
  439.                         case '--color=force':
  440.                         case '--color=always':
  441.                         case '--colors=always':
  442.                             return static::$defaultColors true;
  443.                         case '--no-ansi':
  444.                         case '--color=no':
  445.                         case '--color=none':
  446.                         case '--color=never':
  447.                         case '--colors=never':
  448.                             return static::$defaultColors false;
  449.                     }
  450.                 }
  451.             }
  452.         }
  453.         $h stream_get_meta_data($this->outputStream) + ['wrapper_type' => null];
  454.         $h 'Output' === $h['stream_type'] && 'PHP' === $h['wrapper_type'] ? fopen('php://stdout''w') : $this->outputStream;
  455.         return static::$defaultColors $this->hasColorSupport($h);
  456.     }
  457.     protected function dumpLine(int $depthbool $endOfValue false)
  458.     {
  459.         if ($this->colors) {
  460.             $this->line sprintf("\033[%sm%s\033[m"$this->styles['default'], $this->line);
  461.         }
  462.         parent::dumpLine($depth);
  463.     }
  464.     protected function endValue(Cursor $cursor)
  465.     {
  466.         if (-=== $cursor->hashType) {
  467.             return;
  468.         }
  469.         if (Stub::ARRAY_INDEXED === $cursor->hashType || Stub::ARRAY_ASSOC === $cursor->hashType) {
  470.             if (self::DUMP_TRAILING_COMMA $this->flags && $cursor->depth) {
  471.                 $this->line .= ',';
  472.             } elseif (self::DUMP_COMMA_SEPARATOR $this->flags && $cursor->hashLength $cursor->hashIndex) {
  473.                 $this->line .= ',';
  474.             }
  475.         }
  476.         $this->dumpLine($cursor->depthtrue);
  477.     }
  478.     /**
  479.      * Returns true if the stream supports colorization.
  480.      *
  481.      * Reference: Composer\XdebugHandler\Process::supportsColor
  482.      * https://github.com/composer/xdebug-handler
  483.      */
  484.     private function hasColorSupport(mixed $stream): bool
  485.     {
  486.         if (!\is_resource($stream) || 'stream' !== get_resource_type($stream)) {
  487.             return false;
  488.         }
  489.         // Follow https://no-color.org/
  490.         if (isset($_SERVER['NO_COLOR']) || false !== getenv('NO_COLOR')) {
  491.             return false;
  492.         }
  493.         if ('Hyper' === getenv('TERM_PROGRAM')) {
  494.             return true;
  495.         }
  496.         if (\DIRECTORY_SEPARATOR === '\\') {
  497.             return (\function_exists('sapi_windows_vt100_support')
  498.                 && @sapi_windows_vt100_support($stream))
  499.                 || false !== getenv('ANSICON')
  500.                 || 'ON' === getenv('ConEmuANSI')
  501.                 || 'xterm' === getenv('TERM');
  502.         }
  503.         return stream_isatty($stream);
  504.     }
  505.     /**
  506.      * Returns true if the Windows terminal supports true color.
  507.      *
  508.      * Note that this does not check an output stream, but relies on environment
  509.      * variables from known implementations, or a PHP and Windows version that
  510.      * supports true color.
  511.      */
  512.     private function isWindowsTrueColor(): bool
  513.     {
  514.         $result 183 <= getenv('ANSICON_VER')
  515.             || 'ON' === getenv('ConEmuANSI')
  516.             || 'xterm' === getenv('TERM')
  517.             || 'Hyper' === getenv('TERM_PROGRAM');
  518.         if (!$result) {
  519.             $version sprintf(
  520.                 '%s.%s.%s',
  521.                 PHP_WINDOWS_VERSION_MAJOR,
  522.                 PHP_WINDOWS_VERSION_MINOR,
  523.                 PHP_WINDOWS_VERSION_BUILD
  524.             );
  525.             $result $version >= '10.0.15063';
  526.         }
  527.         return $result;
  528.     }
  529.     private function getSourceLink(string $fileint $line)
  530.     {
  531.         if ($fmt $this->displayOptions['fileLinkFormat']) {
  532.             return \is_string($fmt) ? strtr($fmt, ['%f' => $file'%l' => $line]) : ($fmt->format($file$line) ?: 'file://'.$file.'#L'.$line);
  533.         }
  534.         return false;
  535.     }
  536. }