Chain.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2011 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Loads templates from other loaders.
  12. *
  13. * @package twig
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_Loader_Chain implements Twig_LoaderInterface
  17. {
  18. protected $loaders;
  19. /**
  20. * Constructor.
  21. *
  22. * @param Twig_LoaderInterface[] $loaders An array of loader instances
  23. */
  24. public function __construct(array $loaders = array())
  25. {
  26. $this->loaders = array();
  27. foreach ($loaders as $loader) {
  28. $this->addLoader($loader);
  29. }
  30. }
  31. /**
  32. * Adds a loader instance.
  33. *
  34. * @param Twig_LoaderInterface $loader A Loader instance
  35. */
  36. public function addLoader(Twig_LoaderInterface $loader)
  37. {
  38. $this->loaders[] = $loader;
  39. }
  40. /**
  41. * Gets the source code of a template, given its name.
  42. *
  43. * @param string $name The name of the template to load
  44. *
  45. * @return string The template source code
  46. */
  47. public function getSource($name)
  48. {
  49. $exceptions = array();
  50. foreach ($this->loaders as $loader) {
  51. try {
  52. return $loader->getSource($name);
  53. } catch (Twig_Error_Loader $e) {
  54. $exceptions[] = $e->getMessage();
  55. }
  56. }
  57. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(', ', $exceptions)));
  58. }
  59. /**
  60. * Gets the cache key to use for the cache for a given template name.
  61. *
  62. * @param string $name The name of the template to load
  63. *
  64. * @return string The cache key
  65. */
  66. public function getCacheKey($name)
  67. {
  68. $exceptions = array();
  69. foreach ($this->loaders as $loader) {
  70. try {
  71. return $loader->getCacheKey($name);
  72. } catch (Twig_Error_Loader $e) {
  73. $exceptions[] = get_class($loader).': '.$e->getMessage();
  74. }
  75. }
  76. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
  77. }
  78. /**
  79. * Returns true if the template is still fresh.
  80. *
  81. * @param string $name The template name
  82. * @param timestamp $time The last modification time of the cached template
  83. */
  84. public function isFresh($name, $time)
  85. {
  86. $exceptions = array();
  87. foreach ($this->loaders as $loader) {
  88. try {
  89. return $loader->isFresh($name, $time);
  90. } catch (Twig_Error_Loader $e) {
  91. $exceptions[] = get_class($loader).': '.$e->getMessage();
  92. }
  93. }
  94. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
  95. }
  96. }