VarDumper.php 1.4 KB

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