[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/Extension/ -> FormExtension.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\Extension;
  13  
  14  use Symfony\Bridge\Twig\Form\TwigRendererInterface;
  15  use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
  16  use Symfony\Component\Form\Extension\Core\View\ChoiceView;
  17  use Symfony\Component\Form\FormView;
  18  use Twig\Environment;
  19  use Twig\Extension\AbstractExtension;
  20  use Twig\Extension\InitRuntimeInterface;
  21  use Twig\TwigFilter;
  22  use Twig\TwigFunction;
  23  use Twig\TwigTest;
  24  
  25  /**
  26   * FormExtension extends Twig with form capabilities.
  27   *
  28   * @author Fabien Potencier <fabien@symfony.com>
  29   * @author Bernhard Schussek <bschussek@gmail.com>
  30   */
  31  class FormExtension extends AbstractExtension implements InitRuntimeInterface
  32  {
  33      /**
  34       * This property is public so that it can be accessed directly from compiled
  35       * templates without having to call a getter, which slightly decreases performance.
  36       *
  37       * @var TwigRendererInterface
  38       */
  39      public $renderer;
  40  
  41      public function __construct(TwigRendererInterface $renderer)
  42      {
  43          $this->renderer = $renderer;
  44      }
  45  
  46      /**
  47       * {@inheritdoc}
  48       */
  49      public function initRuntime(Environment $environment)
  50      {
  51          $this->renderer->setEnvironment($environment);
  52      }
  53  
  54      /**
  55       * {@inheritdoc}
  56       */
  57      public function getTokenParsers()
  58      {
  59          return array(
  60              // {% form_theme form "SomeBundle::widgets.twig" %}
  61              new FormThemeTokenParser(),
  62          );
  63      }
  64  
  65      /**
  66       * {@inheritdoc}
  67       */
  68      public function getFunctions()
  69      {
  70          return array(
  71              new TwigFunction('form_enctype', null, array('node_class' => 'Symfony\Bridge\Twig\Node\FormEnctypeNode', 'is_safe' => array('html'), 'deprecated' => true, 'alternative' => 'form_start')),
  72              new TwigFunction('form_widget', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
  73              new TwigFunction('form_errors', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
  74              new TwigFunction('form_label', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
  75              new TwigFunction('form_row', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
  76              new TwigFunction('form_rest', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
  77              new TwigFunction('form', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
  78              new TwigFunction('form_start', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
  79              new TwigFunction('form_end', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
  80              new TwigFunction('csrf_token', array($this, 'renderCsrfToken')),
  81          );
  82      }
  83  
  84      /**
  85       * {@inheritdoc}
  86       */
  87      public function getFilters()
  88      {
  89          return array(
  90              new TwigFilter('humanize', array($this, 'humanize')),
  91              new TwigFilter('form_encode_currency', array($this, 'encodeCurrency'), array('is_safe' => array('html'), 'needs_environment' => true)),
  92          );
  93      }
  94  
  95      /**
  96       * {@inheritdoc}
  97       */
  98      public function getTests()
  99      {
 100          return array(
 101              new TwigTest('selectedchoice', array($this, 'isSelectedChoice')),
 102              new TwigTest('rootform', array($this, 'isRootForm')),
 103          );
 104      }
 105  
 106      /**
 107       * Renders a CSRF token.
 108       *
 109       * @param string $intention The intention of the protected action
 110       *
 111       * @return string A CSRF token
 112       */
 113      public function renderCsrfToken($intention)
 114      {
 115          return $this->renderer->renderCsrfToken($intention);
 116      }
 117  
 118      /**
 119       * Makes a technical name human readable.
 120       *
 121       * @param string $text The text to humanize
 122       *
 123       * @return string The humanized text
 124       */
 125      public function humanize($text)
 126      {
 127          return $this->renderer->humanize($text);
 128      }
 129  
 130      /**
 131       * Returns whether a choice is selected for a given form value.
 132       *
 133       * Unfortunately Twig does not support an efficient way to execute the
 134       * "is_selected" closure passed to the template by ChoiceType. It is faster
 135       * to implement the logic here (around 65ms for a specific form).
 136       *
 137       * Directly implementing the logic here is also faster than doing so in
 138       * ChoiceView (around 30ms).
 139       *
 140       * The worst option tested so far is to implement the logic in ChoiceView
 141       * and access the ChoiceView method directly in the template. Doing so is
 142       * around 220ms slower than doing the method call here in the filter. Twig
 143       * seems to be much more efficient at executing filters than at executing
 144       * methods of an object.
 145       *
 146       * @param ChoiceView   $choice        The choice to check
 147       * @param string|array $selectedValue The selected value to compare
 148       *
 149       * @return bool Whether the choice is selected
 150       *
 151       * @see ChoiceView::isSelected()
 152       */
 153      public function isSelectedChoice(ChoiceView $choice, $selectedValue)
 154      {
 155          if (\is_array($selectedValue)) {
 156              return \in_array($choice->value, $selectedValue, true);
 157          }
 158  
 159          return $choice->value === $selectedValue;
 160      }
 161  
 162      /**
 163       * @internal
 164       */
 165      public function isRootForm(FormView $formView)
 166      {
 167          return null === $formView->parent;
 168      }
 169  
 170      /**
 171       * @internal
 172       */
 173      public function encodeCurrency(Environment $environment, $text, $widget = '')
 174      {
 175          if ('UTF-8' === $charset = $environment->getCharset()) {
 176              $text = htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
 177          } else {
 178              $text = htmlentities($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
 179              $text = iconv('UTF-8', $charset, $text);
 180              $widget = iconv('UTF-8', $charset, $widget);
 181          }
 182  
 183          return str_replace('{{ widget }}', $widget, $text);
 184      }
 185  
 186      /**
 187       * {@inheritdoc}
 188       */
 189      public function getName()
 190      {
 191          return 'form';
 192      }
 193  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1