VarDumper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. private static $handler;
  31. public static function dump($var)
  32. {
  33. if (null === self::$handler) {
  34. self::register();
  35. }
  36. return (self::$handler)($var);
  37. }
  38. public static function setHandler(callable $callable = null)
  39. {
  40. $prevHandler = self::$handler;
  41. // Prevent replacing the handler with expected format as soon as the env var was set:
  42. if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
  43. return $prevHandler;
  44. }
  45. self::$handler = $callable;
  46. return $prevHandler;
  47. }
  48. private static function register(): void
  49. {
  50. $cloner = new VarCloner();
  51. $cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
  52. $format = $_SERVER['VAR_DUMPER_FORMAT'] ?? null;
  53. switch (true) {
  54. case 'html' === $format:
  55. $dumper = new HtmlDumper();
  56. break;
  57. case 'cli' === $format:
  58. $dumper = new CliDumper();
  59. break;
  60. case 'server' === $format:
  61. case 'tcp' === parse_url($format, \PHP_URL_SCHEME):
  62. $host = 'server' === $format ? $_SERVER['VAR_DUMPER_SERVER'] ?? '127.0.0.1:9912' : $format;
  63. $dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliDumper() : new HtmlDumper();
  64. $dumper = new ServerDumper($host, $dumper, self::getDefaultContextProviders());
  65. break;
  66. default:
  67. $dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliDumper() : new HtmlDumper();
  68. }
  69. if (!$dumper instanceof ServerDumper) {
  70. $dumper = new ContextualizedDumper($dumper, [new SourceContextProvider()]);
  71. }
  72. self::$handler = function ($var) use ($cloner, $dumper) {
  73. $dumper->dump($cloner->cloneVar($var));
  74. };
  75. }
  76. private static function getDefaultContextProviders(): array
  77. {
  78. $contextProviders = [];
  79. if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && (class_exists(Request::class))) {
  80. $requestStack = new RequestStack();
  81. $requestStack->push(Request::createFromGlobals());
  82. $contextProviders['request'] = new RequestContextProvider($requestStack);
  83. }
  84. $fileLinkFormatter = class_exists(FileLinkFormatter::class) ? new FileLinkFormatter(null, $requestStack ?? null) : null;
  85. return $contextProviders + [
  86. 'cli' => new CliContextProvider(),
  87. 'source' => new SourceContextProvider(null, null, $fileLinkFormatter),
  88. ];
  89. }
  90. }