2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
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.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Templating;
|
|
|
|
|
|
|
|
use Symfony\Component\Templating\PhpEngine as BasePhpEngine;
|
|
|
|
use Symfony\Component\Templating\Loader\LoaderInterface;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
2010-02-17 13:55:05 +00:00
|
|
|
/**
|
2010-11-23 09:22:48 +00:00
|
|
|
* This engine knows how to render Symfony templates.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-02-17 13:55:05 +00:00
|
|
|
*/
|
2011-01-13 13:58:17 +00:00
|
|
|
class PhpEngine extends BasePhpEngine implements EngineInterface
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $container;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-01-05 11:13:27 +00:00
|
|
|
* @param ContainerInterface $container The DI container
|
2010-05-06 12:25:53 +01:00
|
|
|
* @param LoaderInterface $loader A loader instance
|
|
|
|
*/
|
2011-01-06 19:35:08 +00:00
|
|
|
public function __construct(ContainerInterface $container, LoaderInterface $loader)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->container = $container;
|
|
|
|
|
2011-01-06 19:35:08 +00:00
|
|
|
parent::__construct($loader);
|
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
|
|
|
|
2011-01-13 13:58:17 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2011-01-05 11:13:27 +00:00
|
|
|
public function setHelpers(array $helpers)
|
|
|
|
{
|
|
|
|
$this->helpers = $helpers;
|
|
|
|
}
|
|
|
|
|
2011-01-06 13:45:12 +00:00
|
|
|
/**
|
2011-01-13 13:58:17 +00:00
|
|
|
* 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
|
2011-01-06 13:45:12 +00:00
|
|
|
*/
|
2011-01-13 13:58:17 +00:00
|
|
|
public function renderResponse($view, array $parameters = array(), Response $response = null)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2011-01-13 13:58:17 +00:00
|
|
|
if (null === $response) {
|
|
|
|
$response = $this->container->get('response');
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->setContent($this->render($view, $parameters));
|
|
|
|
|
|
|
|
return $response;
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
}
|