DelegatingEngine.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Component\Templating;
  11. /**
  12. * DelegatingEngine selects an engine for a given template.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class DelegatingEngine implements EngineInterface, StreamingEngineInterface
  17. {
  18. /**
  19. * @var EngineInterface[]
  20. */
  21. protected $engines = array();
  22. /**
  23. * Constructor.
  24. *
  25. * @param EngineInterface[] $engines An array of EngineInterface instances to add
  26. */
  27. public function __construct(array $engines = array())
  28. {
  29. foreach ($engines as $engine) {
  30. $this->addEngine($engine);
  31. }
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function render($name, array $parameters = array())
  37. {
  38. return $this->getEngine($name)->render($name, $parameters);
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function stream($name, array $parameters = array())
  44. {
  45. $engine = $this->getEngine($name);
  46. if (!$engine instanceof StreamingEngineInterface) {
  47. throw new \LogicException(sprintf('Template "%s" cannot be streamed as the engine supporting it does not implement StreamingEngineInterface.', $name));
  48. }
  49. $engine->stream($name, $parameters);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function exists($name)
  55. {
  56. return $this->getEngine($name)->exists($name);
  57. }
  58. /**
  59. * Adds an engine.
  60. *
  61. * @param EngineInterface $engine An EngineInterface instance
  62. */
  63. public function addEngine(EngineInterface $engine)
  64. {
  65. $this->engines[] = $engine;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function supports($name)
  71. {
  72. try {
  73. $this->getEngine($name);
  74. } catch (\RuntimeException $e) {
  75. return false;
  76. }
  77. return true;
  78. }
  79. /**
  80. * Get an engine able to render the given template.
  81. *
  82. * @param string|TemplateReferenceInterface $name A template name or a TemplateReferenceInterface instance
  83. *
  84. * @return EngineInterface The engine
  85. *
  86. * @throws \RuntimeException if no engine able to work with the template is found
  87. */
  88. public function getEngine($name)
  89. {
  90. foreach ($this->engines as $engine) {
  91. if ($engine->supports($name)) {
  92. return $engine;
  93. }
  94. }
  95. throw new \RuntimeException(sprintf('No engine is able to work with the template "%s".', $name));
  96. }
  97. }