DelegatingEngine.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Templating\DelegatingEngine as BaseDelegatingEngine;
  14. /**
  15. * DelegatingEngine selects an engine for a given template.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class DelegatingEngine extends BaseDelegatingEngine implements EngineInterface
  20. {
  21. protected $container;
  22. public function __construct(ContainerInterface $container, array $engineIds)
  23. {
  24. $this->container = $container;
  25. $this->engines = $engineIds;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function getEngine($name)
  31. {
  32. $this->resolveEngines();
  33. return parent::getEngine($name);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function renderResponse($view, array $parameters = array(), Response $response = null)
  39. {
  40. $engine = $this->getEngine($view);
  41. if ($engine instanceof EngineInterface) {
  42. return $engine->renderResponse($view, $parameters, $response);
  43. }
  44. if (null === $response) {
  45. $response = new Response();
  46. }
  47. $response->setContent($engine->render($view, $parameters));
  48. return $response;
  49. }
  50. /**
  51. * Resolved engine ids to their real engine instances from the container.
  52. */
  53. private function resolveEngines()
  54. {
  55. foreach ($this->engines as $i => $engine) {
  56. if (\is_string($engine)) {
  57. $this->engines[$i] = $this->container->get($engine);
  58. }
  59. }
  60. }
  61. }