SearchAndRenderBlockNode.php 4.4 KB

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