DefaultRenderer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\FieldInterface;
  12. use Symfony\Component\Form\Renderer\Theme\ThemeInterface;
  13. use Symfony\Component\Form\Renderer\Plugin\RendererPluginInterface;
  14. class DefaultRenderer implements RendererInterface, \ArrayAccess
  15. {
  16. private $field;
  17. private $template;
  18. private $theme;
  19. private $vars = array();
  20. private $changes = array();
  21. private $initialized = false;
  22. private $rendered = false;
  23. private $children = array();
  24. public function __construct(FieldInterface $field, ThemeInterface $theme, $template)
  25. {
  26. $this->field = $field;
  27. $this->theme = $theme;
  28. $this->template = $template;
  29. }
  30. public function __clone()
  31. {
  32. foreach ($this->changes as $key => $change) {
  33. if (is_object($change)) {
  34. $this->changes[$key] = clone $change;
  35. }
  36. }
  37. }
  38. private function initialize()
  39. {
  40. if (!$this->initialized) {
  41. $this->initialized = true;
  42. // Make sure that plugins and set variables are applied in the
  43. // order they were added
  44. foreach ($this->changes as $key => $value) {
  45. if ($value instanceof RendererPluginInterface) {
  46. $value->setUp($this->field, $this);
  47. } else {
  48. $this->vars[$key] = $value;
  49. }
  50. }
  51. $this->changes = array();
  52. }
  53. }
  54. public function setChildren(array $renderers)
  55. {
  56. $this->children = $renderers;
  57. }
  58. public function setTheme(ThemeInterface $theme)
  59. {
  60. $this->theme = $theme;
  61. }
  62. public function getTheme()
  63. {
  64. return $this->theme;
  65. }
  66. public function addPlugin(RendererPluginInterface $plugin)
  67. {
  68. $this->initialized = false;
  69. $this->changes[] = $plugin;
  70. }
  71. public function setVar($name, $value)
  72. {
  73. if ($this->initialized) {
  74. $this->vars[$name] = $value;
  75. } else {
  76. $this->changes[$name] = $value;
  77. }
  78. }
  79. public function hasVar($name)
  80. {
  81. return array_key_exists($name, $this->vars);
  82. }
  83. public function getVar($name)
  84. {
  85. $this->initialize();
  86. // TODO exception handling
  87. return $this->vars[$name];
  88. }
  89. public function getVars()
  90. {
  91. return $this->vars;
  92. }
  93. public function isRendered()
  94. {
  95. return $this->rendered;
  96. }
  97. public function getWidget(array $vars = array())
  98. {
  99. $this->rendered = true;
  100. return $this->render('widget', $vars);
  101. }
  102. public function getErrors(array $vars = array())
  103. {
  104. return $this->render('errors', $vars);
  105. }
  106. public function getRow(array $vars = array())
  107. {
  108. return $this->render('row', $vars);
  109. }
  110. public function getRest(array $vars = array())
  111. {
  112. return $this->render('rest', $vars);
  113. }
  114. /**
  115. * Renders the label of the given field
  116. *
  117. * @param FieldInterface $field The field to render the label for
  118. * @param array $params Additional variables passed to the template
  119. */
  120. public function getLabel($label = null, array $vars = array())
  121. {
  122. if (null !== $label) {
  123. $vars['label'] = $label;
  124. }
  125. return $this->render('label', $vars);
  126. }
  127. protected function render($block, array $vars = array())
  128. {
  129. $this->initialize();
  130. return $this->theme->render($this->template, $block, array_replace(
  131. $this->vars,
  132. $vars
  133. ));
  134. }
  135. public function offsetGet($name)
  136. {
  137. return $this->children[$name];
  138. }
  139. public function offsetExists($name)
  140. {
  141. return isset($this->children[$name]);
  142. }
  143. public function offsetSet($name, $value)
  144. {
  145. throw new \BadMethodCallException('Not supported');
  146. }
  147. public function offsetUnset($name)
  148. {
  149. throw new \BadMethodCallException('Not supported');
  150. }
  151. }