DefaultRenderer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\PluginInterface;
  14. class DefaultRenderer implements RendererInterface
  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. public function __construct(ThemeInterface $theme, $template)
  24. {
  25. $this->theme = $theme;
  26. $this->template = $template;
  27. }
  28. private function initialize()
  29. {
  30. if (!$this->initialized) {
  31. $this->initialized = true;
  32. // Make sure that plugins and set variables are applied in the
  33. // order they were added
  34. foreach ($this->changes as $key => $value) {
  35. if ($value instanceof PluginInterface) {
  36. $value->setUp($this);
  37. } else {
  38. $this->vars[$key] = $value;
  39. }
  40. }
  41. }
  42. }
  43. public function setTheme(ThemeInterface $theme)
  44. {
  45. $this->theme = $theme;
  46. }
  47. public function getTheme()
  48. {
  49. return $this->theme;
  50. }
  51. public function addPlugin(PluginInterface $plugin)
  52. {
  53. $this->initialized = false;
  54. $this->changes[] = $plugin;
  55. }
  56. public function setVar($name, $value)
  57. {
  58. if ($this->initialized) {
  59. $this->vars[$name] = $value;
  60. } else {
  61. $this->changes[$name] = $value;
  62. }
  63. }
  64. public function hasVar($name)
  65. {
  66. return array_key_exists($name, $this->vars);
  67. }
  68. public function getVar($name)
  69. {
  70. $this->initialize();
  71. // TODO exception handling
  72. return $this->vars[$name];
  73. }
  74. public function getVars()
  75. {
  76. return $this->vars;
  77. }
  78. public function isRendered()
  79. {
  80. return $this->rendered;
  81. }
  82. public function getWidget(array $vars = array())
  83. {
  84. $this->rendered = true;
  85. return $this->render('widget', $vars);
  86. }
  87. public function getErrors(array $vars = array())
  88. {
  89. return $this->render('errors', $vars);
  90. }
  91. public function getRow(array $vars = array())
  92. {
  93. return $this->render('row', $vars);
  94. }
  95. public function getRest(array $vars = array())
  96. {
  97. return $this->render('rest', $vars);
  98. }
  99. /**
  100. * Renders the label of the given field
  101. *
  102. * @param FieldInterface $field The field to render the label for
  103. * @param array $params Additional variables passed to the template
  104. */
  105. public function getLabel($label = null, array $vars = array())
  106. {
  107. if (null !== $label) {
  108. $vars['label'] = $label;
  109. }
  110. return $this->render('label', $vars);
  111. }
  112. protected function render($block, array $vars = array())
  113. {
  114. $this->initialize();
  115. return $this->theme->render($this->template, $block, array_replace(
  116. $this->vars,
  117. $vars
  118. ));
  119. }
  120. }