|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bridge\Twig\Node; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Julien Galenski <julien.galenski@gmail.com> |
| 16 | + */ |
| 17 | +class DumpNode extends \Twig_Node |
| 18 | +{ |
| 19 | + public function __construct(\Twig_NodeInterface $values = null, $lineno, $tag = null) |
| 20 | + { |
| 21 | + parent::__construct(array('values' => $values), array(), $lineno, $tag); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * {@inheritdoc} |
| 26 | + */ |
| 27 | + public function compile(\Twig_Compiler $compiler) |
| 28 | + { |
| 29 | + $compiler |
| 30 | + ->write("if (\$this->env->isDebug()) {\n") |
| 31 | + ->indent() |
| 32 | + ; |
| 33 | + |
| 34 | + $values = $this->getNode('values'); |
| 35 | + |
| 36 | + if (null === $values) { |
| 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 | + ->addDebugInfo($this) |
| 50 | + ->write('\Symfony\Component\Debug\Debug::dump($vars);'."\n") |
| 51 | + ; |
| 52 | + } elseif (1 === $values->count()) { |
| 53 | + $compiler |
| 54 | + ->addDebugInfo($this) |
| 55 | + ->write('\Symfony\Component\Debug\Debug::dump(') |
| 56 | + ->subcompile($values->getNode(0)) |
| 57 | + ->raw(");\n") |
| 58 | + ; |
| 59 | + } else { |
| 60 | + $compiler |
| 61 | + ->addDebugInfo($this) |
| 62 | + ->write('\Symfony\Component\Debug\Debug::dump(array(') |
| 63 | + ->indent() |
| 64 | + ; |
| 65 | + foreach ($values as $node) { |
| 66 | + $compiler->addIndentation(); |
| 67 | + if ($node->hasAttribute('name')) { |
| 68 | + $compiler |
| 69 | + ->string($node->getAttribute('name')) |
| 70 | + ->raw(' => ') |
| 71 | + ; |
| 72 | + } |
| 73 | + $compiler |
| 74 | + ->subcompile($node) |
| 75 | + ->raw(",\n") |
| 76 | + ; |
| 77 | + } |
| 78 | + $compiler |
| 79 | + ->outdent() |
| 80 | + ->raw("));\n") |
| 81 | + ; |
| 82 | + } |
| 83 | + |
| 84 | + $compiler |
| 85 | + ->outdent() |
| 86 | + ->write("}\n") |
| 87 | + ; |
| 88 | + } |
| 89 | +} |
0 commit comments