DumpNode.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Bridge\Twig\Node;
  11. use Twig\Compiler;
  12. use Twig\Node\Node;
  13. /**
  14. * @author Julien Galenski <julien.galenski@gmail.com>
  15. */
  16. class DumpNode extends Node
  17. {
  18. private $varPrefix;
  19. public function __construct($varPrefix, Node $values = null, $lineno, $tag = null)
  20. {
  21. $nodes = array();
  22. if (null !== $values) {
  23. $nodes['values'] = $values;
  24. }
  25. parent::__construct($nodes, array(), $lineno, $tag);
  26. $this->varPrefix = $varPrefix;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function compile(Compiler $compiler)
  32. {
  33. $compiler
  34. ->write("if (\$this->env->isDebug()) {\n")
  35. ->indent();
  36. if (!$this->hasNode('values')) {
  37. // remove embedded templates (macros) from the context
  38. $compiler
  39. ->write(sprintf('$%svars = array();'."\n", $this->varPrefix))
  40. ->write(sprintf('foreach ($context as $%1$skey => $%1$sval) {'."\n", $this->varPrefix))
  41. ->indent()
  42. ->write(sprintf('if (!$%sval instanceof \Twig\Template) {'."\n", $this->varPrefix))
  43. ->indent()
  44. ->write(sprintf('$%1$svars[$%1$skey] = $%1$sval;'."\n", $this->varPrefix))
  45. ->outdent()
  46. ->write("}\n")
  47. ->outdent()
  48. ->write("}\n")
  49. ->addDebugInfo($this)
  50. ->write(sprintf('\Symfony\Component\VarDumper\VarDumper::dump($%svars);'."\n", $this->varPrefix));
  51. } elseif (($values = $this->getNode('values')) && 1 === $values->count()) {
  52. $compiler
  53. ->addDebugInfo($this)
  54. ->write('\Symfony\Component\VarDumper\VarDumper::dump(')
  55. ->subcompile($values->getNode(0))
  56. ->raw(");\n");
  57. } else {
  58. $compiler
  59. ->addDebugInfo($this)
  60. ->write('\Symfony\Component\VarDumper\VarDumper::dump(array('."\n")
  61. ->indent();
  62. foreach ($values as $node) {
  63. $compiler->write('');
  64. if ($node->hasAttribute('name')) {
  65. $compiler
  66. ->string($node->getAttribute('name'))
  67. ->raw(' => ');
  68. }
  69. $compiler
  70. ->subcompile($node)
  71. ->raw(",\n");
  72. }
  73. $compiler
  74. ->outdent()
  75. ->write("));\n");
  76. }
  77. $compiler
  78. ->outdent()
  79. ->write("}\n");
  80. }
  81. }