Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\VarDumper\Dumper; Chris@0: Chris@0: use Symfony\Component\VarDumper\Cloner\Cursor; Chris@0: use Symfony\Component\VarDumper\Cloner\Stub; Chris@0: Chris@0: /** Chris@0: * CliDumper dumps variables for command line output. Chris@0: * Chris@0: * @author Nicolas Grekas Chris@0: */ Chris@0: class CliDumper extends AbstractDumper Chris@0: { Chris@0: public static $defaultColors; Chris@0: public static $defaultOutput = 'php://stdout'; Chris@0: Chris@0: protected $colors; Chris@0: protected $maxStringWidth = 0; Chris@17: protected $styles = [ Chris@0: // See http://en.wikipedia.org/wiki/ANSI_escape_code#graphics Chris@0: 'default' => '38;5;208', Chris@0: 'num' => '1;38;5;38', Chris@0: 'const' => '1;38;5;208', Chris@0: 'str' => '1;38;5;113', Chris@0: 'note' => '38;5;38', Chris@0: 'ref' => '38;5;247', Chris@0: 'public' => '', Chris@0: 'protected' => '', Chris@0: 'private' => '', Chris@0: 'meta' => '38;5;170', Chris@0: 'key' => '38;5;113', Chris@0: 'index' => '38;5;38', Chris@17: ]; Chris@0: Chris@0: protected static $controlCharsRx = '/[\x00-\x1F\x7F]+/'; Chris@17: protected static $controlCharsMap = [ Chris@0: "\t" => '\t', Chris@0: "\n" => '\n', Chris@0: "\v" => '\v', Chris@0: "\f" => '\f', Chris@0: "\r" => '\r', Chris@0: "\033" => '\e', Chris@17: ]; Chris@0: Chris@12: protected $collapseNextHash = false; Chris@12: protected $expandNextHash = false; Chris@12: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function __construct($output = null, $charset = null, $flags = 0) Chris@0: { Chris@0: parent::__construct($output, $charset, $flags); Chris@0: Chris@17: if ('\\' === \DIRECTORY_SEPARATOR && !$this->isWindowsTrueColor()) { Chris@0: // Use only the base 16 xterm colors when using ANSICON or standard Windows 10 CLI Chris@17: $this->setStyles([ Chris@0: 'default' => '31', Chris@0: 'num' => '1;34', Chris@0: 'const' => '1;31', Chris@0: 'str' => '1;32', Chris@0: 'note' => '34', Chris@0: 'ref' => '1;30', Chris@0: 'meta' => '35', Chris@0: 'key' => '32', Chris@0: 'index' => '34', Chris@17: ]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Enables/disables colored output. Chris@0: * Chris@0: * @param bool $colors Chris@0: */ Chris@0: public function setColors($colors) Chris@0: { Chris@0: $this->colors = (bool) $colors; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the maximum number of characters per line for dumped strings. Chris@0: * Chris@0: * @param int $maxStringWidth Chris@0: */ Chris@0: public function setMaxStringWidth($maxStringWidth) Chris@0: { Chris@0: $this->maxStringWidth = (int) $maxStringWidth; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Configures styles. Chris@0: * Chris@0: * @param array $styles A map of style names to style definitions Chris@0: */ Chris@0: public function setStyles(array $styles) Chris@0: { Chris@0: $this->styles = $styles + $this->styles; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function dumpScalar(Cursor $cursor, $type, $value) Chris@0: { Chris@0: $this->dumpKey($cursor); Chris@0: Chris@0: $style = 'const'; Chris@0: $attr = $cursor->attr; Chris@0: Chris@0: switch ($type) { Chris@0: case 'default': Chris@0: $style = 'default'; Chris@0: break; Chris@0: Chris@0: case 'integer': Chris@0: $style = 'num'; Chris@0: break; Chris@0: Chris@0: case 'double': Chris@0: $style = 'num'; Chris@0: Chris@0: switch (true) { Chris@0: case INF === $value: $value = 'INF'; break; Chris@0: case -INF === $value: $value = '-INF'; break; Chris@0: case is_nan($value): $value = 'NAN'; break; Chris@0: default: Chris@0: $value = (string) $value; Chris@0: if (false === strpos($value, $this->decimalPoint)) { Chris@0: $value .= $this->decimalPoint.'0'; Chris@0: } Chris@0: break; Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'NULL': Chris@0: $value = 'null'; Chris@0: break; Chris@0: Chris@0: case 'boolean': Chris@0: $value = $value ? 'true' : 'false'; Chris@0: break; Chris@0: Chris@0: default: Chris@17: $attr += ['value' => $this->utf8Encode($value)]; Chris@0: $value = $this->utf8Encode($type); Chris@0: break; Chris@0: } Chris@0: Chris@0: $this->line .= $this->style($style, $value, $attr); Chris@0: Chris@0: $this->endValue($cursor); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function dumpString(Cursor $cursor, $str, $bin, $cut) Chris@0: { Chris@0: $this->dumpKey($cursor); Chris@0: $attr = $cursor->attr; Chris@0: Chris@0: if ($bin) { Chris@0: $str = $this->utf8Encode($str); Chris@0: } Chris@0: if ('' === $str) { Chris@0: $this->line .= '""'; Chris@0: $this->endValue($cursor); Chris@0: } else { Chris@17: $attr += [ Chris@0: 'length' => 0 <= $cut ? mb_strlen($str, 'UTF-8') + $cut : 0, Chris@0: 'binary' => $bin, Chris@17: ]; Chris@0: $str = explode("\n", $str); Chris@0: if (isset($str[1]) && !isset($str[2]) && !isset($str[1][0])) { Chris@0: unset($str[1]); Chris@0: $str[0] .= "\n"; Chris@0: } Chris@17: $m = \count($str) - 1; Chris@0: $i = $lineCut = 0; Chris@0: Chris@0: if (self::DUMP_STRING_LENGTH & $this->flags) { Chris@0: $this->line .= '('.$attr['length'].') '; Chris@0: } Chris@0: if ($bin) { Chris@0: $this->line .= 'b'; Chris@0: } Chris@0: Chris@0: if ($m) { Chris@0: $this->line .= '"""'; Chris@0: $this->dumpLine($cursor->depth); Chris@0: } else { Chris@0: $this->line .= '"'; Chris@0: } Chris@0: Chris@0: foreach ($str as $str) { Chris@0: if ($i < $m) { Chris@0: $str .= "\n"; Chris@0: } Chris@0: if (0 < $this->maxStringWidth && $this->maxStringWidth < $len = mb_strlen($str, 'UTF-8')) { Chris@0: $str = mb_substr($str, 0, $this->maxStringWidth, 'UTF-8'); Chris@0: $lineCut = $len - $this->maxStringWidth; Chris@0: } Chris@0: if ($m && 0 < $cursor->depth) { Chris@0: $this->line .= $this->indentPad; Chris@0: } Chris@0: if ('' !== $str) { Chris@0: $this->line .= $this->style('str', $str, $attr); Chris@0: } Chris@0: if ($i++ == $m) { Chris@0: if ($m) { Chris@0: if ('' !== $str) { Chris@0: $this->dumpLine($cursor->depth); Chris@0: if (0 < $cursor->depth) { Chris@0: $this->line .= $this->indentPad; Chris@0: } Chris@0: } Chris@0: $this->line .= '"""'; Chris@0: } else { Chris@0: $this->line .= '"'; Chris@0: } Chris@0: if ($cut < 0) { Chris@0: $this->line .= '…'; Chris@0: $lineCut = 0; Chris@0: } elseif ($cut) { Chris@0: $lineCut += $cut; Chris@0: } Chris@0: } Chris@0: if ($lineCut) { Chris@0: $this->line .= '…'.$lineCut; Chris@0: $lineCut = 0; Chris@0: } Chris@0: Chris@0: if ($i > $m) { Chris@0: $this->endValue($cursor); Chris@0: } else { Chris@0: $this->dumpLine($cursor->depth); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function enterHash(Cursor $cursor, $type, $class, $hasChild) Chris@0: { Chris@0: $this->dumpKey($cursor); Chris@0: Chris@12: if ($this->collapseNextHash) { Chris@12: $cursor->skipChildren = true; Chris@12: $this->collapseNextHash = $hasChild = false; Chris@12: } Chris@12: Chris@0: $class = $this->utf8Encode($class); Chris@0: if (Cursor::HASH_OBJECT === $type) { Chris@0: $prefix = $class && 'stdClass' !== $class ? $this->style('note', $class).' {' : '{'; Chris@0: } elseif (Cursor::HASH_RESOURCE === $type) { Chris@0: $prefix = $this->style('note', $class.' resource').($hasChild ? ' {' : ' '); Chris@0: } else { Chris@0: $prefix = $class && !(self::DUMP_LIGHT_ARRAY & $this->flags) ? $this->style('note', 'array:'.$class).' [' : '['; Chris@0: } Chris@0: Chris@0: if ($cursor->softRefCount || 0 < $cursor->softRefHandle) { Chris@17: $prefix .= $this->style('ref', (Cursor::HASH_RESOURCE === $type ? '@' : '#').(0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->softRefTo), ['count' => $cursor->softRefCount]); Chris@0: } elseif ($cursor->hardRefTo && !$cursor->refIndex && $class) { Chris@17: $prefix .= $this->style('ref', '&'.$cursor->hardRefTo, ['count' => $cursor->hardRefCount]); Chris@0: } elseif (!$hasChild && Cursor::HASH_RESOURCE === $type) { Chris@0: $prefix = substr($prefix, 0, -1); Chris@0: } Chris@0: Chris@0: $this->line .= $prefix; Chris@0: Chris@0: if ($hasChild) { Chris@0: $this->dumpLine($cursor->depth); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function leaveHash(Cursor $cursor, $type, $class, $hasChild, $cut) Chris@0: { Chris@0: $this->dumpEllipsis($cursor, $hasChild, $cut); Chris@0: $this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : '')); Chris@0: $this->endValue($cursor); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Dumps an ellipsis for cut children. Chris@0: * Chris@0: * @param Cursor $cursor The Cursor position in the dump Chris@0: * @param bool $hasChild When the dump of the hash has child item Chris@0: * @param int $cut The number of items the hash has been cut by Chris@0: */ Chris@0: protected function dumpEllipsis(Cursor $cursor, $hasChild, $cut) Chris@0: { Chris@0: if ($cut) { Chris@0: $this->line .= ' …'; Chris@0: if (0 < $cut) { Chris@0: $this->line .= $cut; Chris@0: } Chris@0: if ($hasChild) { Chris@0: $this->dumpLine($cursor->depth + 1); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Dumps a key in a hash structure. Chris@0: * Chris@0: * @param Cursor $cursor The Cursor position in the dump Chris@0: */ Chris@0: protected function dumpKey(Cursor $cursor) Chris@0: { Chris@0: if (null !== $key = $cursor->hashKey) { Chris@0: if ($cursor->hashKeyIsBinary) { Chris@0: $key = $this->utf8Encode($key); Chris@0: } Chris@17: $attr = ['binary' => $cursor->hashKeyIsBinary]; Chris@0: $bin = $cursor->hashKeyIsBinary ? 'b' : ''; Chris@0: $style = 'key'; Chris@0: switch ($cursor->hashType) { Chris@0: default: Chris@0: case Cursor::HASH_INDEXED: Chris@0: if (self::DUMP_LIGHT_ARRAY & $this->flags) { Chris@0: break; Chris@0: } Chris@0: $style = 'index'; Chris@0: // no break Chris@0: case Cursor::HASH_ASSOC: Chris@17: if (\is_int($key)) { Chris@0: $this->line .= $this->style($style, $key).' => '; Chris@0: } else { Chris@0: $this->line .= $bin.'"'.$this->style($style, $key).'" => '; Chris@0: } Chris@0: break; Chris@0: Chris@0: case Cursor::HASH_RESOURCE: Chris@0: $key = "\0~\0".$key; Chris@0: // no break Chris@0: case Cursor::HASH_OBJECT: Chris@0: if (!isset($key[0]) || "\0" !== $key[0]) { Chris@0: $this->line .= '+'.$bin.$this->style('public', $key).': '; Chris@0: } elseif (0 < strpos($key, "\0", 1)) { Chris@0: $key = explode("\0", substr($key, 1), 2); Chris@0: Chris@0: switch ($key[0][0]) { Chris@0: case '+': // User inserted keys Chris@0: $attr['dynamic'] = true; Chris@0: $this->line .= '+'.$bin.'"'.$this->style('public', $key[1], $attr).'": '; Chris@0: break 2; Chris@0: case '~': Chris@0: $style = 'meta'; Chris@0: if (isset($key[0][1])) { Chris@0: parse_str(substr($key[0], 1), $attr); Chris@17: $attr += ['binary' => $cursor->hashKeyIsBinary]; Chris@0: } Chris@0: break; Chris@0: case '*': Chris@0: $style = 'protected'; Chris@0: $bin = '#'.$bin; Chris@0: break; Chris@0: default: Chris@0: $attr['class'] = $key[0]; Chris@0: $style = 'private'; Chris@0: $bin = '-'.$bin; Chris@0: break; Chris@0: } Chris@0: Chris@12: if (isset($attr['collapse'])) { Chris@12: if ($attr['collapse']) { Chris@12: $this->collapseNextHash = true; Chris@12: } else { Chris@12: $this->expandNextHash = true; Chris@12: } Chris@12: } Chris@12: Chris@12: $this->line .= $bin.$this->style($style, $key[1], $attr).(isset($attr['separator']) ? $attr['separator'] : ': '); Chris@0: } else { Chris@0: // This case should not happen Chris@17: $this->line .= '-'.$bin.'"'.$this->style('private', $key, ['class' => '']).'": '; Chris@0: } Chris@0: break; Chris@0: } Chris@0: Chris@0: if ($cursor->hardRefTo) { Chris@17: $this->line .= $this->style('ref', '&'.($cursor->hardRefCount ? $cursor->hardRefTo : ''), ['count' => $cursor->hardRefCount]).' '; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Decorates a value with some style. Chris@0: * Chris@0: * @param string $style The type of style being applied Chris@0: * @param string $value The value being styled Chris@0: * @param array $attr Optional context information Chris@0: * Chris@0: * @return string The value with style decoration Chris@0: */ Chris@17: protected function style($style, $value, $attr = []) Chris@0: { Chris@0: if (null === $this->colors) { Chris@0: $this->colors = $this->supportsColors(); Chris@0: } Chris@0: Chris@12: if (isset($attr['ellipsis'], $attr['ellipsis-type'])) { Chris@12: $prefix = substr($value, 0, -$attr['ellipsis']); Chris@17: if ('cli' === \PHP_SAPI && 'path' === $attr['ellipsis-type'] && isset($_SERVER[$pwd = '\\' === \DIRECTORY_SEPARATOR ? 'CD' : 'PWD']) && 0 === strpos($prefix, $_SERVER[$pwd])) { Chris@17: $prefix = '.'.substr($prefix, \strlen($_SERVER[$pwd])); Chris@12: } Chris@12: if (!empty($attr['ellipsis-tail'])) { Chris@12: $prefix .= substr($value, -$attr['ellipsis'], $attr['ellipsis-tail']); Chris@12: $value = substr($value, -$attr['ellipsis'] + $attr['ellipsis-tail']); Chris@12: } else { Chris@12: $value = substr($value, -$attr['ellipsis']); Chris@12: } Chris@12: Chris@12: return $this->style('default', $prefix).$this->style($style, $value); Chris@12: } Chris@12: Chris@0: $style = $this->styles[$style]; Chris@0: Chris@0: $map = static::$controlCharsMap; Chris@0: $startCchr = $this->colors ? "\033[m\033[{$this->styles['default']}m" : ''; Chris@0: $endCchr = $this->colors ? "\033[m\033[{$style}m" : ''; Chris@0: $value = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $startCchr, $endCchr) { Chris@0: $s = $startCchr; Chris@0: $c = $c[$i = 0]; Chris@0: do { Chris@17: $s .= isset($map[$c[$i]]) ? $map[$c[$i]] : sprintf('\x%02X', \ord($c[$i])); Chris@0: } while (isset($c[++$i])); Chris@0: Chris@0: return $s.$endCchr; Chris@0: }, $value, -1, $cchrCount); Chris@0: Chris@0: if ($this->colors) { Chris@0: if ($cchrCount && "\033" === $value[0]) { Chris@17: $value = substr($value, \strlen($startCchr)); Chris@0: } else { Chris@0: $value = "\033[{$style}m".$value; Chris@0: } Chris@17: if ($cchrCount && $endCchr === substr($value, -\strlen($endCchr))) { Chris@17: $value = substr($value, 0, -\strlen($endCchr)); Chris@0: } else { Chris@0: $value .= "\033[{$this->styles['default']}m"; Chris@0: } Chris@0: } Chris@0: Chris@0: return $value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return bool Tells if the current output stream supports ANSI colors or not Chris@0: */ Chris@0: protected function supportsColors() Chris@0: { Chris@0: if ($this->outputStream !== static::$defaultOutput) { Chris@16: return $this->hasColorSupport($this->outputStream); Chris@0: } Chris@0: if (null !== static::$defaultColors) { Chris@0: return static::$defaultColors; Chris@0: } Chris@0: if (isset($_SERVER['argv'][1])) { Chris@0: $colors = $_SERVER['argv']; Chris@17: $i = \count($colors); Chris@0: while (--$i > 0) { Chris@0: if (isset($colors[$i][5])) { Chris@0: switch ($colors[$i]) { Chris@0: case '--ansi': Chris@0: case '--color': Chris@0: case '--color=yes': Chris@0: case '--color=force': Chris@0: case '--color=always': Chris@0: return static::$defaultColors = true; Chris@0: Chris@0: case '--no-ansi': Chris@0: case '--color=no': Chris@0: case '--color=none': Chris@0: case '--color=never': Chris@0: return static::$defaultColors = false; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@17: $h = stream_get_meta_data($this->outputStream) + ['wrapper_type' => null]; Chris@16: $h = 'Output' === $h['stream_type'] && 'PHP' === $h['wrapper_type'] ? fopen('php://stdout', 'wb') : $this->outputStream; Chris@0: Chris@16: return static::$defaultColors = $this->hasColorSupport($h); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function dumpLine($depth, $endOfValue = false) Chris@0: { Chris@0: if ($this->colors) { Chris@0: $this->line = sprintf("\033[%sm%s\033[m", $this->styles['default'], $this->line); Chris@0: } Chris@0: parent::dumpLine($depth); Chris@0: } Chris@0: Chris@0: protected function endValue(Cursor $cursor) Chris@0: { Chris@0: if (Stub::ARRAY_INDEXED === $cursor->hashType || Stub::ARRAY_ASSOC === $cursor->hashType) { Chris@0: if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth) { Chris@0: $this->line .= ','; Chris@0: } elseif (self::DUMP_COMMA_SEPARATOR & $this->flags && 1 < $cursor->hashLength - $cursor->hashIndex) { Chris@0: $this->line .= ','; Chris@0: } Chris@0: } Chris@0: Chris@0: $this->dumpLine($cursor->depth, true); Chris@0: } Chris@16: Chris@16: /** Chris@16: * Returns true if the stream supports colorization. Chris@16: * Chris@16: * Reference: Composer\XdebugHandler\Process::supportsColor Chris@16: * https://github.com/composer/xdebug-handler Chris@16: * Chris@16: * @param mixed $stream A CLI output stream Chris@16: * Chris@16: * @return bool Chris@16: */ Chris@16: private function hasColorSupport($stream) Chris@16: { Chris@17: if (!\is_resource($stream) || 'stream' !== get_resource_type($stream)) { Chris@16: return false; Chris@16: } Chris@16: Chris@17: if ('Hyper' === getenv('TERM_PROGRAM')) { Chris@17: return true; Chris@17: } Chris@17: Chris@17: if (\DIRECTORY_SEPARATOR === '\\') { Chris@17: return (\function_exists('sapi_windows_vt100_support') Chris@16: && @sapi_windows_vt100_support($stream)) Chris@16: || false !== getenv('ANSICON') Chris@16: || 'ON' === getenv('ConEmuANSI') Chris@16: || 'xterm' === getenv('TERM'); Chris@16: } Chris@16: Chris@17: if (\function_exists('stream_isatty')) { Chris@16: return @stream_isatty($stream); Chris@16: } Chris@16: Chris@17: if (\function_exists('posix_isatty')) { Chris@16: return @posix_isatty($stream); Chris@16: } Chris@16: Chris@16: $stat = @fstat($stream); Chris@16: // Check if formatted mode is S_IFCHR Chris@16: return $stat ? 0020000 === ($stat['mode'] & 0170000) : false; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true if the Windows terminal supports true color. Chris@16: * Chris@16: * Note that this does not check an output stream, but relies on environment Chris@16: * variables from known implementations, or a PHP and Windows version that Chris@16: * supports true color. Chris@16: * Chris@16: * @return bool Chris@16: */ Chris@16: private function isWindowsTrueColor() Chris@16: { Chris@16: $result = 183 <= getenv('ANSICON_VER') Chris@16: || 'ON' === getenv('ConEmuANSI') Chris@17: || 'xterm' === getenv('TERM') Chris@17: || 'Hyper' === getenv('TERM_PROGRAM'); Chris@16: Chris@17: if (!$result && \PHP_VERSION_ID >= 70200) { Chris@16: $version = sprintf( Chris@16: '%s.%s.%s', Chris@16: PHP_WINDOWS_VERSION_MAJOR, Chris@16: PHP_WINDOWS_VERSION_MINOR, Chris@16: PHP_WINDOWS_VERSION_BUILD Chris@16: ); Chris@16: $result = $version >= '10.0.15063'; Chris@16: } Chris@16: Chris@16: return $result; Chris@16: } Chris@0: }