first commit

This commit is contained in:
2025-07-18 16:20:14 +07:00
commit 98af45c018
16382 changed files with 3148096 additions and 0 deletions

View File

@@ -0,0 +1,204 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\VarDumper\Dumper;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\DumperInterface;
/**
* Abstract mechanism for dumping a Data object.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
abstract class AbstractDumper implements DataDumperInterface, DumperInterface
{
public const DUMP_LIGHT_ARRAY = 1;
public const DUMP_STRING_LENGTH = 2;
public const DUMP_COMMA_SEPARATOR = 4;
public const DUMP_TRAILING_COMMA = 8;
/** @var callable|resource|string|null */
public static $defaultOutput = 'php://output';
protected $line = '';
/** @var callable|null */
protected $lineDumper;
/** @var resource|null */
protected $outputStream;
protected $decimalPoint = '.';
protected $indentPad = ' ';
protected $flags;
private string $charset = '';
/**
* @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutput
* @param string|null $charset The default character encoding to use for non-UTF8 strings
* @param int $flags A bit field of static::DUMP_* constants to fine tune dumps representation
*/
public function __construct($output = null, ?string $charset = null, int $flags = 0)
{
$this->flags = $flags;
$this->setCharset($charset ?: \ini_get('php.output_encoding') ?: \ini_get('default_charset') ?: 'UTF-8');
$this->setOutput($output ?: static::$defaultOutput);
if (!$output && \is_string(static::$defaultOutput)) {
static::$defaultOutput = $this->outputStream;
}
}
/**
* Sets the output destination of the dumps.
*
* @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path
*
* @return callable|resource|string|null The previous output destination
*/
public function setOutput($output)
{
$prev = $this->outputStream ?? $this->lineDumper;
if (\is_callable($output)) {
$this->outputStream = null;
$this->lineDumper = $output;
} else {
if (\is_string($output)) {
$output = fopen($output, 'w');
}
$this->outputStream = $output;
$this->lineDumper = $this->echoLine(...);
}
return $prev;
}
/**
* Sets the default character encoding to use for non-UTF8 strings.
*
* @return string The previous charset
*/
public function setCharset(string $charset): string
{
$prev = $this->charset;
$charset = strtoupper($charset);
$charset = null === $charset || 'UTF-8' === $charset || 'UTF8' === $charset ? 'CP1252' : $charset;
$this->charset = $charset;
return $prev;
}
/**
* Sets the indentation pad string.
*
* @param string $pad A string that will be prepended to dumped lines, repeated by nesting level
*
* @return string The previous indent pad
*/
public function setIndentPad(string $pad): string
{
$prev = $this->indentPad;
$this->indentPad = $pad;
return $prev;
}
/**
* Dumps a Data object.
*
* @param callable|resource|string|true|null $output A line dumper callable, an opened stream, an output path or true to return the dump
*
* @return string|null The dump as string when $output is true
*/
public function dump(Data $data, $output = null): ?string
{
if ($locale = $this->flags & (self::DUMP_COMMA_SEPARATOR | self::DUMP_TRAILING_COMMA) ? setlocale(\LC_NUMERIC, 0) : null) {
setlocale(\LC_NUMERIC, 'C');
}
if ($returnDump = true === $output) {
$output = fopen('php://memory', 'r+');
}
if ($output) {
$prevOutput = $this->setOutput($output);
}
try {
$data->dump($this);
$this->dumpLine(-1);
if ($returnDump) {
$result = stream_get_contents($output, -1, 0);
fclose($output);
return $result;
}
} finally {
if ($output) {
$this->setOutput($prevOutput);
}
if ($locale) {
setlocale(\LC_NUMERIC, $locale);
}
}
return null;
}
/**
* Dumps the current line.
*
* @param int $depth The recursive depth in the dumped structure for the line being dumped,
* or -1 to signal the end-of-dump to the line dumper callable
*
* @return void
*/
protected function dumpLine(int $depth)
{
($this->lineDumper)($this->line, $depth, $this->indentPad);
$this->line = '';
}
/**
* Generic line dumper callback.
*
* @return void
*/
protected function echoLine(string $line, int $depth, string $indentPad)
{
if (-1 !== $depth) {
fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n");
}
}
/**
* Converts a non-UTF-8 string to UTF-8.
*/
protected function utf8Encode(?string $s): ?string
{
if (null === $s || preg_match('//u', $s)) {
return $s;
}
if (!\function_exists('iconv')) {
throw new \RuntimeException('Unable to convert a non-UTF-8 string to UTF-8: required function iconv() does not exist. You should install ext-iconv or symfony/polyfill-iconv.');
}
if (false !== $c = @iconv($this->charset, 'UTF-8', $s)) {
return $c;
}
if ('CP1252' !== $this->charset && false !== $c = @iconv('CP1252', 'UTF-8', $s)) {
return $c;
}
return iconv('CP850', 'UTF-8', $s);
}
}

