You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.8 KiB

2 years ago
  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;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
  14. use Symfony\Component\VarDumper\Caster\ReflectionCaster;
  15. use Symfony\Component\VarDumper\Cloner\VarCloner;
  16. use Symfony\Component\VarDumper\Dumper\CliDumper;
  17. use Symfony\Component\VarDumper\Dumper\ContextProvider\CliContextProvider;
  18. use Symfony\Component\VarDumper\Dumper\ContextProvider\RequestContextProvider;
  19. use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
  20. use Symfony\Component\VarDumper\Dumper\ContextualizedDumper;
  21. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  22. use Symfony\Component\VarDumper\Dumper\ServerDumper;
  23. // Load the global dump() function
  24. require_once __DIR__.'/Resources/functions/dump.php';
  25. /**
  26. * @author Nicolas Grekas <p@tchwork.com>
  27. */
  28. class VarDumper
  29. {
  30. /**
  31. * @var callable|null
  32. */
  33. private static $handler;
  34. public static function dump($var)
  35. {
  36. if (null === self::$handler) {
  37. self::register();
  38. }
  39. return (self::$handler)($var);
  40. }
  41. /**
  42. * @return callable|null
  43. */
  44. public static function setHandler(callable $callable = null)
  45. {
  46. $prevHandler = self::$handler;
  47. // Prevent replacing the handler with expected format as soon as the env var was set:
  48. if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
  49. return $prevHandler;
  50. }
  51. self::$handler = $callable;
  52. return $prevHandler;
  53. }
  54. private static function register(): void
  55. {
  56. $cloner = new VarCloner();
  57. $cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
  58. $format = $_SERVER['VAR_DUMPER_FORMAT'] ?? null;
  59. switch (true) {
  60. case 'html' === $format:
  61. $dumper = new HtmlDumper();
  62. break;
  63. case 'cli' === $format:
  64. $dumper = new CliDumper();
  65. break;
  66. case 'server' === $format:
  67. case $format && 'tcp' === parse_url($format, \PHP_URL_SCHEME):
  68. $host = 'server' === $format ? $_SERVER['VAR_DUMPER_SERVER'] ?? '127.0.0.1:9912' : $format;
  69. $dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliDumper() : new HtmlDumper();
  70. $dumper = new ServerDumper($host, $dumper, self::getDefaultContextProviders());
  71. break;
  72. default:
  73. $dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliDumper() : new HtmlDumper();
  74. }
  75. if (!$dumper instanceof ServerDumper) {
  76. $dumper = new ContextualizedDumper($dumper, [new SourceContextProvider()]);
  77. }
  78. self::$handler = function ($var) use ($cloner, $dumper) {
  79. $dumper->dump($cloner->cloneVar($var));
  80. };
  81. }
  82. private static function getDefaultContextProviders(): array
  83. {
  84. $contextProviders = [];
  85. if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && class_exists(Request::class)) {
  86. $requestStack = new RequestStack();
  87. $requestStack->push(Request::createFromGlobals());
  88. $contextProviders['request'] = new RequestContextProvider($requestStack);
  89. }
  90. $fileLinkFormatter = class_exists(FileLinkFormatter::class) ? new FileLinkFormatter(null, $requestStack ?? null) : null;
  91. return $contextProviders + [
  92. 'cli' => new CliContextProvider(),
  93. 'source' => new SourceContextProvider(null, null, $fileLinkFormatter),
  94. ];
  95. }
  96. }