FilesystemLoader.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\Bundle\FrameworkBundle\Templating\Loader;
  11. use Symfony\Component\Config\FileLocatorInterface;
  12. use Symfony\Component\Templating\Loader\LoaderInterface;
  13. use Symfony\Component\Templating\Storage\FileStorage;
  14. use Symfony\Component\Templating\TemplateReferenceInterface;
  15. /**
  16. * FilesystemLoader is a loader that read templates from the filesystem.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class FilesystemLoader implements LoaderInterface
  21. {
  22. protected $locator;
  23. public function __construct(FileLocatorInterface $locator)
  24. {
  25. $this->locator = $locator;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function load(TemplateReferenceInterface $template)
  31. {
  32. try {
  33. $file = $this->locator->locate($template);
  34. } catch (\InvalidArgumentException $e) {
  35. return false;
  36. }
  37. return new FileStorage($file);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function isFresh(TemplateReferenceInterface $template, $time)
  43. {
  44. if (false === $storage = $this->load($template)) {
  45. return false;
  46. }
  47. if (!is_readable((string) $storage)) {
  48. return false;
  49. }
  50. return filemtime((string) $storage) < $time;
  51. }
  52. }