123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Form\Renderer;
- use Symfony\Component\Form\FormInterface;
- use Symfony\Component\Form\Renderer\Theme\FormThemeInterface;
- use Symfony\Component\Form\Renderer\Theme\FormThemeFactoryInterface;
- use Symfony\Component\Form\Renderer\Plugin\FormRendererPluginInterface;
- class ThemeRenderer implements FormRendererInterface, \ArrayAccess, \IteratorAggregate
- {
- private $form;
- private $block;
- private $themeFactory;
- private $theme;
- private $vars = array();
- private $changes = array();
- private $initialized = false;
- /**
- * Is the form attached to this renderer rendered?
- *
- * Rendering happens when either the widget or the row method was called.
- * Row implicitly includes widget, however certain rendering mechanisms
- * have to skip widget rendering when a row is rendered.
- *
- * @var Boolean
- */
- private $rendered = false;
- public function __construct(FormThemeFactoryInterface $themeFactory, $template = null)
- {
- $this->themeFactory = $themeFactory;
- $this->setTemplate($template);
- }
- public function __clone()
- {
- foreach ($this->changes as $key => $change) {
- if (is_object($change)) {
- $this->changes[$key] = clone $change;
- }
- }
- }
- private function initialize()
- {
- if (!$this->initialized) {
- $this->initialized = true;
- // Make sure that plugins and set variables are applied in the
- // order they were added
- foreach ($this->changes as $key => $value) {
- if ($value instanceof FormRendererPluginInterface) {
- $value->setUp($this->form, $this);
- } else {
- $this->vars[$key] = $value;
- }
- }
- $this->changes = array();
- }
- }
- public function setForm(FormInterface $form)
- {
- $this->form = $form;
- }
- public function setTemplate($template)
- {
- $this->setTheme($this->themeFactory->create($template));
- }
- public function setTheme(FormThemeInterface $theme)
- {
- $this->theme = $theme;
- }
- public function getTheme()
- {
- return $this->theme;
- }
- public function setBlock($block)
- {
- $this->block = $block;
- }
- public function getBlock()
- {
- return $this->block;
- }
- public function addPlugin(FormRendererPluginInterface $plugin)
- {
- $this->initialized = false;
- $this->changes[] = $plugin;
- }
- public function setVar($name, $value)
- {
- if ($this->initialized) {
- $this->vars[$name] = $value;
- } else {
- $this->changes[$name] = $value;
- }
- }
- public function setAttribute($name, $value)
- {
- // handling through $this->changes not necessary
- $this->vars['attr'][$name] = $value;
- }
- public function hasVar($name)
- {
- return array_key_exists($name, $this->vars);
- }
- public function getVar($name)
- {
- $this->initialize();
- // TODO exception handling
- if (isset($this->vars[$name])) {
- return $this->vars[$name];
- }
- return null;
- }
- public function getVars()
- {
- $this->initialize();
- return $this->vars;
- }
- public function isRendered()
- {
- return $this->rendered;
- }
- public function getWidget(array $vars = array())
- {
- $this->rendered = true;
- return $this->render('widget', $vars);
- }
- public function getErrors(array $vars = array())
- {
- return $this->render('errors', $vars);
- }
- public function getRow(array $vars = array())
- {
- $this->rendered = true;
- return $this->render('row', $vars);
- }
- public function getRest(array $vars = array())
- {
- return $this->render('rest', $vars);
- }
- /**
- * Renders the label of the given form
- *
- * @param FormInterface $form The form to render the label for
- * @param array $params Additional variables passed to the block
- */
- public function getLabel($label = null, array $vars = array())
- {
- if (null !== $label) {
- $vars['label'] = $label;
- }
- return $this->render('label', $vars);
- }
- public function getEnctype()
- {
- return $this->render('enctype', $this->vars);
- }
- protected function render($part, array $vars = array())
- {
- $this->initialize();
- return $this->theme->render($this->block, $part, array_replace(
- $this->vars,
- $vars
- ));
- }
- public function offsetGet($name)
- {
- $this->initialize();
- return $this->vars['fields'][$name];
- }
- public function offsetExists($name)
- {
- $this->initialize();
- return isset($this->vars['fields'][$name]);
- }
- public function offsetSet($name, $value)
- {
- throw new \BadMethodCallException('Not supported');
- }
- public function offsetUnset($name)
- {
- throw new \BadMethodCallException('Not supported');
- }
- public function getIterator()
- {
- $this->initialize();
- if (isset($this->vars['fields'])) {
- $this->rendered = true;
- return new \ArrayIterator($this->vars['fields']);
- }
- return new \ArrayIterator(array());
- }
- }
|