PageRenderTime 72ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/www/vendor/twig/lib/Twig/Extension/FormExtension.php

https://bitbucket.org/wayfarer/verse
PHP | 256 lines | 132 code | 33 blank | 91 comment | 9 complexity | 6b46989490e2b5b22df91f26d978f3f5 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Twig\Extension;
  11. use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
  12. use Symfony\Component\Form\FormView;
  13. use Symfony\Component\Form\Exception\FormException;
  14. /**
  15. * FormExtension extends Twig with form capabilities.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  19. */
  20. class FormExtension extends \Twig_Extension
  21. {
  22. protected $resources;
  23. protected $templates;
  24. protected $environment;
  25. protected $themes;
  26. protected $varStack;
  27. public function __construct(array $resources = array())
  28. {
  29. $this->themes = new \SplObjectStorage();
  30. $this->varStack = new \SplObjectStorage();
  31. $this->templates = new \SplObjectStorage();
  32. $this->resources = $resources;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function initRuntime(\Twig_Environment $environment)
  38. {
  39. $this->environment = $environment;
  40. }
  41. /**
  42. * Sets a theme for a given view.
  43. *
  44. * @param FormView $view A FormView instance
  45. * @param array $resources An array of resources
  46. */
  47. public function setTheme(FormView $view, array $resources)
  48. {
  49. $this->themes->attach($view, $resources);
  50. $this->templates->detach($view);
  51. }
  52. /**
  53. * Returns the token parser instance to add to the existing list.
  54. *
  55. * @return array An array of Twig_TokenParser instances
  56. */
  57. public function getTokenParsers()
  58. {
  59. return array(
  60. // {% form_theme form "SomeBungle::widgets.twig" %}
  61. new FormThemeTokenParser(),
  62. );
  63. }
  64. public function getFunctions()
  65. {
  66. return array(
  67. 'form_enctype' => new \Twig_Function_Method($this, 'renderEnctype', array('is_safe' => array('html'))),
  68. 'form_widget' => new \Twig_Function_Method($this, 'renderWidget', array('is_safe' => array('html'))),
  69. 'form_errors' => new \Twig_Function_Method($this, 'renderErrors', array('is_safe' => array('html'))),
  70. 'form_label' => new \Twig_Function_Method($this, 'renderLabel', array('is_safe' => array('html'))),
  71. 'form_row' => new \Twig_Function_Method($this, 'renderRow', array('is_safe' => array('html'))),
  72. 'form_rest' => new \Twig_Function_Method($this, 'renderRest', array('is_safe' => array('html'))),
  73. );
  74. }
  75. /**
  76. * Renders the HTML enctype in the form tag, if necessary
  77. *
  78. * Example usage in Twig templates:
  79. *
  80. * <form action="..." method="post" {{ form_enctype(form) }}>
  81. *
  82. * @param FormView $view The view for which to render the encoding type
  83. */
  84. public function renderEnctype(FormView $view)
  85. {
  86. return $this->render($view, 'enctype');
  87. }
  88. /**
  89. * Renders a row for the view.
  90. *
  91. * @param FormView $view The view to render as a row
  92. * @param array $variables An array of variables
  93. */
  94. public function renderRow(FormView $view, array $variables = array())
  95. {
  96. return $this->render($view, 'row', $variables);
  97. }
  98. public function renderRest(FormView $view, array $variables = array())
  99. {
  100. return $this->render($view, 'rest', $variables);
  101. }
  102. /**
  103. * Renders the HTML for a given view
  104. *
  105. * Example usage in Twig:
  106. *
  107. * {{ form_widget(view) }}
  108. *
  109. * You can pass attributes element during the call:
  110. *
  111. * {{ form_widget(view, {'class': 'foo'}) }}
  112. *
  113. * Some fields also accept additional variables as parameters:
  114. *
  115. * {{ form_widget(view, {}, {'separator': '+++++'}) }}
  116. *
  117. * @param FormView $view The view to render
  118. * @param array $variables Additional variables passed to the template
  119. */
  120. public function renderWidget(FormView $view, array $variables = array())
  121. {
  122. return $this->render($view, 'widget', $variables);
  123. }
  124. /**
  125. * Renders the errors of the given view
  126. *
  127. * @param FormView $view The view to render the errors for
  128. */
  129. public function renderErrors(FormView $view)
  130. {
  131. return $this->render($view, 'errors');
  132. }
  133. /**
  134. * Renders the label of the given view
  135. *
  136. * @param FormView $view The view to render the label for
  137. * @param string $label Label name
  138. */
  139. public function renderLabel(FormView $view, $label = null)
  140. {
  141. return $this->render($view, 'label', null === $label ? array() : array('label' => $label));
  142. }
  143. protected function render(FormView $view, $section, array $variables = array())
  144. {
  145. $templates = $this->getTemplates($view);
  146. $blocks = $view->get('types');
  147. array_unshift($blocks, '_'.$view->get('id'));
  148. foreach ($blocks as &$block) {
  149. $block = $block.'_'.$section;
  150. if (isset($templates[$block])) {
  151. if ('widget' === $section || 'row' === $section) {
  152. $view->setRendered();
  153. }
  154. $this->varStack[$view] = array_replace(
  155. $view->all(),
  156. isset($this->varStack[$view]) ? $this->varStack[$view] : array(),
  157. $variables
  158. );
  159. $html = $templates[$block]->renderBlock($block, $this->varStack[$view]);
  160. return $html;
  161. }
  162. }
  163. throw new FormException(sprintf('Unable to render form as none of the following blocks exist: "%s".', implode('", "', $blocks)));
  164. }
  165. /**
  166. * Returns the templates used by the view.
  167. *
  168. * templates are looked for in the following resources:
  169. * * resources from the themes (and its parents)
  170. * * default resources
  171. *
  172. * @param FormView $view The view
  173. *
  174. * @return array An array of Twig_TemplateInterface instances
  175. */
  176. protected function getTemplates(FormView $view)
  177. {
  178. if (!$this->templates->contains($view)) {
  179. // defaults
  180. $all = $this->resources;
  181. // themes
  182. $parent = $view;
  183. do {
  184. if (isset($this->themes[$parent])) {
  185. $all = array_merge($all, $this->themes[$parent]);
  186. }
  187. } while ($parent = $parent->getParent());
  188. $templates = array();
  189. foreach ($all as $resource) {
  190. if (!$resource instanceof \Twig_Template) {
  191. $resource = $this->environment->loadTemplate($resource);
  192. }
  193. $blocks = array();
  194. foreach ($this->getBlockNames($resource) as $name) {
  195. $blocks[$name] = $resource;
  196. }
  197. $templates = array_replace($templates, $blocks);
  198. }
  199. $this->templates->attach($view, $templates);
  200. } else {
  201. $templates = $this->templates[$view];
  202. }
  203. return $templates;
  204. }
  205. protected function getBlockNames($resource)
  206. {
  207. $names = $resource->getBlockNames();
  208. $parent = $resource;
  209. while (false !== $parent = $parent->getParent(array())) {
  210. $names = array_merge($names, $parent->getBlockNames());
  211. }
  212. return array_unique($names);
  213. }
  214. /**
  215. * Returns the name of the extension.
  216. *
  217. * @return string The extension name
  218. */
  219. public function getName()
  220. {
  221. return 'form';
  222. }
  223. }