ChainLoader.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\Loader;
  11. use Symfony\Component\Templating\Storage\Storage;
  12. use Symfony\Component\Templating\TemplateReferenceInterface;
  13. /**
  14. * ChainLoader is a loader that calls other loaders to load templates.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class ChainLoader extends Loader
  19. {
  20. protected $loaders = array();
  21. /**
  22. * Constructor.
  23. *
  24. * @param LoaderInterface[] $loaders An array of loader instances
  25. */
  26. public function __construct(array $loaders = array())
  27. {
  28. foreach ($loaders as $loader) {
  29. $this->addLoader($loader);
  30. }
  31. }
  32. /**
  33. * Adds a loader instance.
  34. *
  35. * @param LoaderInterface $loader A Loader instance
  36. */
  37. public function addLoader(LoaderInterface $loader)
  38. {
  39. $this->loaders[] = $loader;
  40. }
  41. /**
  42. * Loads a template.
  43. *
  44. * @param TemplateReferenceInterface $template A template
  45. *
  46. * @return Storage|bool false if the template cannot be loaded, a Storage instance otherwise
  47. */
  48. public function load(TemplateReferenceInterface $template)
  49. {
  50. foreach ($this->loaders as $loader) {
  51. if (false !== $storage = $loader->load($template)) {
  52. return $storage;
  53. }
  54. }
  55. return false;
  56. }
  57. /**
  58. * Returns true if the template is still fresh.
  59. *
  60. * @param TemplateReferenceInterface $template A template
  61. * @param int $time The last modification time of the cached template (timestamp)
  62. *
  63. * @return bool
  64. */
  65. public function isFresh(TemplateReferenceInterface $template, $time)
  66. {
  67. foreach ($this->loaders as $loader) {
  68. return $loader->isFresh($template, $time);
  69. }
  70. return false;
  71. }
  72. }