TwigEngine.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Bridge\Twig;
  11. use Symfony\Component\Templating\EngineInterface;
  12. use Symfony\Component\Templating\StreamingEngineInterface;
  13. use Symfony\Component\Templating\TemplateNameParserInterface;
  14. use Symfony\Component\Templating\TemplateReferenceInterface;
  15. use Twig\Environment;
  16. use Twig\Error\Error;
  17. use Twig\Error\LoaderError;
  18. use Twig\Loader\ExistsLoaderInterface;
  19. use Twig\Template;
  20. /**
  21. * This engine knows how to render Twig templates.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. */
  25. class TwigEngine implements EngineInterface, StreamingEngineInterface
  26. {
  27. protected $environment;
  28. protected $parser;
  29. public function __construct(Environment $environment, TemplateNameParserInterface $parser)
  30. {
  31. $this->environment = $environment;
  32. $this->parser = $parser;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. *
  37. * It also supports Template as name parameter.
  38. *
  39. * @throws Error if something went wrong like a thrown exception while rendering the template
  40. */
  41. public function render($name, array $parameters = array())
  42. {
  43. return $this->load($name)->render($parameters);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. *
  48. * It also supports Template as name parameter.
  49. *
  50. * @throws Error if something went wrong like a thrown exception while rendering the template
  51. */
  52. public function stream($name, array $parameters = array())
  53. {
  54. $this->load($name)->display($parameters);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. *
  59. * It also supports Template as name parameter.
  60. */
  61. public function exists($name)
  62. {
  63. if ($name instanceof Template) {
  64. return true;
  65. }
  66. $loader = $this->environment->getLoader();
  67. if ($loader instanceof ExistsLoaderInterface || method_exists($loader, 'exists')) {
  68. return $loader->exists((string) $name);
  69. }
  70. try {
  71. // cast possible TemplateReferenceInterface to string because the
  72. // EngineInterface supports them but LoaderInterface does not
  73. $loader->getSourceContext((string) $name)->getCode();
  74. } catch (LoaderError $e) {
  75. return false;
  76. }
  77. return true;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. *
  82. * It also supports Template as name parameter.
  83. */
  84. public function supports($name)
  85. {
  86. if ($name instanceof Template) {
  87. return true;
  88. }
  89. $template = $this->parser->parse($name);
  90. return 'twig' === $template->get('engine');
  91. }
  92. /**
  93. * Loads the given template.
  94. *
  95. * @param string|TemplateReferenceInterface|Template $name A template name or an instance of
  96. * TemplateReferenceInterface or Template
  97. *
  98. * @return Template
  99. *
  100. * @throws \InvalidArgumentException if the template does not exist
  101. */
  102. protected function load($name)
  103. {
  104. if ($name instanceof Template) {
  105. return $name;
  106. }
  107. try {
  108. return $this->environment->loadTemplate((string) $name);
  109. } catch (LoaderError $e) {
  110. throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  111. }
  112. }
  113. }