FilesystemLoader.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Storage\FileStorage;
  13. use Symfony\Component\Templating\TemplateReferenceInterface;
  14. /**
  15. * FilesystemLoader is a loader that read templates from the filesystem.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class FilesystemLoader extends Loader
  20. {
  21. protected $templatePathPatterns;
  22. /**
  23. * Constructor.
  24. *
  25. * @param array $templatePathPatterns An array of path patterns to look for templates
  26. */
  27. public function __construct($templatePathPatterns)
  28. {
  29. $this->templatePathPatterns = (array) $templatePathPatterns;
  30. }
  31. /**
  32. * Loads a template.
  33. *
  34. * @param TemplateReferenceInterface $template A template
  35. *
  36. * @return Storage|bool false if the template cannot be loaded, a Storage instance otherwise
  37. */
  38. public function load(TemplateReferenceInterface $template)
  39. {
  40. $file = $template->get('name');
  41. if (self::isAbsolutePath($file) && is_file($file)) {
  42. return new FileStorage($file);
  43. }
  44. $replacements = array();
  45. foreach ($template->all() as $key => $value) {
  46. $replacements['%'.$key.'%'] = $value;
  47. }
  48. $fileFailures = array();
  49. foreach ($this->templatePathPatterns as $templatePathPattern) {
  50. if (is_file($file = strtr($templatePathPattern, $replacements)) && is_readable($file)) {
  51. if (null !== $this->logger) {
  52. $this->logger->debug('Loaded template file.', array('file' => $file));
  53. }
  54. return new FileStorage($file);
  55. }
  56. if (null !== $this->logger) {
  57. $fileFailures[] = $file;
  58. }
  59. }
  60. // only log failures if no template could be loaded at all
  61. foreach ($fileFailures as $file) {
  62. if (null !== $this->logger) {
  63. $this->logger->debug('Failed loading template file.', array('file' => $file));
  64. }
  65. }
  66. return false;
  67. }
  68. /**
  69. * Returns true if the template is still fresh.
  70. *
  71. * @param TemplateReferenceInterface $template A template
  72. * @param int $time The last modification time of the cached template (timestamp)
  73. *
  74. * @return bool true if the template is still fresh, false otherwise
  75. */
  76. public function isFresh(TemplateReferenceInterface $template, $time)
  77. {
  78. if (false === $storage = $this->load($template)) {
  79. return false;
  80. }
  81. return filemtime((string) $storage) < $time;
  82. }
  83. /**
  84. * Returns true if the file is an existing absolute path.
  85. *
  86. * @param string $file A path
  87. *
  88. * @return bool true if the path exists and is absolute, false otherwise
  89. */
  90. protected static function isAbsolutePath($file)
  91. {
  92. if ($file[0] == '/' || $file[0] == '\\'
  93. || (strlen($file) > 3 && ctype_alpha($file[0])
  94. && $file[1] == ':'
  95. && ($file[2] == '\\' || $file[2] == '/')
  96. )
  97. || null !== parse_url($file, PHP_URL_SCHEME)
  98. ) {
  99. return true;
  100. }
  101. return false;
  102. }
  103. }