SearchAndRenderBlockNode.php 4.5 KB

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