Engine.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\OutputEscaper\Escaper;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\HttpFoundation\Response;
  8. /*
  9. * This file is part of the Symfony package.
  10. *
  11. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  12. *
  13. * For the full copyright and license information, please view the LICENSE
  14. * file that was distributed with this source code.
  15. */
  16. /**
  17. * This engine knows how to render Symfony templates and automatically
  18. * escapes template parameters.
  19. *
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. class Engine extends BaseEngine
  23. {
  24. protected $container;
  25. protected $escaper;
  26. protected $level;
  27. /**
  28. * Constructor.
  29. *
  30. * @param ContainerInterface $container A ContainerInterface instance
  31. * @param LoaderInterface $loader A loader instance
  32. * @param array $renderers An array of renderer instances
  33. * @param mixed $escaper The escaper to use (or false to disable escaping)
  34. */
  35. public function __construct(ContainerInterface $container, LoaderInterface $loader, array $renderers = array(), $escaper = false)
  36. {
  37. $this->level = 0;
  38. $this->container = $container;
  39. $this->escaper = $escaper;
  40. parent::__construct($loader);
  41. foreach ($this->container->findTaggedServiceIds('templating.renderer') as $id => $attributes) {
  42. if (isset($attributes[0]['alias'])) {
  43. $this->renderers[$attributes[0]['alias']] = $id;
  44. }
  45. }
  46. $this->helpers = array();
  47. foreach ($this->container->findTaggedServiceIds('templating.helper') as $id => $attributes) {
  48. if (isset($attributes[0]['alias'])) {
  49. $this->helpers[$attributes[0]['alias']] = $id;
  50. }
  51. }
  52. }
  53. public function render($name, array $parameters = array())
  54. {
  55. ++$this->level;
  56. list(, $options) = $this->splitTemplateName($name);
  57. $renderer = $options['renderer'];
  58. if (isset($this->renderers[$renderer]) && is_string($this->renderers[$renderer])) {
  59. $this->renderers[$renderer] = $this->container->get($this->renderers[$renderer]);
  60. $this->renderers[$renderer]->setEngine($this);
  61. }
  62. if ('php' === $renderer) {
  63. // escape only once
  64. if (1 === $this->level && !isset($parameters['_data'])) {
  65. $parameters = $this->escapeParameters($parameters);
  66. }
  67. }
  68. $content = parent::render($name, $parameters);
  69. --$this->level;
  70. return $content;
  71. }
  72. /**
  73. * Renders a view and returns a Response.
  74. *
  75. * @param string $view The view name
  76. * @param array $parameters An array of parameters to pass to the view
  77. * @param Response $response A Response instance
  78. *
  79. * @return Response A Response instance
  80. */
  81. public function renderResponse($view, array $parameters = array(), Response $response = null)
  82. {
  83. if (null === $response) {
  84. $response = $this->container->get('response');
  85. }
  86. $response->setContent($this->render($view, $parameters));
  87. return $response;
  88. }
  89. public function has($name)
  90. {
  91. return isset($this->helpers[$name]);
  92. }
  93. /**
  94. * @throws \InvalidArgumentException When the helper is not defined
  95. */
  96. public function get($name)
  97. {
  98. if (!isset($this->helpers[$name])) {
  99. throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
  100. }
  101. if (is_string($this->helpers[$name])) {
  102. $this->helpers[$name] = $this->container->get($this->helpers[$name]);
  103. $this->helpers[$name]->setCharset($this->charset);
  104. }
  105. return $this->helpers[$name];
  106. }
  107. protected function escapeParameters(array $parameters)
  108. {
  109. if (false !== $this->escaper) {
  110. Escaper::setCharset($this->getCharset());
  111. $parameters['_data'] = Escaper::escape($this->escaper, $parameters);
  112. foreach ($parameters['_data'] as $key => $value) {
  113. $parameters[$key] = $value;
  114. }
  115. } else {
  116. $parameters['_data'] = Escaper::escape('raw', $parameters);
  117. }
  118. return $parameters;
  119. }
  120. // parses template names following the following pattern:
  121. // bundle:section:template(.format).renderer
  122. public function splitTemplateName($name, array $defaults = array())
  123. {
  124. $parts = explode(':', $name);
  125. if (3 !== count($parts)) {
  126. throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name));
  127. }
  128. $options = array_replace(
  129. array(
  130. 'format' => '',
  131. ),
  132. $defaults,
  133. array(
  134. 'bundle' => str_replace('\\', '/', $parts[0]),
  135. 'controller' => $parts[1],
  136. )
  137. );
  138. $elements = explode('.', $parts[2]);
  139. if (3 === count($elements)) {
  140. $parts[2] = $elements[0];
  141. if ('html' !== $elements[1]) {
  142. $options['format'] = '.'.$elements[1];
  143. }
  144. $options['renderer'] = $elements[2];
  145. } elseif (2 === count($elements)) {
  146. $parts[2] = $elements[0];
  147. $options['renderer'] = $elements[1];
  148. $format = $this->container->get('request')->getRequestFormat();
  149. if (null !== $format && 'html' !== $format) {
  150. $options['format'] = '.'.$format;
  151. }
  152. } else {
  153. throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name));
  154. }
  155. return array($parts[2], $options);
  156. }
  157. }