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
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
|
|
|
* 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;
|
2011-01-26 13:53:12 +00:00
|
|
|
use Symfony\Component\Templating\TemplateNameParserInterface;
|
2011-01-15 13:29:43 +00:00
|
|
|
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
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.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-26 13:53:12 +00:00
|
|
|
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
|
|
|
|
* @param ContainerInterface $container The DI container
|
|
|
|
* @param LoaderInterface $loader A loader instance
|
2011-04-23 16:05:44 +01:00
|
|
|
* @param GlobalVariables $globals A GlobalVariables instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-03-16 12:11:29 +00:00
|
|
|
public function __construct(TemplateNameParserInterface $parser, ContainerInterface $container, LoaderInterface $loader, GlobalVariables $globals)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->container = $container;
|
|
|
|
|
2011-01-26 13:53:12 +00:00
|
|
|
parent::__construct($parser, $loader);
|
2011-03-16 12:11:29 +00:00
|
|
|
$this->addGlobal('app', $globals);
|
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) {
|
2011-02-21 16:26:35 +00:00
|
|
|
$response = new Response();
|
2011-01-13 13:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$response->setContent($this->render($view, $parameters));
|
|
|
|
|
|
|
|
return $response;
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
}
|