PhpEngine.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Templating;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Templating\Loader\LoaderInterface;
  14. use Symfony\Component\Templating\PhpEngine as BasePhpEngine;
  15. use Symfony\Component\Templating\TemplateNameParserInterface;
  16. /**
  17. * This engine knows how to render Symfony templates.
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class PhpEngine extends BasePhpEngine implements EngineInterface
  22. {
  23. protected $container;
  24. public function __construct(TemplateNameParserInterface $parser, ContainerInterface $container, LoaderInterface $loader, GlobalVariables $globals = null)
  25. {
  26. $this->container = $container;
  27. parent::__construct($parser, $loader);
  28. if (null !== $globals) {
  29. $this->addGlobal('app', $globals);
  30. }
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function get($name)
  36. {
  37. if (!isset($this->helpers[$name])) {
  38. throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
  39. }
  40. if (\is_string($this->helpers[$name])) {
  41. $this->helpers[$name] = $this->container->get($this->helpers[$name]);
  42. $this->helpers[$name]->setCharset($this->charset);
  43. }
  44. return $this->helpers[$name];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function setHelpers(array $helpers)
  50. {
  51. $this->helpers = $helpers;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function renderResponse($view, array $parameters = array(), Response $response = null)
  57. {
  58. if (null === $response) {
  59. $response = new Response();
  60. }
  61. $response->setContent($this->render($view, $parameters));
  62. return $response;
  63. }
  64. }