Debug.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 overriden by xdebug
  20. $isDumpOutputHtmlSafe = extension_loaded('xdebug') && (false === get_cfg_var('xdebug.overload_var_dump') || get_cfg_var('xdebug.overload_var_dump')) && get_cfg_var('html_errors');
  21. return array(
  22. 'dump' => new Twig_Function_Function('twig_var_dump', array('is_safe' => $isDumpOutputHtmlSafe ? array('html') : array(), 'needs_context' => true, 'needs_environment' => true)),
  23. );
  24. }
  25. /**
  26. * Returns the name of the extension.
  27. *
  28. * @return string The extension name
  29. */
  30. public function getName()
  31. {
  32. return 'debug';
  33. }
  34. }
  35. function twig_var_dump(Twig_Environment $env, $context)
  36. {
  37. if (!$env->isDebug()) {
  38. return;
  39. }
  40. ob_start();
  41. $count = func_num_args();
  42. if (2 === $count) {
  43. $vars = array();
  44. foreach ($context as $key => $value) {
  45. if (!$value instanceof Twig_Template) {
  46. $vars[$key] = $value;
  47. }
  48. }
  49. var_dump($vars);
  50. } else {
  51. for ($i = 2; $i < $count; $i++) {
  52. var_dump(func_get_arg($i));
  53. }
  54. }
  55. return ob_get_clean();
  56. }