ChainLoader.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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;
  21. /**
  22. * Constructor.
  23. *
  24. * @param LoaderInterface[] $loaders An array of loader instances
  25. */
  26. public function __construct(array $loaders = array())
  27. {
  28. $this->loaders = array();
  29. foreach ($loaders as $loader) {
  30. $this->addLoader($loader);
  31. }
  32. }
  33. /**
  34. * Adds a loader instance.
  35. *
  36. * @param LoaderInterface $loader A Loader instance
  37. */
  38. public function addLoader(LoaderInterface $loader)
  39. {
  40. $this->loaders[] = $loader;
  41. }
  42. /**
  43. * Loads a template.
  44. *
  45. * @param TemplateReferenceInterface $template A template
  46. *
  47. * @return Storage|Boolean false if the template cannot be loaded, a Storage instance otherwise
  48. */
  49. public function load(TemplateReferenceInterface $template)
  50. {
  51. foreach ($this->loaders as $loader) {
  52. if (false !== $storage = $loader->load($template)) {
  53. return $storage;
  54. }
  55. }
  56. return false;
  57. }
  58. /**
  59. * Returns true if the template is still fresh.
  60. *
  61. * @param TemplateReferenceInterface $template A template
  62. * @param integer $time The last modification time of the cached template (timestamp)
  63. *
  64. * @return Boolean
  65. */
  66. public function isFresh(TemplateReferenceInterface $template, $time)
  67. {
  68. foreach ($this->loaders as $loader) {
  69. return $loader->isFresh($template);
  70. }
  71. return false;
  72. }
  73. }