Engine.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 A ContainerInterface instance
  27. * @param LoaderInterface $loader A loader instance
  28. */
  29. public function __construct(ContainerInterface $container, LoaderInterface $loader)
  30. {
  31. $this->container = $container;
  32. parent::__construct($loader);
  33. foreach ($this->container->findTaggedServiceIds('templating.renderer') as $id => $attributes) {
  34. if (isset($attributes[0]['alias'])) {
  35. $this->renderers[$attributes[0]['alias']] = $this->container->get($id);
  36. $this->renderers[$attributes[0]['alias']]->setEngine($this);
  37. }
  38. }
  39. $this->helpers = array();
  40. foreach ($this->container->findTaggedServiceIds('templating.helper') as $id => $attributes) {
  41. if (isset($attributes[0]['alias'])) {
  42. $this->helpers[$attributes[0]['alias']] = $id;
  43. }
  44. }
  45. }
  46. /**
  47. * Renders a view and returns a Response.
  48. *
  49. * @param string $view The view name
  50. * @param array $parameters An array of parameters to pass to the view
  51. * @param Response $response A Response instance
  52. *
  53. * @return Response A Response instance
  54. */
  55. public function renderResponse($view, array $parameters = array(), Response $response = null)
  56. {
  57. if (null === $response) {
  58. $response = $this->container->get('response');
  59. }
  60. $response->setContent($this->render($view, $parameters));
  61. return $response;
  62. }
  63. public function has($name)
  64. {
  65. return isset($this->helpers[$name]);
  66. }
  67. /**
  68. * @throws \InvalidArgumentException When the helper is not defined
  69. */
  70. public function get($name)
  71. {
  72. if (!isset($this->helpers[$name])) {
  73. throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
  74. }
  75. if (is_string($this->helpers[$name])) {
  76. $this->helpers[$name] = $this->container->get($this->helpers[$name]);
  77. $this->helpers[$name]->setCharset($this->charset);
  78. }
  79. return $this->helpers[$name];
  80. }
  81. // parses template names following the following pattern:
  82. // bundle:section:template(.format).renderer
  83. public function splitTemplateName($name, array $defaults = array())
  84. {
  85. $parts = explode(':', $name);
  86. if (3 !== count($parts)) {
  87. throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name));
  88. }
  89. $options = array_replace(
  90. array(
  91. 'format' => '',
  92. ),
  93. $defaults,
  94. array(
  95. 'bundle' => str_replace('\\', '/', $parts[0]),
  96. 'controller' => $parts[1],
  97. )
  98. );
  99. $elements = explode('.', $parts[2]);
  100. if (3 === count($elements)) {
  101. $parts[2] = $elements[0];
  102. if ('html' !== $elements[1]) {
  103. $options['format'] = '.'.$elements[1];
  104. }
  105. $options['renderer'] = $elements[2];
  106. } elseif (2 === count($elements)) {
  107. $parts[2] = $elements[0];
  108. $options['renderer'] = $elements[1];
  109. $format = $this->container->get('request')->getRequestFormat();
  110. if (null !== $format && 'html' !== $format) {
  111. $options['format'] = '.'.$format;
  112. }
  113. } else {
  114. throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name));
  115. }
  116. return array($parts[2], $options);
  117. }
  118. }