VarDumper.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. self::$handler = $callable;
  46. return $prevHandler;
  47. }
  48. }