Debug.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. public function getFunctions()
  13. {
  14. // dump is safe if var_dump is overridden by xdebug
  15. $isDumpOutputHtmlSafe = extension_loaded('xdebug')
  16. // false means that it was not set (and the default is on) or it explicitly enabled
  17. && (false === ini_get('xdebug.overload_var_dump') || ini_get('xdebug.overload_var_dump'))
  18. // false means that it was not set (and the default is on) or it explicitly enabled
  19. // xdebug.overload_var_dump produces HTML only when html_errors is also enabled
  20. && (false === ini_get('html_errors') || ini_get('html_errors'))
  21. || 'cli' === php_sapi_name()
  22. ;
  23. return array(
  24. new Twig_SimpleFunction('dump', 'twig_var_dump', array('is_safe' => $isDumpOutputHtmlSafe ? array('html') : array(), 'needs_context' => true, 'needs_environment' => true)),
  25. );
  26. }
  27. public function getName()
  28. {
  29. return 'debug';
  30. }
  31. }
  32. function twig_var_dump(Twig_Environment $env, $context)
  33. {
  34. if (!$env->isDebug()) {
  35. return;
  36. }
  37. ob_start();
  38. $count = func_num_args();
  39. if (2 === $count) {
  40. $vars = array();
  41. foreach ($context as $key => $value) {
  42. if (!$value instanceof Twig_Template) {
  43. $vars[$key] = $value;
  44. }
  45. }
  46. var_dump($vars);
  47. } else {
  48. for ($i = 2; $i < $count; ++$i) {
  49. var_dump(func_get_arg($i));
  50. }
  51. }
  52. return ob_get_clean();
  53. }