Ei kuvausta

VarDumper.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\VarDumper\Caster\ReflectionCaster;
  12. use Symfony\Component\VarDumper\Cloner\VarCloner;
  13. use Symfony\Component\VarDumper\Dumper\CliDumper;
  14. use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
  15. use Symfony\Component\VarDumper\Dumper\ContextualizedDumper;
  16. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  17. // Load the global dump() function
  18. require_once __DIR__.'/Resources/functions/dump.php';
  19. /**
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. */
  22. class VarDumper
  23. {
  24. private static $handler;
  25. public static function dump($var)
  26. {
  27. if (null === self::$handler) {
  28. $cloner = new VarCloner();
  29. $cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
  30. if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
  31. $dumper = 'html' === $_SERVER['VAR_DUMPER_FORMAT'] ? new HtmlDumper() : new CliDumper();
  32. } else {
  33. $dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper() : new HtmlDumper();
  34. }
  35. $dumper = new ContextualizedDumper($dumper, [new SourceContextProvider()]);
  36. self::$handler = function ($var) use ($cloner, $dumper) {
  37. $dumper->dump($cloner->cloneVar($var));
  38. };
  39. }
  40. return (self::$handler)($var);
  41. }
  42. public static function setHandler(callable $callable = null)
  43. {
  44. $prevHandler = self::$handler;
  45. // Prevent replacing the handler with expected format as soon as the env var was set:
  46. if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
  47. return $prevHandler;
  48. }
  49. self::$handler = $callable;
  50. return $prevHandler;
  51. }
  52. }