FilesystemLoader.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\TwigBundle\Loader;
  11. use Symfony\Component\Templating\TemplateNameParserInterface;
  12. use Symfony\Component\Config\FileLocatorInterface;
  13. use Symfony\Component\Templating\TemplateReferenceInterface;
  14. /**
  15. * FilesystemLoader extends the default Twig filesystem loader
  16. * to work with the Symfony2 paths.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class FilesystemLoader implements \Twig_LoaderInterface
  21. {
  22. protected $locator;
  23. protected $parser;
  24. protected $cache;
  25. /**
  26. * Constructor.
  27. *
  28. * @param FileLocatorInterface $locator A FileLocatorInterface instance
  29. */
  30. public function __construct(FileLocatorInterface $locator, TemplateNameParserInterface $parser)
  31. {
  32. $this->locator = $locator;
  33. $this->parser = $parser;
  34. $this->cache = array();
  35. }
  36. /**
  37. * Gets the source code of a template, given its name.
  38. *
  39. * @param mixed $name The template name or a TemplateReferenceInterface instance
  40. *
  41. * @return string The template source code
  42. */
  43. public function getSource($name)
  44. {
  45. return file_get_contents($this->findTemplate($name));
  46. }
  47. /**
  48. * Gets the cache key to use for the cache for a given template name.
  49. *
  50. * @param mixed $name The template name or a TemplateReferenceInterface instance
  51. *
  52. * @return string The cache key
  53. */
  54. public function getCacheKey($name)
  55. {
  56. return $this->findTemplate($name);
  57. }
  58. /**
  59. * Returns true if the template is still fresh.
  60. *
  61. * @param mixed $name The template name or a TemplateReferenceInterface instance
  62. * @param timestamp $time The last modification time of the cached template
  63. *
  64. * @throws \Twig_Error_Loader if the template does not exist
  65. */
  66. public function isFresh($name, $time)
  67. {
  68. return filemtime($this->findTemplate($name)) < $time;
  69. }
  70. /**
  71. * Returns the path to the template file
  72. *
  73. * @param $name The template logical name
  74. *
  75. * @return string The path to the template file
  76. */
  77. protected function findTemplate($name)
  78. {
  79. $tpl = $this->parser->parse($name);
  80. if (isset($this->cache[$key = $tpl->getSignature()])) {
  81. return $this->cache[$key];
  82. }
  83. $file = null;
  84. $previous = null;
  85. try {
  86. $file = $this->locator->locate($tpl);
  87. } catch (\InvalidArgumentException $e) {
  88. $previous = $e;
  89. }
  90. if (false === $file || null === $file) {
  91. throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $tpl->getLogicalName()), -1, null, $previous);
  92. }
  93. return $this->cache[$key] = $file;
  94. }
  95. }