| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\VarDumper;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
15 |
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
16 |
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
|
|
|
17 |
use Symfony\Component\VarDumper\Caster\ReflectionCaster;
|
|
|
18 |
use Symfony\Component\VarDumper\Cloner\VarCloner;
|
|
|
19 |
use Symfony\Component\VarDumper\Dumper\CliDumper;
|
|
|
20 |
use Symfony\Component\VarDumper\Dumper\ContextProvider\CliContextProvider;
|
|
|
21 |
use Symfony\Component\VarDumper\Dumper\ContextProvider\RequestContextProvider;
|
|
|
22 |
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
|
|
|
23 |
use Symfony\Component\VarDumper\Dumper\ContextualizedDumper;
|
|
|
24 |
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
|
|
|
25 |
use Symfony\Component\VarDumper\Dumper\ServerDumper;
|
|
|
26 |
|
|
|
27 |
// Load the global dump() function
|
|
|
28 |
require_once __DIR__.'/Resources/functions/dump.php';
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
32 |
*/
|
|
|
33 |
class VarDumper
|
|
|
34 |
{
|
|
|
35 |
/**
|
|
|
36 |
* @var callable|null
|
|
|
37 |
*/
|
|
|
38 |
private static $handler;
|
|
|
39 |
|
|
|
40 |
public static function dump(mixed $var)
|
|
|
41 |
{
|
|
|
42 |
if (null === self::$handler) {
|
|
|
43 |
self::register();
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
return (self::$handler)($var);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public static function setHandler(callable $callable = null): ?callable
|
|
|
50 |
{
|
|
|
51 |
if (1 > \func_num_args()) {
|
|
|
52 |
trigger_deprecation('symfony/var-dumper', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
|
|
|
53 |
}
|
|
|
54 |
$prevHandler = self::$handler;
|
|
|
55 |
|
|
|
56 |
// Prevent replacing the handler with expected format as soon as the env var was set:
|
|
|
57 |
if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
|
|
|
58 |
return $prevHandler;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
self::$handler = $callable;
|
|
|
62 |
|
|
|
63 |
return $prevHandler;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
private static function register(): void
|
|
|
67 |
{
|
|
|
68 |
$cloner = new VarCloner();
|
|
|
69 |
$cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
|
|
|
70 |
|
|
|
71 |
$format = $_SERVER['VAR_DUMPER_FORMAT'] ?? null;
|
|
|
72 |
switch (true) {
|
|
|
73 |
case 'html' === $format:
|
|
|
74 |
$dumper = new HtmlDumper();
|
|
|
75 |
break;
|
|
|
76 |
case 'cli' === $format:
|
|
|
77 |
$dumper = new CliDumper();
|
|
|
78 |
break;
|
|
|
79 |
case 'server' === $format:
|
|
|
80 |
case $format && 'tcp' === parse_url($format, \PHP_URL_SCHEME):
|
|
|
81 |
$host = 'server' === $format ? $_SERVER['VAR_DUMPER_SERVER'] ?? '127.0.0.1:9912' : $format;
|
|
|
82 |
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliDumper() : new HtmlDumper();
|
|
|
83 |
$dumper = new ServerDumper($host, $dumper, self::getDefaultContextProviders());
|
|
|
84 |
break;
|
|
|
85 |
default:
|
|
|
86 |
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliDumper() : new HtmlDumper();
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
if (!$dumper instanceof ServerDumper) {
|
|
|
90 |
$dumper = new ContextualizedDumper($dumper, [new SourceContextProvider()]);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
self::$handler = function ($var) use ($cloner, $dumper) {
|
|
|
94 |
$dumper->dump($cloner->cloneVar($var));
|
|
|
95 |
};
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
private static function getDefaultContextProviders(): array
|
|
|
99 |
{
|
|
|
100 |
$contextProviders = [];
|
|
|
101 |
|
|
|
102 |
if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && class_exists(Request::class)) {
|
|
|
103 |
$requestStack = new RequestStack();
|
|
|
104 |
$requestStack->push(Request::createFromGlobals());
|
|
|
105 |
$contextProviders['request'] = new RequestContextProvider($requestStack);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
$fileLinkFormatter = class_exists(FileLinkFormatter::class) ? new FileLinkFormatter(null, $requestStack ?? null) : null;
|
|
|
109 |
|
|
|
110 |
return $contextProviders + [
|
|
|
111 |
'cli' => new CliContextProvider(),
|
|
|
112 |
'source' => new SourceContextProvider(null, null, $fileLinkFormatter),
|
|
|
113 |
];
|
|
|
114 |
}
|
|
|
115 |
}
|