/vendor/symfony/src/Symfony/Component/Templating/Loader/FilesystemLoader.php

https://github.com/nguyennamtien/TaskBoxx · PHP · 116 lines · 61 code · 17 blank · 38 comment · 20 complexity · 403fad7f3ecbe3ab2c8f3454207386ba MD5 · raw file

  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|Boolean 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) && file_exists($file)) {
  42. return new FileStorage($file);
  43. }
  44. $replacements = array();
  45. foreach ($template->all() as $key => $value) {
  46. $replacements['%'.$key.'%'] = $value;
  47. }
  48. $logs = array();
  49. foreach ($this->templatePathPatterns as $templatePathPattern) {
  50. if (is_file($file = strtr($templatePathPattern, $replacements)) && is_readable($file)) {
  51. if (null !== $this->debugger) {
  52. $this->debugger->log(sprintf('Loaded template file "%s"', $file));
  53. }
  54. return new FileStorage($file);
  55. }
  56. if (null !== $this->debugger) {
  57. $logs[] = sprintf('Failed loading template file "%s"', $file);
  58. }
  59. }
  60. if (null !== $this->debugger) {
  61. foreach ($logs as $log) {
  62. $this->debugger->log($log);
  63. }
  64. }
  65. return false;
  66. }
  67. /**
  68. * Returns true if the template is still fresh.
  69. *
  70. * @param TemplateReferenceInterface $template A template
  71. * @param integer $time The last modification time of the cached template (timestamp)
  72. */
  73. public function isFresh(TemplateReferenceInterface $template, $time)
  74. {
  75. if (false === $storage = $this->load($template)) {
  76. return false;
  77. }
  78. return filemtime((string) $storage) < $time;
  79. }
  80. /**
  81. * Returns true if the file is an existing absolute path.
  82. *
  83. * @param string $file A path
  84. *
  85. * @return true if the path exists and is absolute, false otherwise
  86. */
  87. static protected function isAbsolutePath($file)
  88. {
  89. if ($file[0] == '/' || $file[0] == '\\'
  90. || (strlen($file) > 3 && ctype_alpha($file[0])
  91. && $file[1] == ':'
  92. && ($file[2] == '\\' || $file[2] == '/')
  93. )
  94. ) {
  95. return true;
  96. }
  97. return false;
  98. }
  99. }