Engine.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle\Templating;
  3. use Symfony\Component\Templating\Engine as BaseEngine;
  4. use Symfony\Component\Templating\Loader\LoaderInterface;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. /*
  8. * This file is part of the Symfony package.
  9. *
  10. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  11. *
  12. * For the full copyright and license information, please view the LICENSE
  13. * file that was distributed with this source code.
  14. */
  15. /**
  16. * This engine knows how to render Symfony templates.
  17. *
  18. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  19. */
  20. class Engine extends BaseEngine
  21. {
  22. protected $container;
  23. /**
  24. * Constructor.
  25. *
  26. * @param ContainerInterface $container The DI container
  27. * @param LoaderInterface $loader A loader instance
  28. * @param array $renderers All templating renderers
  29. */
  30. public function __construct(ContainerInterface $container, LoaderInterface $loader, array $renderers)
  31. {
  32. $this->container = $container;
  33. parent::__construct($loader, $renderers);
  34. }
  35. public function getContainer()
  36. {
  37. return $this->container;
  38. }
  39. /**
  40. * Renders a view and returns a Response.
  41. *
  42. * @param string $view The view name
  43. * @param array $parameters An array of parameters to pass to the view
  44. * @param Response $response A Response instance
  45. *
  46. * @return Response A Response instance
  47. */
  48. public function renderResponse($view, array $parameters = array(), Response $response = null)
  49. {
  50. if (null === $response) {
  51. $response = $this->container->get('response');
  52. }
  53. $response->setContent($this->render($view, $parameters));
  54. return $response;
  55. }
  56. public function has($name)
  57. {
  58. return isset($this->helpers[$name]);
  59. }
  60. /**
  61. * @throws \InvalidArgumentException When the helper is not defined
  62. */
  63. public function get($name)
  64. {
  65. if (!isset($this->helpers[$name])) {
  66. throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
  67. }
  68. if (is_string($this->helpers[$name])) {
  69. $this->helpers[$name] = $this->container->get($this->helpers[$name]);
  70. $this->helpers[$name]->setCharset($this->charset);
  71. }
  72. return $this->helpers[$name];
  73. }
  74. public function setHelpers(array $helpers)
  75. {
  76. $this->helpers = $helpers;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function splitTemplateName($name, array $defaults = array())
  82. {
  83. return $this->container->get('templating.name_converter')->fromShortNotation($name, $defaults);
  84. }
  85. }