[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/Node/ -> SearchAndRenderBlockNode.php (source)

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


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1