DefaultRenderer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 $parameters = array();
  20. private $plugins = array();
  21. private $initialized = false;
  22. public function __construct(ThemeInterface $theme, $template)
  23. {
  24. $this->theme = $theme;
  25. $this->template = $template;
  26. }
  27. private function setUpPlugins()
  28. {
  29. if (!$this->initialized) {
  30. $this->initialized = true;
  31. foreach ($this->plugins as $plugin) {
  32. $plugin->setUp($this);
  33. }
  34. }
  35. }
  36. public function setTheme(ThemeInterface $theme)
  37. {
  38. $this->theme = $theme;
  39. }
  40. public function getTheme()
  41. {
  42. return $this->theme;
  43. }
  44. public function addPlugin(PluginInterface $plugin)
  45. {
  46. $this->initialized = false;
  47. $this->plugins[] = $plugin;
  48. }
  49. public function setParameter($name, $value)
  50. {
  51. $this->parameters[$name] = $value;
  52. }
  53. public function getParameter($name)
  54. {
  55. $this->setUpPlugins();
  56. // TODO exception handling
  57. return $this->parameters[$name];
  58. }
  59. public function getWidget(array $attributes = array(), array $parameters = array())
  60. {
  61. return $this->render('widget', $attributes, $parameters);
  62. }
  63. public function getErrors(array $attributes = array(), array $parameters = array())
  64. {
  65. return $this->render('errors', $attributes, $parameters);
  66. }
  67. public function getRow(array $attributes = array(), array $parameters = array())
  68. {
  69. return $this->render('row', $attributes, $parameters);
  70. }
  71. public function getHidden(array $attributes = array(), array $parameters = array())
  72. {
  73. return $this->render('hidden', $attributes, $parameters);
  74. }
  75. /**
  76. * Renders the label of the given field
  77. *
  78. * @param FieldInterface $field The field to render the label for
  79. * @param array $params Additional variables passed to the template
  80. */
  81. public function getLabel($label = null, array $attributes = array(), array $parameters = array())
  82. {
  83. if (null !== $label) {
  84. $parameters['label'] = $label;
  85. }
  86. return $this->render('label', $attributes, $parameters);
  87. }
  88. protected function render($block, array $attributes = array(), array $parameters = array())
  89. {
  90. $this->setUpPlugins();
  91. return $this->theme->render($this->template, $block, array_replace(
  92. array('attr' => $attributes),
  93. $this->parameters,
  94. $parameters
  95. ));
  96. }
  97. }