ThemeRenderer.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Component\Form\Renderer;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\Form\Renderer\Theme\FormThemeInterface;
  13. use Symfony\Component\Form\Renderer\Theme\FormThemeFactoryInterface;
  14. use Symfony\Component\Form\Renderer\Plugin\FormRendererPluginInterface;
  15. class ThemeRenderer implements FormRendererInterface, \ArrayAccess, \IteratorAggregate
  16. {
  17. private $form;
  18. private $block;
  19. private $themeFactory;
  20. private $theme;
  21. private $vars = array();
  22. private $changes = array();
  23. private $initialized = false;
  24. /**
  25. * Is the form attached to this renderer rendered?
  26. *
  27. * Rendering happens when either the widget or the row method was called.
  28. * Row implicitly includes widget, however certain rendering mechanisms
  29. * have to skip widget rendering when a row is rendered.
  30. *
  31. * @var Boolean
  32. */
  33. private $rendered = false;
  34. public function __construct(FormThemeFactoryInterface $themeFactory, $template = null)
  35. {
  36. $this->themeFactory = $themeFactory;
  37. $this->setTemplate($template);
  38. }
  39. public function __clone()
  40. {
  41. foreach ($this->changes as $key => $change) {
  42. if (is_object($change)) {
  43. $this->changes[$key] = clone $change;
  44. }
  45. }
  46. }
  47. private function initialize()
  48. {
  49. if (!$this->initialized) {
  50. $this->initialized = true;
  51. // Make sure that plugins and set variables are applied in the
  52. // order they were added
  53. foreach ($this->changes as $key => $value) {
  54. if ($value instanceof FormRendererPluginInterface) {
  55. $value->setUp($this->form, $this);
  56. } else {
  57. $this->vars[$key] = $value;
  58. }
  59. }
  60. $this->changes = array();
  61. }
  62. }
  63. public function setForm(FormInterface $form)
  64. {
  65. $this->form = $form;
  66. }
  67. public function setTemplate($template)
  68. {
  69. $this->setTheme($this->themeFactory->create($template));
  70. }
  71. public function setTheme(FormThemeInterface $theme)
  72. {
  73. $this->theme = $theme;
  74. }
  75. public function getTheme()
  76. {
  77. return $this->theme;
  78. }
  79. public function setBlock($block)
  80. {
  81. $this->block = $block;
  82. }
  83. public function getBlock()
  84. {
  85. return $this->block;
  86. }
  87. public function addPlugin(FormRendererPluginInterface $plugin)
  88. {
  89. $this->initialized = false;
  90. $this->changes[] = $plugin;
  91. }
  92. public function setVar($name, $value)
  93. {
  94. if ($this->initialized) {
  95. $this->vars[$name] = $value;
  96. } else {
  97. $this->changes[$name] = $value;
  98. }
  99. }
  100. public function setAttribute($name, $value)
  101. {
  102. // handling through $this->changes not necessary
  103. $this->vars['attr'][$name] = $value;
  104. }
  105. public function hasVar($name)
  106. {
  107. return array_key_exists($name, $this->vars);
  108. }
  109. public function getVar($name)
  110. {
  111. $this->initialize();
  112. // TODO exception handling
  113. if (isset($this->vars[$name])) {
  114. return $this->vars[$name];
  115. }
  116. return null;
  117. }
  118. public function getVars()
  119. {
  120. $this->initialize();
  121. return $this->vars;
  122. }
  123. public function isRendered()
  124. {
  125. return $this->rendered;
  126. }
  127. public function getWidget(array $vars = array())
  128. {
  129. $this->rendered = true;
  130. return $this->render('widget', $vars);
  131. }
  132. public function getErrors(array $vars = array())
  133. {
  134. return $this->render('errors', $vars);
  135. }
  136. public function getRow(array $vars = array())
  137. {
  138. $this->rendered = true;
  139. return $this->render('row', $vars);
  140. }
  141. public function getRest(array $vars = array())
  142. {
  143. return $this->render('rest', $vars);
  144. }
  145. /**
  146. * Renders the label of the given form
  147. *
  148. * @param FormInterface $form The form to render the label for
  149. * @param array $params Additional variables passed to the block
  150. */
  151. public function getLabel($label = null, array $vars = array())
  152. {
  153. if (null !== $label) {
  154. $vars['label'] = $label;
  155. }
  156. return $this->render('label', $vars);
  157. }
  158. public function getEnctype()
  159. {
  160. return $this->render('enctype', $this->vars);
  161. }
  162. protected function render($part, array $vars = array())
  163. {
  164. $this->initialize();
  165. return $this->theme->render($this->block, $part, array_replace(
  166. $this->vars,
  167. $vars
  168. ));
  169. }
  170. public function offsetGet($name)
  171. {
  172. $this->initialize();
  173. return $this->vars['fields'][$name];
  174. }
  175. public function offsetExists($name)
  176. {
  177. $this->initialize();
  178. return isset($this->vars['fields'][$name]);
  179. }
  180. public function offsetSet($name, $value)
  181. {
  182. throw new \BadMethodCallException('Not supported');
  183. }
  184. public function offsetUnset($name)
  185. {
  186. throw new \BadMethodCallException('Not supported');
  187. }
  188. public function getIterator()
  189. {
  190. $this->initialize();
  191. if (isset($this->vars['fields'])) {
  192. $this->rendered = true;
  193. return new \ArrayIterator($this->vars['fields']);
  194. }
  195. return new \ArrayIterator(array());
  196. }
  197. }