]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Extension/FormExtension.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / twig-bridge / Symfony / Bridge / Twig / Extension / FormExtension.php
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\TokenParser\FormThemeTokenParser;
15 use Symfony\Bridge\Twig\Form\TwigRendererInterface;
16 use Symfony\Component\Form\Extension\Core\View\ChoiceView;
17
18 /**
19 * FormExtension extends Twig with form capabilities.
20 *
21 * @author Fabien Potencier <fabien@symfony.com>
22 * @author Bernhard Schussek <bschussek@gmail.com>
23 */
24 class FormExtension extends \Twig_Extension
25 {
26 /**
27 * This property is public so that it can be accessed directly from compiled
28 * templates without having to call a getter, which slightly decreases performance.
29 *
30 * @var TwigRendererInterface
31 */
32 public $renderer;
33
34 public function __construct(TwigRendererInterface $renderer)
35 {
36 $this->renderer = $renderer;
37 }
38
39 /**
40 * {@inheritdoc}
41 */
42 public function initRuntime(\Twig_Environment $environment)
43 {
44 $this->renderer->setEnvironment($environment);
45 }
46
47 /**
48 * {@inheritdoc}
49 */
50 public function getTokenParsers()
51 {
52 return array(
53 // {% form_theme form "SomeBundle::widgets.twig" %}
54 new FormThemeTokenParser(),
55 );
56 }
57
58 /**
59 * {@inheritdoc}
60 */
61 public function getFunctions()
62 {
63 return array(
64 'form_enctype' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\FormEnctypeNode', array('is_safe' => array('html'))),
65 'form_widget' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
66 'form_errors' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
67 'form_label' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
68 'form_row' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
69 'form_rest' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
70 'form' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\RenderBlockNode', array('is_safe' => array('html'))),
71 'form_start' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\RenderBlockNode', array('is_safe' => array('html'))),
72 'form_end' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\RenderBlockNode', array('is_safe' => array('html'))),
73 'csrf_token' => new \Twig_Function_Method($this, 'renderer->renderCsrfToken'),
74 );
75 }
76
77 /**
78 * {@inheritdoc}
79 */
80 public function getFilters()
81 {
82 return array(
83 'humanize' => new \Twig_Filter_Method($this, 'renderer->humanize'),
84 );
85 }
86
87 /**
88 * {@inheritdoc}
89 */
90 public function getTests()
91 {
92 return array(
93 'selectedchoice' => new \Twig_Test_Method($this, 'isSelectedChoice'),
94 );
95 }
96
97 /**
98 * Returns whether a choice is selected for a given form value.
99 *
100 * Unfortunately Twig does not support an efficient way to execute the
101 * "is_selected" closure passed to the template by ChoiceType. It is faster
102 * to implement the logic here (around 65ms for a specific form).
103 *
104 * Directly implementing the logic here is also faster than doing so in
105 * ChoiceView (around 30ms).
106 *
107 * The worst option tested so far is to implement the logic in ChoiceView
108 * and access the ChoiceView method directly in the template. Doing so is
109 * around 220ms slower than doing the method call here in the filter. Twig
110 * seems to be much more efficient at executing filters than at executing
111 * methods of an object.
112 *
113 * @param ChoiceView $choice The choice to check.
114 * @param string|array $selectedValue The selected value to compare.
115 *
116 * @return Boolean Whether the choice is selected.
117 *
118 * @see ChoiceView::isSelected()
119 */
120 public function isSelectedChoice(ChoiceView $choice, $selectedValue)
121 {
122 if (is_array($selectedValue)) {
123 return false !== array_search($choice->value, $selectedValue, true);
124 }
125
126 return $choice->value === $selectedValue;
127 }
128
129 /**
130 * {@inheritdoc}
131 */
132 public function getName()
133 {
134 return 'form';
135 }
136 }