ThemeRenderer.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. class ThemeRenderer implements FormRendererInterface, \ArrayAccess, \IteratorAggregate
  15. {
  16. private $blockHistory = array();
  17. private $themeFactory;
  18. private $theme;
  19. private $vars = array();
  20. /**
  21. * Is the form attached to this renderer rendered?
  22. *
  23. * Rendering happens when either the widget or the row method was called.
  24. * Row implicitly includes widget, however certain rendering mechanisms
  25. * have to skip widget rendering when a row is rendered.
  26. *
  27. * @var Boolean
  28. */
  29. private $rendered = false;
  30. private $parent;
  31. private $children = array();
  32. public function __construct(FormThemeFactoryInterface $themeFactory)
  33. {
  34. $this->themeFactory = $themeFactory;
  35. }
  36. public function setParent(self $parent)
  37. {
  38. $this->parent = $parent;
  39. }
  40. public function setChildren(array $children)
  41. {
  42. $this->children = $children;
  43. }
  44. public function setTemplate($template)
  45. {
  46. $this->setTheme($this->themeFactory->create($template));
  47. }
  48. public function setTheme(FormThemeInterface $theme)
  49. {
  50. $this->theme = $theme;
  51. }
  52. public function getTheme()
  53. {
  54. if (!$this->theme && $this->parent) {
  55. return $this->parent->getTheme();
  56. }
  57. return $this->theme;
  58. }
  59. public function setBlock($block)
  60. {
  61. array_unshift($this->blockHistory, $block);
  62. }
  63. public function getBlock()
  64. {
  65. reset($this->blockHistory);
  66. return current($this->block);
  67. }
  68. public function setVar($name, $value)
  69. {
  70. $this->vars[$name] = $value;
  71. }
  72. public function setAttribute($name, $value)
  73. {
  74. $this->vars['attr'][$name] = $value;
  75. }
  76. public function hasVar($name)
  77. {
  78. return array_key_exists($name, $this->vars);
  79. }
  80. public function getVar($name)
  81. {
  82. if (!isset($this->vars[$name])) {
  83. return null;
  84. }
  85. return $this->vars[$name];
  86. }
  87. public function getVars()
  88. {
  89. return $this->vars;
  90. }
  91. public function isRendered()
  92. {
  93. return $this->rendered;
  94. }
  95. public function getWidget(array $vars = array())
  96. {
  97. $this->rendered = true;
  98. return $this->render('widget', $vars);
  99. }
  100. public function getErrors(array $vars = array())
  101. {
  102. return $this->render('errors', $vars);
  103. }
  104. public function getRow(array $vars = array())
  105. {
  106. $this->rendered = true;
  107. return $this->render('row', $vars);
  108. }
  109. public function getRest(array $vars = array())
  110. {
  111. return $this->render('rest', $vars);
  112. }
  113. /**
  114. * Renders the label of the given form
  115. *
  116. * @param FormInterface $form The form to render the label for
  117. * @param array $params Additional variables passed to the block
  118. */
  119. public function getLabel($label = null, array $vars = array())
  120. {
  121. if (null !== $label) {
  122. $vars['label'] = $label;
  123. }
  124. return $this->render('label', $vars);
  125. }
  126. public function getEnctype()
  127. {
  128. return $this->render('enctype', $this->vars);
  129. }
  130. protected function render($part, array $vars = array())
  131. {
  132. return $this->getTheme()->render($this->blockHistory, $part, array_replace(
  133. $this->vars,
  134. $vars
  135. ));
  136. }
  137. public function getParent()
  138. {
  139. return $this->parent;
  140. }
  141. public function hasParent()
  142. {
  143. return null !== $this->parent;
  144. }
  145. public function getChildren()
  146. {
  147. return $this->children;
  148. }
  149. public function hasChildren()
  150. {
  151. return count($this->children) > 0;
  152. }
  153. public function offsetGet($name)
  154. {
  155. return $this->children[$name];
  156. }
  157. public function offsetExists($name)
  158. {
  159. return isset($this->children[$name]);
  160. }
  161. public function offsetSet($name, $value)
  162. {
  163. throw new \BadMethodCallException('Not supported');
  164. }
  165. public function offsetUnset($name)
  166. {
  167. throw new \BadMethodCallException('Not supported');
  168. }
  169. public function getIterator()
  170. {
  171. if (isset($this->children)) {
  172. $this->rendered = true;
  173. return new \ArrayIterator($this->children);
  174. }
  175. return new \ArrayIterator(array());
  176. }
  177. }