Debug.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2011 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Extension_Debug extends Twig_Extension
  11. {
  12. /**
  13. * Returns a list of global functions to add to the existing list.
  14. *
  15. * @return array An array of global functions
  16. */
  17. public function getFunctions()
  18. {
  19. // dump is safe if var_dump is overridden by xdebug
  20. $isDumpOutputHtmlSafe = extension_loaded('xdebug')
  21. // false means that it was not set (and the default is on) or it explicitly enabled
  22. && (false === ini_get('xdebug.overload_var_dump') || ini_get('xdebug.overload_var_dump'))
  23. // false means that it was not set (and the default is on) or it explicitly enabled
  24. // xdebug.overload_var_dump produces HTML only when html_errors is also enabled
  25. && (false === ini_get('html_errors') || ini_get('html_errors'))
  26. ;
  27. return array(
  28. 'dump' => new Twig_Function_Function('twig_var_dump', array('is_safe' => $isDumpOutputHtmlSafe ? array('html') : array(), 'needs_context' => true, 'needs_environment' => true)),
  29. );
  30. }
  31. /**
  32. * Returns the name of the extension.
  33. *
  34. * @return string The extension name
  35. */
  36. public function getName()
  37. {
  38. return 'debug';
  39. }
  40. }
  41. function twig_var_dump(Twig_Environment $env, $context)
  42. {
  43. if (!$env->isDebug()) {
  44. return;
  45. }
  46. ob_start();
  47. $count = func_num_args();
  48. if (2 === $count) {
  49. $vars = array();
  50. foreach ($context as $key => $value) {
  51. if (!$value instanceof Twig_Template) {
  52. $vars[$key] = $value;
  53. }
  54. }
  55. var_dump($vars);
  56. } else {
  57. for ($i = 2; $i < $count; $i++) {
  58. var_dump(func_get_arg($i));
  59. }
  60. }
  61. return ob_get_clean();
  62. }