View File

@@ -0,0 +1,689 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\VarDumper\Dumper;
use Symfony\Component\ErrorHandler\ErrorRenderer\FileLinkFormatter;
use Symfony\Component\VarDumper\Cloner\Cursor;
use Symfony\Component\VarDumper\Cloner\Stub;
/**
* CliDumper dumps variables for command line output.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class CliDumper extends AbstractDumper
{
public static $defaultColors;
/** @var callable|resource|string|null */
public static $defaultOutput = 'php://stdout';
protected $colors;
protected $maxStringWidth = 0;
protected $styles = [
// See http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
'default' => '0;38;5;208',
'num' => '1;38;5;38',
'const' => '1;38;5;208',
'str' => '1;38;5;113',
'note' => '38;5;38',
'ref' => '38;5;247',
'public' => '',
'protected' => '',
'private' => '',
'meta' => '38;5;170',
'key' => '38;5;113',
'index' => '38;5;38',
];
protected static $controlCharsRx = '/[\x00-\x1F\x7F]+/';
protected static $controlCharsMap = [
"\t" => '\t',
"\n" => '\n',
"\v" => '\v',
"\f" => '\f',
"\r" => '\r',
"\033" => '\e',
];
protected static $unicodeCharsRx = "/[\u{00A0}\u{00AD}\u{034F}\u{061C}\u{115F}\u{1160}\u{17B4}\u{17B5}\u{180E}\u{2000}-\u{200F}\u{202F}\u{205F}\u{2060}-\u{2064}\u{206A}-\u{206F}\u{3000}\u{2800}\u{3164}\u{FEFF}\u{FFA0}\u{1D159}\u{1D173}-\u{1D17A}]/u";
protected $collapseNextHash = false;
protected $expandNextHash = false;
private array $displayOptions = [
'fileLinkFormat' => null,
];
private bool $handlesHrefGracefully;
public function __construct($output = null, ?string $charset = null, int $flags = 0)
{
parent::__construct($output, $charset, $flags);
if ('\\' === \DIRECTORY_SEPARATOR && !$this->isWindowsTrueColor()) {
// Use only the base 16 xterm colors when using ANSICON or standard Windows 10 CLI
$this->setStyles([
'default' => '31',
'num' => '1;34',
'const' => '1;31',
'str' => '1;32',
'note' => '34',
'ref' => '1;30',
'meta' => '35',
'key' => '32',
'index' => '34',
]);
}
$this->displayOptions['fileLinkFormat'] = class_exists(FileLinkFormatter::class) ? new FileLinkFormatter() : (\ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: 'file://%f#L%l');
}
/**
* Enables/disables colored output.
*
* @return void
*/
public function setColors(bool $colors)
{
$this->colors = $colors;
}
/**
* Sets the maximum number of characters per line for dumped strings.
*
* @return void
*/
public function setMaxStringWidth(int $maxStringWidth)
{
$this->maxStringWidth = $maxStringWidth;
}
/**
* Configures styles.
*
* @param array $styles A map of style names to style definitions
*
* @return void
*/
public function setStyles(array $styles)
{
$this->styles = $styles + $this->styles;
}
/**
* Configures display options.
*
* @param array $displayOptions A map of display options to customize the behavior
*
* @return void
*/
public function setDisplayOptions(array $displayOptions)
{
$this->displayOptions = $displayOptions + $this->displayOptions;
}
/**
* @return void
*/
public function dumpScalar(Cursor $cursor, string $type, string|int|float|bool|null $value)
{
$this->dumpKey($cursor);
$this->collapseNextHash = $this->expandNextHash = false;
$style = 'const';
$attr = $cursor->attr;
switch ($type) {
case 'default':
$style = 'default';
break;
case 'label':
$this->styles += ['label' => $this->styles['default']];
$style = 'label';
break;
case 'integer':
$style = 'num';
if (isset($this->styles['integer'])) {
$style = 'integer';
}
break;
case 'double':
$style = 'num';
if (isset($this->styles['float'])) {
$style = 'float';
}
$value = match (true) {
\INF === $value => 'INF',
-\INF === $value => '-INF',
is_nan($value) => 'NAN',
default => !str_contains($value = (string) $value, $this->decimalPoint) ? $value .= $this->decimalPoint.'0' : $value,
};
break;
case 'NULL':
$value = 'null';
break;
case 'boolean':
$value = $value ? 'true' : 'false';
break;
default:
$attr += ['value' => $this->utf8Encode($value)];
$value = $this->utf8Encode($type);
break;
}
$this->line .= $this->style($style, $value, $attr);
$this->endValue($cursor);
}
/**
* @return void
*/
public function dumpString(Cursor $cursor, string $str, bool $bin, int $cut)
{
$this->dumpKey($cursor);
$this->collapseNextHash = $this->expandNextHash = false;
$attr = $cursor->attr;
if ($bin) {
$str = $this->utf8Encode($str);
}
if ('' === $str) {
$this->line .= '""';
if ($cut) {
$this->line .= '…'.$cut;
}
$this->endValue($cursor);
} else {
$attr += [
'length' => 0 <= $cut ? mb_strlen($str, 'UTF-8') + $cut : 0,
'binary' => $bin,
];
$str = $bin && str_contains($str, "\0") ? [$str] : explode("\n", $str);
if (isset($str[1]) && !isset($str[2]) && !isset($str[1][0])) {
unset($str[1]);
$str[0] .= "\n";
}
$m = \count($str) - 1;
$i = $lineCut = 0;
if (self::DUMP_STRING_LENGTH & $this->flags) {
$this->line .= '('.$attr['length'].') ';
}
if ($bin) {
$this->line .= 'b';
}
if ($m) {
$this->line .= '"""';
$this->dumpLine($cursor->depth);
} else {
$this->line .= '"';
}
foreach ($str as $str) {
if ($i < $m) {
$str .= "\n";
}
if (0 < $this->maxStringWidth && $this->maxStringWidth < $len = mb_strlen($str, 'UTF-8')) {
$str = mb_substr($str, 0, $this->maxStringWidth, 'UTF-8');
$lineCut = $len - $this->maxStringWidth;
}
if ($m && 0 < $cursor->depth) {
$this->line .= $this->indentPad;
}
if ('' !== $str) {
$this->line .= $this->style('str', $str, $attr);
}
if ($i++ == $m) {
if ($m) {
if ('' !== $str) {
$this->dumpLine($cursor->depth);
if (0 < $cursor->depth) {
$this->line .= $this->indentPad;
}
}
$this->line .= '"""';
} else {
$this->line .= '"';
}
if ($cut < 0) {
$this->line .= '…';
$lineCut = 0;
} elseif ($cut) {
$lineCut += $cut;
}
}
if ($lineCut) {
$this->line .= '…'.$lineCut;
$lineCut = 0;
}
if ($i > $m) {
$this->endValue($cursor);
} else {
$this->dumpLine($cursor->depth);
}
}
}
}
/**
* @return void
*/
public function enterHash(Cursor $cursor, int $type, string|int|null $class, bool $hasChild)
{
$this->colors ??= $this->supportsColors();
$this->dumpKey($cursor);
$this->expandNextHash = false;
$attr = $cursor->attr;
if ($this->collapseNextHash) {
$cursor->skipChildren = true;
$this->collapseNextHash = $hasChild = false;
}
$class = $this->utf8Encode($class);
if (Cursor::HASH_OBJECT === $type) {
$prefix = $class && 'stdClass' !== $class ? $this->style('note', $class, $attr).(empty($attr['cut_hash']) ? ' {' : '') : '{';
} elseif (Cursor::HASH_RESOURCE === $type) {
$prefix = $this->style('note', $class.' resource', $attr).($hasChild ? ' {' : ' ');
} else {
$prefix = $class && !(self::DUMP_LIGHT_ARRAY & $this->flags) ? $this->style('note', 'array:'.$class).' [' : '[';
}
if (($cursor->softRefCount || 0 < $cursor->softRefHandle) && empty($attr['cut_hash'])) {
$prefix .= $this->style('ref', (Cursor::HASH_RESOURCE === $type ? '@' : '#').(0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->softRefTo), ['count' => $cursor->softRefCount]);
} elseif ($cursor->hardRefTo && !$cursor->refIndex && $class) {
$prefix .= $this->style('ref', '&'.$cursor->hardRefTo, ['count' => $cursor->hardRefCount]);
} elseif (!$hasChild && Cursor::HASH_RESOURCE === $type) {
$prefix = substr($prefix, 0, -1);
}
$this->line .= $prefix;
if ($hasChild) {
$this->dumpLine($cursor->depth);
}
}
/**
* @return void
*/
public function leaveHash(Cursor $cursor, int $type, string|int|null $class, bool $hasChild, int $cut)
{
if (empty($cursor->attr['cut_hash'])) {
$this->dumpEllipsis($cursor, $hasChild, $cut);
$this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : ''));
}
$this->endValue($cursor);
}
/**
* Dumps an ellipsis for cut children.
*
* @param bool $hasChild When the dump of the hash has child item
* @param int $cut The number of items the hash has been cut by
*
* @return void
*/
protected function dumpEllipsis(Cursor $cursor, bool $hasChild, int $cut)
{
if ($cut) {
$this->line .= ' …';
if (0 < $cut) {
$this->line .= $cut;
}
if ($hasChild) {
$this->dumpLine($cursor->depth + 1);
}
}
}
/**
* Dumps a key in a hash structure.
*
* @return void
*/
protected function dumpKey(Cursor $cursor)
{
if (null !== $key = $cursor->hashKey) {
if ($cursor->hashKeyIsBinary) {
$key = $this->utf8Encode($key);
}
$attr = ['binary' => $cursor->hashKeyIsBinary];
$bin = $cursor->hashKeyIsBinary ? 'b' : '';
$style = 'key';
switch ($cursor->hashType) {
default:
case Cursor::HASH_INDEXED:
if (self::DUMP_LIGHT_ARRAY & $this->flags) {
break;
}
$style = 'index';
// no break
case Cursor::HASH_ASSOC:
if (\is_int($key)) {
$this->line .= $this->style($style, $key).' => ';
} else {
$this->line .= $bin.'"'.$this->style($style, $key).'" => ';
}
break;
case Cursor::HASH_RESOURCE:
$key = "\0~\0".$key;
// no break
case Cursor::HASH_OBJECT:
if (!isset($key[0]) || "\0" !== $key[0]) {
$this->line .= '+'.$bin.$this->style('public', $key).': ';
} elseif (0 < strpos($key, "\0", 1)) {
$key = explode("\0", substr($key, 1), 2);
switch ($key[0][0]) {
case '+': // User inserted keys
$attr['dynamic'] = true;
$this->line .= '+'.$bin.'"'.$this->style('public', $key[1], $attr).'": ';
break 2;
case '~':
$style = 'meta';
if (isset($key[0][1])) {
parse_str(substr($key[0], 1), $attr);
$attr += ['binary' => $cursor->hashKeyIsBinary];
}
break;
case '*':
$style = 'protected';
$bin = '#'.$bin;
break;
default:
$attr['class'] = $key[0];
$style = 'private';
$bin = '-'.$bin;
break;
}
if (isset($attr['collapse'])) {
if ($attr['collapse']) {
$this->collapseNextHash = true;
} else {
$this->expandNextHash = true;
}
}
$this->line .= $bin.$this->style($style, $key[1], $attr).($attr['separator'] ?? ': ');
} else {
// This case should not happen
$this->line .= '-'.$bin.'"'.$this->style('private', $key, ['class' => '']).'": ';
}
break;
}
if ($cursor->hardRefTo) {
$this->line .= $this->style('ref', '&'.($cursor->hardRefCount ? $cursor->hardRefTo : ''), ['count' => $cursor->hardRefCount]).' ';
}
}
}
/**
* Decorates a value with some style.
*
* @param string $style The type of style being applied
* @param string $value The value being styled
* @param array $attr Optional context information
*/
protected function style(string $style, string $value, array $attr = []): string
{
$this->colors ??= $this->supportsColors();
$this->handlesHrefGracefully ??= 'JetBrains-JediTerm' !== getenv('TERMINAL_EMULATOR')
&& (!getenv('KONSOLE_VERSION') || (int) getenv('KONSOLE_VERSION') > 201100)
&& !isset($_SERVER['IDEA_INITIAL_DIRECTORY']);
if (isset($attr['ellipsis'], $attr['ellipsis-type'])) {
$prefix = substr($value, 0, -$attr['ellipsis']);
if ('cli' === \PHP_SAPI && 'path' === $attr['ellipsis-type'] && isset($_SERVER[$pwd = '\\' === \DIRECTORY_SEPARATOR ? 'CD' : 'PWD']) && str_starts_with($prefix, $_SERVER[$pwd])) {
$prefix = '.'.substr($prefix, \strlen($_SERVER[$pwd]));
}
if (!empty($attr['ellipsis-tail'])) {
$prefix .= substr($value, -$attr['ellipsis'], $attr['ellipsis-tail']);
$value = substr($value, -$attr['ellipsis'] + $attr['ellipsis-tail']);
} else {
$value = substr($value, -$attr['ellipsis']);
}
$value = $this->style('default', $prefix).$this->style($style, $value);
goto href;
}
$map = static::$controlCharsMap;
$startCchr = $this->colors ? "\033[m\033[{$this->styles['default']}m" : '';
$endCchr = $this->colors ? "\033[m\033[{$this->styles[$style]}m" : '';
$value = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $startCchr, $endCchr) {
$s = $startCchr;
$c = $c[$i = 0];
do {
$s .= $map[$c[$i]] ?? sprintf('\x%02X', \ord($c[$i]));
} while (isset($c[++$i]));
return $s.$endCchr;
}, $value, -1, $cchrCount);
if (!($attr['binary'] ?? false)) {
$value = preg_replace_callback(static::$unicodeCharsRx, function ($c) use (&$cchrCount, $startCchr, $endCchr) {
++$cchrCount;
return $startCchr.'\u{'.strtoupper(dechex(mb_ord($c[0]))).'}'.$endCchr;
}, $value);
}
if ($this->colors && '' !== $value) {
if ($cchrCount && "\033" === $value[0]) {
$value = substr($value, \strlen($startCchr));
} else {
$value = "\033[{$this->styles[$style]}m".$value;
}
if ($cchrCount && str_ends_with($value, $endCchr)) {
$value = substr($value, 0, -\strlen($endCchr));
} else {
$value .= "\033[{$this->styles['default']}m";
}
}
href:
if ($this->colors && $this->handlesHrefGracefully) {
if (isset($attr['file']) && $href = $this->getSourceLink($attr['file'], $attr['line'] ?? 0)) {
if ('note' === $style) {
$value .= "\033]8;;{$href}\033\\^\033]8;;\033\\";
} else {
$attr['href'] = $href;
}
}
if (isset($attr['href'])) {
if ('label' === $style) {
$value .= '^';
}
$value = "\033]8;;{$attr['href']}\033\\{$value}\033]8;;\033\\";
}
}
if ('label' === $style && '' !== $value) {
$value .= ' ';
}
return $value;
}
protected function supportsColors(): bool
{
if ($this->outputStream !== static::$defaultOutput) {
return $this->hasColorSupport($this->outputStream);
}
if (isset(static::$defaultColors)) {
return static::$defaultColors;
}
if (isset($_SERVER['argv'][1])) {
$colors = $_SERVER['argv'];
$i = \count($colors);
while (--$i > 0) {
if (isset($colors[$i][5])) {
switch ($colors[$i]) {
case '--ansi':
case '--color':
case '--color=yes':
case '--color=force':
case '--color=always':
case '--colors=always':
return static::$defaultColors = true;
case '--no-ansi':
case '--color=no':
case '--color=none':
case '--color=never':
case '--colors=never':
return static::$defaultColors = false;
}
}
}
}
$h = stream_get_meta_data($this->outputStream) + ['wrapper_type' => null];
$h = 'Output' === $h['stream_type'] && 'PHP' === $h['wrapper_type'] ? fopen('php://stdout', 'w') : $this->outputStream;
return static::$defaultColors = $this->hasColorSupport($h);
}
/**
* @return void
*/
protected function dumpLine(int $depth, bool $endOfValue = false)
{
if (null === $this->colors) {
$this->colors = $this->supportsColors();
}
if ($this->colors) {
$this->line = sprintf("\033[%sm%s\033[m", $this->styles['default'], $this->line);
}
parent::dumpLine($depth);
}
/**
* @return void
*/
protected function endValue(Cursor $cursor)
{
if (-1 === $cursor->hashType) {
return;
}
if (Stub::ARRAY_INDEXED === $cursor->hashType || Stub::ARRAY_ASSOC === $cursor->hashType) {
if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth) {
$this->line .= ',';
} elseif (self::DUMP_COMMA_SEPARATOR & $this->flags && 1 < $cursor->hashLength - $cursor->hashIndex) {
$this->line .= ',';
}
}
$this->dumpLine($cursor->depth, true);
}
/**
* Returns true if the stream supports colorization.
*
* Reference: Composer\XdebugHandler\Process::supportsColor
* https://github.com/composer/xdebug-handler
*/
private function hasColorSupport(mixed $stream): bool
{
if (!\is_resource($stream) || 'stream' !== get_resource_type($stream)) {
return false;
}
// Follow https://no-color.org/
if ('' !== (($_SERVER['NO_COLOR'] ?? getenv('NO_COLOR'))[0] ?? '')) {
return false;
}
// Detect msysgit/mingw and assume this is a tty because detection
// does not work correctly, see https://github.com/composer/composer/issues/9690
if (!@stream_isatty($stream) && !\in_array(strtoupper((string) getenv('MSYSTEM')), ['MINGW32', 'MINGW64'], true)) {
return false;
}
if ('\\' === \DIRECTORY_SEPARATOR && @sapi_windows_vt100_support($stream)) {
return true;
}
if ('Hyper' === getenv('TERM_PROGRAM')
|| false !== getenv('COLORTERM')
|| false !== getenv('ANSICON')
|| 'ON' === getenv('ConEmuANSI')
) {
return true;
}
if ('dumb' === $term = (string) getenv('TERM')) {
return false;
}
// See https://github.com/chalk/supports-color/blob/d4f413efaf8da045c5ab440ed418ef02dbb28bf1/index.js#L157
return preg_match('/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/', $term);
}
/**
* Returns true if the Windows terminal supports true color.
*
* Note that this does not check an output stream, but relies on environment
* variables from known implementations, or a PHP and Windows version that
* supports true color.
*/
private function isWindowsTrueColor(): bool
{
$result = 183 <= getenv('ANSICON_VER')
|| 'ON' === getenv('ConEmuANSI')
|| 'xterm' === getenv('TERM')
|| 'Hyper' === getenv('TERM_PROGRAM');
if (!$result) {
$version = sprintf(
'%s.%s.%s',
PHP_WINDOWS_VERSION_MAJOR,
PHP_WINDOWS_VERSION_MINOR,
PHP_WINDOWS_VERSION_BUILD
);
$result = $version >= '10.0.15063';
}
return $result;
}
private function getSourceLink(string $file, int $line): string|false
{
if ($fmt = $this->displayOptions['fileLinkFormat']) {
return \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $line]) : ($fmt->format($file, $line) ?: 'file://'.$file.'#L'.$line);
}
return false;
}
}

