FilesystemLoader.php 3.4 KB

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