2011-02-18 19:17:44 +00:00
|
|
|
<?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;
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
use Symfony\Component\Form\FormInterface;
|
2011-02-18 19:17:44 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Theme\ThemeInterface;
|
2011-03-17 10:18:50 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Plugin\RendererPluginInterface;
|
2011-02-18 19:17:44 +00:00
|
|
|
|
2011-03-02 21:56:20 +00:00
|
|
|
class DefaultRenderer implements RendererInterface, \ArrayAccess
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
|
|
|
private $field;
|
|
|
|
|
|
|
|
private $template;
|
|
|
|
|
|
|
|
private $theme;
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
private $vars = array();
|
2011-02-18 19:17:44 +00:00
|
|
|
|
2011-02-22 00:23:58 +00:00
|
|
|
private $changes = array();
|
2011-02-18 19:17:44 +00:00
|
|
|
|
|
|
|
private $initialized = false;
|
|
|
|
|
2011-03-19 13:31:25 +00:00
|
|
|
/**
|
|
|
|
* Is the field 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 bool
|
|
|
|
*/
|
2011-02-28 16:01:11 +00:00
|
|
|
private $rendered = false;
|
|
|
|
|
2011-03-02 21:56:20 +00:00
|
|
|
private $children = array();
|
|
|
|
|
2011-03-18 11:50:26 +00:00
|
|
|
public function __construct(ThemeInterface $theme, $template)
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
|
|
|
$this->theme = $theme;
|
|
|
|
$this->template = $template;
|
|
|
|
}
|
|
|
|
|
2011-03-02 15:00:31 +00:00
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
foreach ($this->changes as $key => $change) {
|
|
|
|
if (is_object($change)) {
|
|
|
|
$this->changes[$key] = clone $change;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-22 00:23:58 +00:00
|
|
|
private function initialize()
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
|
|
|
if (!$this->initialized) {
|
|
|
|
$this->initialized = true;
|
|
|
|
|
2011-02-22 00:23:58 +00:00
|
|
|
// Make sure that plugins and set variables are applied in the
|
|
|
|
// order they were added
|
|
|
|
foreach ($this->changes as $key => $value) {
|
2011-03-17 10:18:50 +00:00
|
|
|
if ($value instanceof RendererPluginInterface) {
|
|
|
|
$value->setUp($this->field, $this);
|
2011-02-22 00:23:58 +00:00
|
|
|
} else {
|
|
|
|
$this->vars[$key] = $value;
|
|
|
|
}
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
2011-03-02 15:00:31 +00:00
|
|
|
|
|
|
|
$this->changes = array();
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
public function setField(FormInterface $field)
|
2011-03-18 11:50:26 +00:00
|
|
|
{
|
|
|
|
$this->field = $field;
|
|
|
|
}
|
|
|
|
|
2011-03-02 21:56:20 +00:00
|
|
|
public function setChildren(array $renderers)
|
|
|
|
{
|
|
|
|
$this->children = $renderers;
|
|
|
|
}
|
|
|
|
|
2011-02-18 19:17:44 +00:00
|
|
|
public function setTheme(ThemeInterface $theme)
|
|
|
|
{
|
|
|
|
$this->theme = $theme;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTheme()
|
|
|
|
{
|
|
|
|
return $this->theme;
|
|
|
|
}
|
|
|
|
|
2011-03-17 10:18:50 +00:00
|
|
|
public function addPlugin(RendererPluginInterface $plugin)
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
|
|
|
$this->initialized = false;
|
2011-02-22 00:23:58 +00:00
|
|
|
$this->changes[] = $plugin;
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
public function setVar($name, $value)
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-02-22 00:23:58 +00:00
|
|
|
if ($this->initialized) {
|
|
|
|
$this->vars[$name] = $value;
|
|
|
|
} else {
|
|
|
|
$this->changes[$name] = $value;
|
|
|
|
}
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-25 00:11:53 +00:00
|
|
|
public function hasVar($name)
|
|
|
|
{
|
|
|
|
return array_key_exists($name, $this->vars);
|
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
public function getVar($name)
|
2011-02-19 12:46:32 +00:00
|
|
|
{
|
2011-02-22 00:23:58 +00:00
|
|
|
$this->initialize();
|
2011-02-19 12:46:32 +00:00
|
|
|
|
|
|
|
// TODO exception handling
|
2011-02-21 17:16:40 +00:00
|
|
|
return $this->vars[$name];
|
2011-02-19 12:46:32 +00:00
|
|
|
}
|
|
|
|
|
2011-02-28 15:50:23 +00:00
|
|
|
public function getVars()
|
|
|
|
{
|
|
|
|
return $this->vars;
|
|
|
|
}
|
|
|
|
|
2011-02-28 16:01:11 +00:00
|
|
|
public function isRendered()
|
|
|
|
{
|
|
|
|
return $this->rendered;
|
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
public function getWidget(array $vars = array())
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-02-28 16:01:11 +00:00
|
|
|
$this->rendered = true;
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
return $this->render('widget', $vars);
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
public function getErrors(array $vars = array())
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-02-21 17:16:40 +00:00
|
|
|
return $this->render('errors', $vars);
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
public function getRow(array $vars = array())
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-03-19 13:31:25 +00:00
|
|
|
$this->rendered = true;
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
return $this->render('row', $vars);
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-28 15:50:23 +00:00
|
|
|
public function getRest(array $vars = array())
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-02-28 15:50:23 +00:00
|
|
|
return $this->render('rest', $vars);
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the label of the given field
|
|
|
|
*
|
2011-03-20 12:35:19 +00:00
|
|
|
* @param FormInterface $field The field to render the label for
|
2011-02-18 19:17:44 +00:00
|
|
|
* @param array $params Additional variables passed to the template
|
|
|
|
*/
|
2011-02-21 17:16:40 +00:00
|
|
|
public function getLabel($label = null, array $vars = array())
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
|
|
|
if (null !== $label) {
|
2011-02-21 17:16:40 +00:00
|
|
|
$vars['label'] = $label;
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
return $this->render('label', $vars);
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
2011-03-17 13:40:34 +00:00
|
|
|
public function getEnctype()
|
|
|
|
{
|
2011-03-18 16:03:12 +00:00
|
|
|
return $this->render('enctype', $this->vars);
|
2011-03-17 13:40:34 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 17:16:40 +00:00
|
|
|
protected function render($block, array $vars = array())
|
2011-02-18 19:17:44 +00:00
|
|
|
{
|
2011-02-22 00:23:58 +00:00
|
|
|
$this->initialize();
|
2011-02-18 19:17:44 +00:00
|
|
|
|
|
|
|
return $this->theme->render($this->template, $block, array_replace(
|
2011-02-21 17:16:40 +00:00
|
|
|
$this->vars,
|
|
|
|
$vars
|
2011-02-18 19:17:44 +00:00
|
|
|
));
|
|
|
|
}
|
2011-03-02 21:56:20 +00:00
|
|
|
|
|
|
|
public function offsetGet($name)
|
|
|
|
{
|
|
|
|
return $this->children[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetExists($name)
|
|
|
|
{
|
|
|
|
return isset($this->children[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetSet($name, $value)
|
|
|
|
{
|
|
|
|
throw new \BadMethodCallException('Not supported');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetUnset($name)
|
|
|
|
{
|
|
|
|
throw new \BadMethodCallException('Not supported');
|
|
|
|
}
|
2011-02-18 19:17:44 +00:00
|
|
|
}
|