View File

@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\VarDumper\Dumper\ContextProvider;
/**
* Tries to provide context on CLI.
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
final class CliContextProvider implements ContextProviderInterface
{
public function getContext(): ?array
{
if ('cli' !== \PHP_SAPI) {
return null;
}
return [
'command_line' => $commandLine = implode(' ', $_SERVER['argv'] ?? []),
'identifier' => hash('crc32b', $commandLine.$_SERVER['REQUEST_TIME_FLOAT']),
];
}
}

View File

@@ -0,0 +1,22 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\VarDumper\Dumper\ContextProvider;
/**
* Interface to provide contextual data about dump data clones sent to a server.
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
interface ContextProviderInterface
{
public function getContext(): ?array;
}

View File

@@ -0,0 +1,51 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\VarDumper\Dumper\ContextProvider;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\VarDumper\Caster\ReflectionCaster;
use Symfony\Component\VarDumper\Cloner\VarCloner;
/**
* Tries to provide context from a request.
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
final class RequestContextProvider implements ContextProviderInterface
{
private RequestStack $requestStack;
private VarCloner $cloner;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
$this->cloner = new VarCloner();
$this->cloner->setMaxItems(0);
$this->cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
}
public function getContext(): ?array
{
if (null === $request = $this->requestStack->getCurrentRequest()) {
return null;
}
$controller = $request->attributes->get('_controller');
return [
'uri' => $request->getUri(),
'method' => $request->getMethod(),
'controller' => $controller ? $this->cloner->cloneVar($controller) : $controller,
'identifier' => spl_object_hash($request),
];
}
}

View File

@@ -0,0 +1,127 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\VarDumper\Dumper\ContextProvider;
use Symfony\Component\ErrorHandler\ErrorRenderer\FileLinkFormatter;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter as LegacyFileLinkFormatter;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\VarDumper;
use Twig\Template;
/**
* Tries to provide context from sources (class name, file, line, code excerpt, ...).
*
* @author Nicolas Grekas <p@tchwork.com>
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
final class SourceContextProvider implements ContextProviderInterface
{
private int $limit;
private ?string $charset;
private ?string $projectDir;
private FileLinkFormatter|LegacyFileLinkFormatter|null $fileLinkFormatter;
public function __construct(?string $charset = null, ?string $projectDir = null, FileLinkFormatter|LegacyFileLinkFormatter|null $fileLinkFormatter = null, int $limit = 9)
{
$this->charset = $charset;
$this->projectDir = $projectDir;
$this->fileLinkFormatter = $fileLinkFormatter;
$this->limit = $limit;
}
public function getContext(): ?array
{
$trace = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, $this->limit);
$file = $trace[1]['file'];
$line = $trace[1]['line'];
$name = '-' === $file || 'Standard input code' === $file ? 'Standard input code' : false;
$fileExcerpt = false;
for ($i = 2; $i < $this->limit; ++$i) {
if (isset($trace[$i]['class'], $trace[$i]['function'])
&& 'dump' === $trace[$i]['function']
&& VarDumper::class === $trace[$i]['class']
) {
$file = $trace[$i]['file'] ?? $file;
$line = $trace[$i]['line'] ?? $line;
while (++$i < $this->limit) {
if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && !str_starts_with($trace[$i]['function'], 'call_user_func')) {
$file = $trace[$i]['file'];
$line = $trace[$i]['line'];
break;
} elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof Template) {
$template = $trace[$i]['object'];
$name = $template->getTemplateName();
$src = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : false);
$info = $template->getDebugInfo();
if (isset($info[$trace[$i - 1]['line']])) {
$line = $info[$trace[$i - 1]['line']];
$file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : null;
if ($src) {
$src = explode("\n", $src);
$fileExcerpt = [];
for ($i = max($line - 3, 1), $max = min($line + 3, \count($src)); $i <= $max; ++$i) {
$fileExcerpt[] = '<li'.($i === $line ? ' class="selected"' : '').'><code>'.$this->htmlEncode($src[$i - 1]).'</code></li>';
}
$fileExcerpt = '<ol start="'.max($line - 3, 1).'">'.implode("\n", $fileExcerpt).'</ol>';
}
}
break;
}
}
break;
}
}
if (false === $name) {
$name = str_replace('\\', '/', $file);
$name = substr($name, strrpos($name, '/') + 1);
}
$context = ['name' => $name, 'file' => $file, 'line' => $line];
$context['file_excerpt'] = $fileExcerpt;
if (null !== $this->projectDir) {
$context['project_dir'] = $this->projectDir;
if (str_starts_with($file, $this->projectDir)) {
$context['file_relative'] = ltrim(substr($file, \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
}
}
if ($this->fileLinkFormatter && $fileLink = $this->fileLinkFormatter->format($context['file'], $context['line'])) {
$context['file_link'] = $fileLink;
}
return $context;
}
private function htmlEncode(string $s): string
{
$html = '';
$dumper = new HtmlDumper(function ($line) use (&$html) { $html .= $line; }, $this->charset);
$dumper->setDumpHeader('');
$dumper->setDumpBoundaries('', '');
$cloner = new VarCloner();
$dumper->dump($cloner->cloneVar($s));
return substr(strip_tags($html), 1, -1);
}
}

View File

@@ -0,0 +1,46 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\VarDumper\Dumper;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;
/**
* @author Kévin Thérage <therage.kevin@gmail.com>
*/
class ContextualizedDumper implements DataDumperInterface
{
private DataDumperInterface $wrappedDumper;
private array $contextProviders;
/**
* @param ContextProviderInterface[] $contextProviders
*/
public function __construct(DataDumperInterface $wrappedDumper, array $contextProviders)
{
$this->wrappedDumper = $wrappedDumper;
$this->contextProviders = $contextProviders;
}
/**
* @return string|null
*/
public function dump(Data $data)
{
$context = $data->getContext();
foreach ($this->contextProviders as $contextProvider) {
$context[$contextProvider::class] = $contextProvider->getContext();
}
return $this->wrappedDumper->dump($data->withContext($context));
}
}

