DelegatingEngine.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Templating\DelegatingEngine as BaseDelegatingEngine;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  14. use Symfony\Component\HttpFoundation\Response;
  15. /**
  16. * DelegatingEngine selects an engine for a given template.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class DelegatingEngine extends BaseDelegatingEngine implements EngineInterface
  21. {
  22. protected $container;
  23. /**
  24. * Constructor.
  25. *
  26. * @param ContainerInterface $container The DI container
  27. */
  28. public function __construct(ContainerInterface $container, array $engineIds)
  29. {
  30. $this->container = $container;
  31. $this->engines = $engineIds;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function supports($name)
  37. {
  38. foreach ($this->engines as $i => $engine) {
  39. if (is_string($engine)) {
  40. $engine = $this->engines[$i] = $this->container->get($engine);
  41. }
  42. if ($engine->supports($name)) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. protected function getEngine($name)
  52. {
  53. foreach ($this->engines as $i => $engine) {
  54. if (is_string($engine)) {
  55. $engine = $this->engines[$i] = $this->container->get($engine);
  56. }
  57. if ($engine->supports($name)) {
  58. return $engine;
  59. }
  60. }
  61. throw new \RuntimeException(sprintf('No engine is able to work with the template "%s".', $name));
  62. }
  63. /**
  64. * Renders a view and returns a Response.
  65. *
  66. * @param string $view The view name
  67. * @param array $parameters An array of parameters to pass to the view
  68. * @param Response $response A Response instance
  69. *
  70. * @return Response A Response instance
  71. */
  72. public function renderResponse($view, array $parameters = array(), Response $response = null)
  73. {
  74. if (null === $response) {
  75. $response = new Response();
  76. }
  77. $response->setContent($this->render($view, $parameters));
  78. return $response;
  79. }
  80. }