Chain.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. foreach ($this->loaders as $loader) {
  50. try {
  51. return $loader->getSource($name);
  52. } catch (Twig_Error_Loader $e) {
  53. }
  54. }
  55. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
  56. }
  57. /**
  58. * Gets the cache key to use for the cache for a given template name.
  59. *
  60. * @param string $name The name of the template to load
  61. *
  62. * @return string The cache key
  63. */
  64. public function getCacheKey($name)
  65. {
  66. foreach ($this->loaders as $loader) {
  67. try {
  68. return $loader->getCacheKey($name);
  69. } catch (Twig_Error_Loader $e) {
  70. }
  71. }
  72. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
  73. }
  74. /**
  75. * Returns true if the template is still fresh.
  76. *
  77. * @param string $name The template name
  78. * @param timestamp $time The last modification time of the cached template
  79. */
  80. public function isFresh($name, $time)
  81. {
  82. foreach ($this->loaders as $loader) {
  83. try {
  84. return $loader->isFresh($name, $time);
  85. } catch (Twig_Error_Loader $e) {
  86. }
  87. }
  88. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
  89. }
  90. }