/vendors/Twig/Node/Set.php

https://bitbucket.org/mrblackus/micro-muffin · PHP · 117 lines · 81 code · 13 blank · 23 comment · 10 complexity · 0ce3bc521624952ffa8b9dbed705f2b2 MD5 · raw file

  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 set node.
  12. *
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class Twig_Node_Set extends Twig_Node
  16. {
  17. public function __construct($capture, Twig_NodeInterface $names, Twig_NodeInterface $values, $lineno, $tag = null)
  18. {
  19. parent::__construct(array('names' => $names, 'values' => $values), array('capture' => $capture, 'safe' => false), $lineno, $tag);
  20. /*
  21. * Optimizes the node when capture is used for a large block of text.
  22. *
  23. * {% set foo %}foo{% endset %} is compiled to $context['foo'] = new Twig_Markup("foo");
  24. */
  25. if ($this->getAttribute('capture'))
  26. {
  27. $this->setAttribute('safe', true);
  28. $values = $this->getNode('values');
  29. if ($values instanceof Twig_Node_Text)
  30. {
  31. $this->setNode('values', new Twig_Node_Expression_Constant($values->getAttribute('data'), $values->getLine()));
  32. $this->setAttribute('capture', false);
  33. }
  34. }
  35. }
  36. /**
  37. * Compiles the node to PHP.
  38. *
  39. * @param Twig_Compiler A Twig_Compiler instance
  40. */
  41. public function compile(Twig_Compiler $compiler)
  42. {
  43. $compiler->addDebugInfo($this);
  44. if (count($this->getNode('names')) > 1)
  45. {
  46. $compiler->write('list(');
  47. foreach ($this->getNode('names') as $idx => $node)
  48. {
  49. if ($idx)
  50. {
  51. $compiler->raw(', ');
  52. }
  53. $compiler->subcompile($node);
  54. }
  55. $compiler->raw(')');
  56. }
  57. else
  58. {
  59. if ($this->getAttribute('capture'))
  60. {
  61. $compiler
  62. ->write("ob_start();\n")
  63. ->subcompile($this->getNode('values'));
  64. }
  65. $compiler->subcompile($this->getNode('names'), false);
  66. if ($this->getAttribute('capture'))
  67. {
  68. $compiler->raw(" = ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())");
  69. }
  70. }
  71. if (!$this->getAttribute('capture'))
  72. {
  73. $compiler->raw(' = ');
  74. if (count($this->getNode('names')) > 1)
  75. {
  76. $compiler->write('array(');
  77. foreach ($this->getNode('values') as $idx => $value)
  78. {
  79. if ($idx)
  80. {
  81. $compiler->raw(', ');
  82. }
  83. $compiler->subcompile($value);
  84. }
  85. $compiler->raw(')');
  86. }
  87. else
  88. {
  89. if ($this->getAttribute('safe'))
  90. {
  91. $compiler
  92. ->raw("('' === \$tmp = ")
  93. ->subcompile($this->getNode('values'))
  94. ->raw(") ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())");
  95. }
  96. else
  97. {
  98. $compiler->subcompile($this->getNode('values'));
  99. }
  100. }
  101. }
  102. $compiler->raw(";\n");
  103. }
  104. }