[ Index ]

PHP Cross Reference of phpBB-3.3.14-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\DependencyInjection\ContainerInterface;
  17  use Symfony\Component\Form\ChoiceList\View\ChoiceView;
  18  use Symfony\Component\Form\FormView;
  19  use Twig\Environment;
  20  use Twig\Extension\AbstractExtension;
  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       * @deprecated since version 3.2, to be removed in 4.0 alongside with magic methods below
  35       */
  36      private $renderer;
  37  
  38      public function __construct($renderer = null)
  39      {
  40          if ($renderer instanceof TwigRendererInterface) {
  41              @trigger_error(sprintf('Passing a Twig Form Renderer to the "%s" constructor is deprecated since Symfony 3.2 and won\'t be possible in 4.0. Pass the Twig\Environment to the TwigRendererEngine constructor instead.', static::class), \E_USER_DEPRECATED);
  42          } elseif (null !== $renderer && !(\is_array($renderer) && isset($renderer[0], $renderer[1]) && $renderer[0] instanceof ContainerInterface)) {
  43              throw new \InvalidArgumentException(sprintf('Passing any arguments to the constructor of "%s" is reserved for internal use.', __CLASS__));
  44          }
  45          $this->renderer = $renderer;
  46      }
  47  
  48      /**
  49       * {@inheritdoc}
  50       *
  51       * To be removed in 4.0
  52       */
  53      public function initRuntime(Environment $environment)
  54      {
  55          if ($this->renderer instanceof TwigRendererInterface) {
  56              $this->renderer->setEnvironment($environment);
  57          } elseif (\is_array($this->renderer)) {
  58              $this->renderer[2] = $environment;
  59          }
  60      }
  61  
  62      /**
  63       * {@inheritdoc}
  64       */
  65      public function getTokenParsers()
  66      {
  67          return [
  68              // {% form_theme form "SomeBundle::widgets.twig" %}
  69              new FormThemeTokenParser(),
  70          ];
  71      }
  72  
  73      /**
  74       * {@inheritdoc}
  75       */
  76      public function getFunctions()
  77      {
  78          return [
  79              new TwigFunction('form_widget', null, ['node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => ['html']]),
  80              new TwigFunction('form_errors', null, ['node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => ['html']]),
  81              new TwigFunction('form_label', null, ['node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => ['html']]),
  82              new TwigFunction('form_row', null, ['node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => ['html']]),
  83              new TwigFunction('form_rest', null, ['node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => ['html']]),
  84              new TwigFunction('form', null, ['node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => ['html']]),
  85              new TwigFunction('form_start', null, ['node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => ['html']]),
  86              new TwigFunction('form_end', null, ['node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => ['html']]),
  87              new TwigFunction('csrf_token', ['Symfony\Component\Form\FormRenderer', 'renderCsrfToken']),
  88          ];
  89      }
  90  
  91      /**
  92       * {@inheritdoc}
  93       */
  94      public function getFilters()
  95      {
  96          return [
  97              new TwigFilter('humanize', ['Symfony\Component\Form\FormRenderer', 'humanize']),
  98              new TwigFilter('form_encode_currency', ['Symfony\Component\Form\FormRenderer', 'encodeCurrency'], ['is_safe' => ['html'], 'needs_environment' => true]),
  99          ];
 100      }
 101  
 102      /**
 103       * {@inheritdoc}
 104       */
 105      public function getTests()
 106      {
 107          return [
 108              new TwigTest('selectedchoice', 'Symfony\Bridge\Twig\Extension\twig_is_selected_choice'),
 109              new TwigTest('rootform', 'Symfony\Bridge\Twig\Extension\twig_is_root_form'),
 110          ];
 111      }
 112  
 113      /**
 114       * @internal
 115       */
 116      public function __get($name)
 117      {
 118          if ('renderer' === $name) {
 119              @trigger_error(sprintf('Using the "%s::$renderer" property is deprecated since Symfony 3.2 as it will be removed in 4.0.', __CLASS__), \E_USER_DEPRECATED);
 120  
 121              if (\is_array($this->renderer)) {
 122                  $renderer = $this->renderer[0]->get($this->renderer[1]);
 123                  if (isset($this->renderer[2]) && $renderer instanceof TwigRendererInterface) {
 124                      $renderer->setEnvironment($this->renderer[2]);
 125                  }
 126                  $this->renderer = $renderer;
 127              }
 128          }
 129  
 130          return $this->$name;
 131      }
 132  
 133      /**
 134       * @internal
 135       */
 136      public function __set($name, $value)
 137      {
 138          if ('renderer' === $name) {
 139              @trigger_error(sprintf('Using the "%s::$renderer" property is deprecated since Symfony 3.2 as it will be removed in 4.0.', __CLASS__), \E_USER_DEPRECATED);
 140          }
 141  
 142          $this->$name = $value;
 143      }
 144  
 145      /**
 146       * @internal
 147       */
 148      public function __isset($name)
 149      {
 150          if ('renderer' === $name) {
 151              @trigger_error(sprintf('Using the "%s::$renderer" property is deprecated since Symfony 3.2 as it will be removed in 4.0.', __CLASS__), \E_USER_DEPRECATED);
 152          }
 153  
 154          return isset($this->$name);
 155      }
 156  
 157      /**
 158       * @internal
 159       */
 160      public function __unset($name)
 161      {
 162          if ('renderer' === $name) {
 163              @trigger_error(sprintf('Using the "%s::$renderer" property is deprecated since Symfony 3.2 as it will be removed in 4.0.', __CLASS__), \E_USER_DEPRECATED);
 164          }
 165  
 166          unset($this->$name);
 167      }
 168  
 169      /**
 170       * {@inheritdoc}
 171       */
 172      public function getName()
 173      {
 174          return 'form';
 175      }
 176  }
 177  
 178  /**
 179   * Returns whether a choice is selected for a given form value.
 180   *
 181   * This is a function and not callable due to performance reasons.
 182   *
 183   * @param string|array $selectedValue The selected value to compare
 184   *
 185   * @return bool Whether the choice is selected
 186   *
 187   * @see ChoiceView::isSelected()
 188   */
 189  function twig_is_selected_choice(ChoiceView $choice, $selectedValue)
 190  {
 191      if (\is_array($selectedValue)) {
 192          return \in_array($choice->value, $selectedValue, true);
 193      }
 194  
 195      return $choice->value === $selectedValue;
 196  }
 197  
 198  /**
 199   * @internal
 200   */
 201  function twig_is_root_form(FormView $formView)
 202  {
 203      return null === $formView->parent;
 204  }


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