View File

@@ -0,0 +1,27 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\VarDumper\Dumper;
use Symfony\Component\VarDumper\Cloner\Data;
/**
* DataDumperInterface for dumping Data objects.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
interface DataDumperInterface
{
/**
* @return string|null
*/
public function dump(Data $data);
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,55 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\VarDumper\Dumper;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;
use Symfony\Component\VarDumper\Server\Connection;
/**
* ServerDumper forwards serialized Data clones to a server.
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
class ServerDumper implements DataDumperInterface
{
private Connection $connection;
private ?DataDumperInterface $wrappedDumper;
/**
* @param string $host The server host
* @param DataDumperInterface|null $wrappedDumper A wrapped instance used whenever we failed contacting the server
* @param ContextProviderInterface[] $contextProviders Context providers indexed by context name
*/
public function __construct(string $host, ?DataDumperInterface $wrappedDumper = null, array $contextProviders = [])
{
$this->connection = new Connection($host, $contextProviders);
$this->wrappedDumper = $wrappedDumper;
}
public function getContextProviders(): array
{
return $this->connection->getContextProviders();
}
/**
* @return string|null
*/
public function dump(Data $data)
{
if (!$this->connection->write($data) && $this->wrappedDumper) {
return $this->wrappedDumper->dump($data);
}
return null;
}
}