VarDumper.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\HtmlDumper;
  15. // Load the global dump() function
  16. require_once __DIR__.'/Resources/functions/dump.php';
  17. /**
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. class VarDumper
  21. {
  22. private static $handler;
  23. public static function dump($var)
  24. {
  25. if (null === self::$handler) {
  26. $cloner = new VarCloner();
  27. $cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
  28. if (isset($_SERVER['VAR_DUMPER_FORMAT'])) {
  29. $dumper = 'html' === $_SERVER['VAR_DUMPER_FORMAT'] ? new HtmlDumper() : new CliDumper();
  30. } else {
  31. $dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper() : new HtmlDumper();
  32. }
  33. self::$handler = function ($var) use ($cloner, $dumper) {
  34. $dumper->dump($cloner->cloneVar($var));
  35. };
  36. }
  37. return (self::$handler)($var);
  38. }
  39. public static function setHandler(callable $callable = null)
  40. {
  41. $prevHandler = self::$handler;
  42. self::$handler = $callable;
  43. return $prevHandler;
  44. }
  45. }