Chain.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
  16. {
  17. private $hasSourceCache = array();
  18. protected $loaders = array();
  19. /**
  20. * Constructor.
  21. *
  22. * @param Twig_LoaderInterface[] $loaders An array of loader instances
  23. */
  24. public function __construct(array $loaders = array())
  25. {
  26. foreach ($loaders as $loader) {
  27. $this->addLoader($loader);
  28. }
  29. }
  30. /**
  31. * Adds a loader instance.
  32. *
  33. * @param Twig_LoaderInterface $loader A Loader instance
  34. */
  35. public function addLoader(Twig_LoaderInterface $loader)
  36. {
  37. $this->loaders[] = $loader;
  38. $this->hasSourceCache = array();
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getSource($name)
  44. {
  45. $exceptions = array();
  46. foreach ($this->loaders as $loader) {
  47. if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
  48. continue;
  49. }
  50. try {
  51. return $loader->getSource($name);
  52. } catch (Twig_Error_Loader $e) {
  53. $exceptions[] = $e->getMessage();
  54. }
  55. }
  56. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function exists($name)
  62. {
  63. $name = (string) $name;
  64. if (isset($this->hasSourceCache[$name])) {
  65. return $this->hasSourceCache[$name];
  66. }
  67. foreach ($this->loaders as $loader) {
  68. if ($loader instanceof Twig_ExistsLoaderInterface) {
  69. if ($loader->exists($name)) {
  70. return $this->hasSourceCache[$name] = true;
  71. }
  72. continue;
  73. }
  74. try {
  75. $loader->getSource($name);
  76. return $this->hasSourceCache[$name] = true;
  77. } catch (Twig_Error_Loader $e) {
  78. }
  79. }
  80. return $this->hasSourceCache[$name] = false;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getCacheKey($name)
  86. {
  87. $exceptions = array();
  88. foreach ($this->loaders as $loader) {
  89. if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
  90. continue;
  91. }
  92. try {
  93. return $loader->getCacheKey($name);
  94. } catch (Twig_Error_Loader $e) {
  95. $exceptions[] = get_class($loader).': '.$e->getMessage();
  96. }
  97. }
  98. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function isFresh($name, $time)
  104. {
  105. $exceptions = array();
  106. foreach ($this->loaders as $loader) {
  107. if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
  108. continue;
  109. }
  110. try {
  111. return $loader->isFresh($name, $time);
  112. } catch (Twig_Error_Loader $e) {
  113. $exceptions[] = get_class($loader).': '.$e->getMessage();
  114. }
  115. }
  116. throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
  117. }
  118. }