DelegatingEngine.php 2.8 KB

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