Engine.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. foreach ($this->container->findTaggedServiceIds('templating.renderer') as $id => $attributes) {
  41. if (isset($attributes[0]['alias'])) {
  42. $renderers[$attributes[0]['alias']] = $this->container->get($id);
  43. }
  44. }
  45. parent::__construct($loader, $renderers);
  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. if ('php' === $options['renderer']) {
  58. // escape only once
  59. if (1 === $this->level && !isset($parameters['_data'])) {
  60. $parameters = $this->escapeParameters($parameters);
  61. }
  62. }
  63. $content = parent::render($name, $parameters);
  64. --$this->level;
  65. return $content;
  66. }
  67. /**
  68. * Renders a view and returns a Response.
  69. *
  70. * @param string $view The view name
  71. * @param array $parameters An array of parameters to pass to the view
  72. * @param Response $response A Response instance
  73. *
  74. * @return Response A Response instance
  75. */
  76. public function renderResponse($view, array $parameters = array(), Response $response = null)
  77. {
  78. if (null === $response) {
  79. $response = $this->container->get('response');
  80. }
  81. $response->setContent($this->render($view, $parameters));
  82. return $response;
  83. }
  84. public function has($name)
  85. {
  86. return isset($this->helpers[$name]);
  87. }
  88. /**
  89. * @throws \InvalidArgumentException When the helper is not defined
  90. */
  91. public function get($name)
  92. {
  93. if (!isset($this->helpers[$name])) {
  94. throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
  95. }
  96. if (is_string($this->helpers[$name])) {
  97. $this->helpers[$name] = $this->container->get($this->helpers[$name]);
  98. $this->helpers[$name]->setCharset($this->charset);
  99. }
  100. return $this->helpers[$name];
  101. }
  102. protected function escapeParameters(array $parameters)
  103. {
  104. if (false !== $this->escaper) {
  105. Escaper::setCharset($this->getCharset());
  106. $parameters['_data'] = Escaper::escape($this->escaper, $parameters);
  107. foreach ($parameters['_data'] as $key => $value) {
  108. $parameters[$key] = $value;
  109. }
  110. } else {
  111. $parameters['_data'] = Escaper::escape('raw', $parameters);
  112. }
  113. return $parameters;
  114. }
  115. // parses template names following the following pattern:
  116. // bundle:controller:action(.format)(.renderer)
  117. public function splitTemplateName($name, array $defaults = array())
  118. {
  119. $parts = explode(':', $name);
  120. if (3 !== count($parts)) {
  121. throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name));
  122. }
  123. $options = array_replace(
  124. array(
  125. 'format' => '',
  126. ),
  127. $defaults,
  128. array(
  129. 'bundle' => str_replace('\\', '/', $parts[0]),
  130. 'controller' => $parts[1],
  131. )
  132. );
  133. $elements = explode('.', $parts[2]);
  134. if (3 === count($elements)) {
  135. $parts[2] = $elements[0];
  136. $options['format'] = $elements[1];
  137. $options['renderer'] = $elements[2];
  138. } elseif (2 === count($elements)) {
  139. $parts[2] = $elements[0];
  140. $options['renderer'] = $elements[1];
  141. $format = $this->container->get('request')->getRequestFormat();
  142. if (null !== $format && 'html' !== $format) {
  143. $options['format'] = '.'.$format;
  144. }
  145. } else {
  146. throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name));
  147. }
  148. return array($parts[2], $options);
  149. }
  150. }