Debug.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 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. /**
  11. * Represents a debug node.
  12. *
  13. * @package twig
  14. * @subpackage Twig-extensions
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @version SVN: $Id$
  17. */
  18. class Twig_Extensions_Node_Debug extends Twig_Node
  19. {
  20. public function __construct(Twig_Node_Expression $expr = null, $lineno, $tag = null)
  21. {
  22. parent::__construct(array('expr' => $expr), array(), $lineno, $tag);
  23. }
  24. /**
  25. * Compiles the node to PHP.
  26. *
  27. * @param Twig_Compiler A Twig_Compiler instance
  28. */
  29. public function compile(Twig_Compiler $compiler)
  30. {
  31. $compiler->addDebugInfo($this);
  32. $compiler
  33. ->write("if (\$this->env->isDebug()) {\n")
  34. ->indent()
  35. ;
  36. if (null === $this->getNode('expr')) {
  37. // remove embedded templates (macros) from the context
  38. $compiler
  39. ->write("\$vars = array();\n")
  40. ->write("foreach (\$context as \$key => \$value) {\n")
  41. ->indent()
  42. ->write("if (!\$value instanceof Twig_Template) {\n")
  43. ->indent()
  44. ->write("\$vars[\$key] = \$value;\n")
  45. ->outdent()
  46. ->write("}\n")
  47. ->outdent()
  48. ->write("}\n")
  49. ->write("var_dump(\$vars);\n")
  50. ;
  51. } else {
  52. $compiler
  53. ->write("var_dump(")
  54. ->subcompile($this->getNode('expr'))
  55. ->raw(");\n")
  56. ;
  57. }
  58. $compiler
  59. ->outdent()
  60. ->write("}\n")
  61. ;
  62. }
  63. }