2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
2010-07-09 09:05:26 +01:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Templating;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
|
|
|
use Symfony\Components\Templating\Engine as BaseEngine;
|
|
|
|
use Symfony\Components\Templating\Loader\LoaderInterface;
|
|
|
|
use Symfony\Components\OutputEscaper\Escaper;
|
|
|
|
use Symfony\Components\DependencyInjection\ContainerInterface;
|
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
|
|
|
* (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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This engine knows how to render Symfony templates and automatically
|
|
|
|
* escapes template parameters.
|
|
|
|
*
|
2010-04-07 01:51:29 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-02-17 13:55:05 +00:00
|
|
|
*/
|
|
|
|
class Engine extends BaseEngine
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $container;
|
|
|
|
protected $escaper;
|
|
|
|
protected $level;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container A ContainerInterface instance
|
|
|
|
* @param LoaderInterface $loader A loader instance
|
|
|
|
* @param array $renderers An array of renderer instances
|
|
|
|
* @param mixed $escaper The escaper to use (or false to disable escaping)
|
|
|
|
*/
|
|
|
|
public function __construct(ContainerInterface $container, LoaderInterface $loader, array $renderers = array(), $escaper)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->level = 0;
|
|
|
|
$this->container = $container;
|
|
|
|
$this->escaper = $escaper;
|
|
|
|
|
2010-05-20 12:51:47 +01:00
|
|
|
foreach ($this->container->findAnnotatedServiceIds('templating.renderer') as $id => $attributes) {
|
|
|
|
if (isset($attributes[0]['alias'])) {
|
2010-06-27 17:28:29 +01:00
|
|
|
$renderers[$attributes[0]['alias']] = $this->container->get($id);
|
2010-05-20 12:51:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($loader, $renderers);
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->helpers = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->container->findAnnotatedServiceIds('templating.helper') as $id => $attributes) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (isset($attributes[0]['alias'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->helpers[$attributes[0]['alias']] = $id;
|
|
|
|
}
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function render($name, array $parameters = array())
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
++$this->level;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-20 16:22:24 +01:00
|
|
|
list(, $options) = $this->splitTemplateName($name);
|
|
|
|
if ('php' === $options['renderer'])
|
|
|
|
{
|
|
|
|
// escape only once
|
|
|
|
if (1 === $this->level && !isset($parameters['_data'])) {
|
|
|
|
$parameters = $this->escapeParameters($parameters);
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$content = parent::render($name, $parameters);
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
--$this->level;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $content;
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function has($name)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return isset($this->helpers[$name]);
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException When the helper is not defined
|
|
|
|
*/
|
|
|
|
public function get($name)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!isset($this->helpers[$name])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (is_string($this->helpers[$name])) {
|
2010-07-09 13:58:03 +01:00
|
|
|
$this->helpers[$name] = $this->container->get($this->helpers[$name]);
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->helpers[$name]->setCharset($this->charset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->helpers[$name];
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
protected function escapeParameters(array $parameters)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (false !== $this->escaper) {
|
2010-05-06 12:25:53 +01:00
|
|
|
Escaper::setCharset($this->getCharset());
|
|
|
|
|
|
|
|
$parameters['_data'] = Escaper::escape($this->escaper, $parameters);
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($parameters['_data'] as $key => $value) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$parameters[$key] = $value;
|
|
|
|
}
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$parameters['_data'] = Escaper::escape('raw', $parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parameters;
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// Bundle:controller:action(:renderer)
|
2010-05-27 08:25:16 +01:00
|
|
|
public function splitTemplateName($name, array $defaults = array())
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$parts = explode(':', $name, 4);
|
|
|
|
|
2010-06-20 19:51:08 +01:00
|
|
|
if (sizeof($parts) < 3) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name));
|
|
|
|
}
|
|
|
|
|
2010-05-27 08:25:16 +01:00
|
|
|
$options = array_replace(
|
|
|
|
array(
|
|
|
|
'renderer' => 'php',
|
|
|
|
'format' => '',
|
|
|
|
),
|
|
|
|
$defaults,
|
|
|
|
array(
|
|
|
|
'bundle' => str_replace('\\', '/', $parts[0]),
|
|
|
|
'controller' => $parts[1],
|
|
|
|
)
|
2010-05-06 12:25:53 +01:00
|
|
|
);
|
|
|
|
|
2010-05-27 08:25:16 +01:00
|
|
|
if (isset($parts[3]) && $parts[3]) {
|
|
|
|
$options['renderer'] = $parts[3];
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$format = $this->container->getRequestService()->getRequestFormat();
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null !== $format && 'html' !== $format) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$options['format'] = '.'.$format;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($parts[2], $options);
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
}
|