SearchAndRenderBlockNode.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Expression\ArrayExpression;
  13. use Twig\Node\Expression\ConstantExpression;
  14. use Twig\Node\Expression\FunctionExpression;
  15. /**
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. class SearchAndRenderBlockNode extends FunctionExpression
  19. {
  20. public function compile(Compiler $compiler)
  21. {
  22. $compiler->addDebugInfo($this);
  23. $compiler->raw('$this->env->getRuntime(\'Symfony\Bridge\Twig\Form\TwigRenderer\')->searchAndRenderBlock(');
  24. preg_match('/_([^_]+)$/', $this->getAttribute('name'), $matches);
  25. $label = null;
  26. $arguments = iterator_to_array($this->getNode('arguments'));
  27. $blockNameSuffix = $matches[1];
  28. if (isset($arguments[0])) {
  29. $compiler->subcompile($arguments[0]);
  30. $compiler->raw(', \''.$blockNameSuffix.'\'');
  31. if (isset($arguments[1])) {
  32. if ('label' === $blockNameSuffix) {
  33. // The "label" function expects the label in the second and
  34. // the variables in the third argument
  35. $label = $arguments[1];
  36. $variables = isset($arguments[2]) ? $arguments[2] : null;
  37. $lineno = $label->getTemplateLine();
  38. if ($label instanceof ConstantExpression) {
  39. // If the label argument is given as a constant, we can either
  40. // strip it away if it is empty, or integrate it into the array
  41. // of variables at compile time.
  42. $labelIsExpression = false;
  43. // Only insert the label into the array if it is not empty
  44. if (!twig_test_empty($label->getAttribute('value'))) {
  45. $originalVariables = $variables;
  46. $variables = new ArrayExpression(array(), $lineno);
  47. $labelKey = new ConstantExpression('label', $lineno);
  48. if (null !== $originalVariables) {
  49. foreach ($originalVariables->getKeyValuePairs() as $pair) {
  50. // Don't copy the original label attribute over if it exists
  51. if ((string) $labelKey !== (string) $pair['key']) {
  52. $variables->addElement($pair['value'], $pair['key']);
  53. }
  54. }
  55. }
  56. // Insert the label argument into the array
  57. $variables->addElement($label, $labelKey);
  58. }
  59. } else {
  60. // The label argument is not a constant, but some kind of
  61. // expression. This expression needs to be evaluated at runtime.
  62. // Depending on the result (whether it is null or not), the
  63. // label in the arguments should take precedence over the label
  64. // in the attributes or not.
  65. $labelIsExpression = true;
  66. }
  67. } else {
  68. // All other functions than "label" expect the variables
  69. // in the second argument
  70. $label = null;
  71. $variables = $arguments[1];
  72. $labelIsExpression = false;
  73. }
  74. if (null !== $variables || $labelIsExpression) {
  75. $compiler->raw(', ');
  76. if (null !== $variables) {
  77. $compiler->subcompile($variables);
  78. }
  79. if ($labelIsExpression) {
  80. if (null !== $variables) {
  81. $compiler->raw(' + ');
  82. }
  83. // Check at runtime whether the label is empty.
  84. // If not, add it to the array at runtime.
  85. $compiler->raw('(twig_test_empty($_label_ = ');
  86. $compiler->subcompile($label);
  87. $compiler->raw(') ? array() : array("label" => $_label_))');
  88. }
  89. }
  90. }
  91. }
  92. $compiler->raw(')');
  93. }
  94. }