This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bundle/FrameworkBundle/Templating/DelegatingEngine.php
2011-01-18 08:07:46 +01:00

100 lines
2.6 KiB
PHP

<?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\Bundle\FrameworkBundle\Templating;
use Symfony\Component\Templating\DelegatingEngine as BaseDelegatingEngine;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
/**
* DelegatingEngine selects an engine for a given template.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class DelegatingEngine extends BaseDelegatingEngine implements EngineInterface
{
protected $container;
/**
* Constructor.
*
* @param ContainerInterface $container The DI container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->engines = array();
}
public function setEngineIds($ids)
{
$this->engines = $ids;
}
/**
* {@inheritdoc}
*/
public function supports($name)
{
foreach ($this->engines as $i => $engine) {
if (is_string($engine)) {
$engine = $this->engines[$i] = $this->container->get($engine);
}
if ($engine->supports($name)) {
return true;
}
}
return false;
}
/**
* {@inheritdoc}
*/
protected function getEngine($name)
{
foreach ($this->engines as $i => $engine) {
if (is_string($engine)) {
$engine = $this->engines[$i] = $this->container->get($engine);
}
if ($engine->supports($name)) {
return $engine;
}
}
throw new \RuntimeException(sprintf('No engine is able to work with the "%s" template.', $name));
}
/**
* Renders a view and returns a Response.
*
* @param string $view The view name
* @param array $parameters An array of parameters to pass to the view
* @param Response $response A Response instance
*
* @return Response A Response instance
*/
public function renderResponse($view, array $parameters = array(), Response $response = null)
{
if (null === $response) {
$response = $this->container->get('response');
}
$response->setContent($this->render($view, $parameters));
return $response;
}
}