Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

FormExtension.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 5.33 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Bridge\Twig\Extension;
013   
014  use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
015  use Symfony\Bridge\Twig\Form\TwigRendererInterface;
016  use Symfony\Component\Form\Extension\Core\View\ChoiceView;
017   
018  /**
019   * FormExtension extends Twig with form capabilities.
020   *
021   * @author Fabien Potencier <fabien@symfony.com>
022   * @author Bernhard Schussek <bschussek@gmail.com>
023   */
024  class FormExtension extends \Twig_Extension implements \Twig_Extension_InitRuntimeInterface
025  {
026      /**
027       * This property is public so that it can be accessed directly from compiled
028       * templates without having to call a getter, which slightly decreases performance.
029       *
030       * @var TwigRendererInterface
031       */
032      public $renderer;
033   
034      public function __construct(TwigRendererInterface $renderer)
035      {
036          $this->renderer = $renderer;
037      }
038   
039      /**
040       * {@inheritdoc}
041       */
042      public function initRuntime(\Twig_Environment $environment)
043      {
044          $this->renderer->setEnvironment($environment);
045      }
046   
047      /**
048       * {@inheritdoc}
049       */
050      public function getTokenParsers()
051      {
052          return array(
053              // {% form_theme form "SomeBundle::widgets.twig" %}
054              new FormThemeTokenParser(),
055          );
056      }
057   
058      /**
059       * {@inheritdoc}
060       */
061      public function getFunctions()
062      {
063          return array(
064              new \Twig_SimpleFunction('form_enctype', null, array('node_class' => 'Symfony\Bridge\Twig\Node\FormEnctypeNode', 'is_safe' => array('html'), 'deprecated' => true, 'alternative' => 'form_start')),
065              new \Twig_SimpleFunction('form_widget', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
066              new \Twig_SimpleFunction('form_errors', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
067              new \Twig_SimpleFunction('form_label', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
068              new \Twig_SimpleFunction('form_row', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
069              new \Twig_SimpleFunction('form_rest', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
070              new \Twig_SimpleFunction('form', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
071              new \Twig_SimpleFunction('form_start', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
072              new \Twig_SimpleFunction('form_end', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),
073              new \Twig_SimpleFunction('csrf_token', array($this, 'renderCsrfToken')),
074          );
075      }
076   
077      /**
078       * {@inheritdoc}
079       */
080      public function getFilters()
081      {
082          return array(
083              new \Twig_SimpleFilter('humanize', array($this, 'humanize')),
084          );
085      }
086   
087      /**
088       * {@inheritdoc}
089       */
090      public function getTests()
091      {
092          return array(
093              new \Twig_SimpleTest('selectedchoice', array($this, 'isSelectedChoice')),
094          );
095      }
096   
097      /**
098       * Renders a CSRF token.
099       *
100       * @param string $intention The intention of the protected action
101       *
102       * @return string A CSRF token
103       */
104      public function renderCsrfToken($intention)
105      {
106          return $this->renderer->renderCsrfToken($intention);
107      }
108   
109      /**
110       * Makes a technical name human readable.
111       *
112       * @param string $text The text to humanize
113       *
114       * @return string The humanized text
115       */
116      public function humanize($text)
117      {
118          return $this->renderer->humanize($text);
119      }
120   
121      /**
122       * Returns whether a choice is selected for a given form value.
123       *
124       * Unfortunately Twig does not support an efficient way to execute the
125       * "is_selected" closure passed to the template by ChoiceType. It is faster
126       * to implement the logic here (around 65ms for a specific form).
127       *
128       * Directly implementing the logic here is also faster than doing so in
129       * ChoiceView (around 30ms).
130       *
131       * The worst option tested so far is to implement the logic in ChoiceView
132       * and access the ChoiceView method directly in the template. Doing so is
133       * around 220ms slower than doing the method call here in the filter. Twig
134       * seems to be much more efficient at executing filters than at executing
135       * methods of an object.
136       *
137       * @param ChoiceView   $choice        The choice to check
138       * @param string|array $selectedValue The selected value to compare
139       *
140       * @return bool Whether the choice is selected
141       *
142       * @see ChoiceView::isSelected()
143       */
144      public function isSelectedChoice(ChoiceView $choice, $selectedValue)
145      {
146          if (is_array($selectedValue)) {
147              return in_array($choice->value, $selectedValue, true);
148          }
149   
150          return $choice->value === $selectedValue;
151      }
152   
153      /**
154       * {@inheritdoc}
155       */
156      public function getName()
157      {
158          return 'form';
159      }
160  }
161