FilesystemLoader.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\TwigBundle\Loader;
  11. use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;
  12. /**
  13. * FilesystemLoader extends the default Twig filesystem loader
  14. * to work with the Symfony2 paths.
  15. *
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. */
  18. class FilesystemLoader implements \Twig_LoaderInterface
  19. {
  20. /**
  21. * Constructor.
  22. *
  23. * @param TemplateLocator $locator A TemplateLocator instance
  24. */
  25. public function __construct(TemplateLocator $locator)
  26. {
  27. $this->locator = $locator;
  28. }
  29. /**
  30. * Gets the source code of a template, given its name.
  31. *
  32. * @param string $name The name of the template to load
  33. *
  34. * @return string The template source code
  35. */
  36. public function getSource($name)
  37. {
  38. return file_get_contents($this->findTemplate($name));
  39. }
  40. /**
  41. * Gets the cache key to use for the cache for a given template name.
  42. *
  43. * @param string $name The name of the template to load
  44. *
  45. * @return string The cache key
  46. */
  47. public function getCacheKey($name)
  48. {
  49. return $this->findTemplate($name);
  50. }
  51. /**
  52. * Returns true if the template is still fresh.
  53. *
  54. * @param string $name The template name
  55. * @param timestamp $time The last modification time of the cached template
  56. */
  57. public function isFresh($name, $time)
  58. {
  59. return filemtime($this->findTemplate($name)) < $time;
  60. }
  61. protected function findTemplate($name)
  62. {
  63. if (false === $file = $this->locator->locate($name)) {
  64. throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $name));
  65. }
  66. return $file;
  67. }
  68. }