PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/casoetan/ServerGroveLiveChat
PHP | 118 lines | 63 code | 17 blank | 38 comment | 14 complexity | 3a225b3f6f039dc9f3e53178ab846e2b MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  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\Component\Templating\Loader;
  11. use Symfony\Component\Templating\Storage\Storage;
  12. use Symfony\Component\Templating\Storage\FileStorage;
  13. /**
  14. * FilesystemLoader is a loader that read templates from the filesystem.
  15. *
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. */
  18. class FilesystemLoader extends Loader
  19. {
  20. protected $templatePathPatterns;
  21. /**
  22. * Constructor.
  23. *
  24. * @param array $templatePathPatterns An array of path patterns to look for templates
  25. */
  26. public function __construct($templatePathPatterns)
  27. {
  28. if (!is_array($templatePathPatterns)) {
  29. $templatePathPatterns = array($templatePathPatterns);
  30. }
  31. $this->templatePathPatterns = $templatePathPatterns;
  32. }
  33. /**
  34. * Loads a template.
  35. *
  36. * @param array $template The template name as an array
  37. *
  38. * @return Storage|Boolean false if the template cannot be loaded, a Storage instance otherwise
  39. */
  40. public function load($template)
  41. {
  42. if (self::isAbsolutePath($template['name']) && file_exists($template['name'])) {
  43. return new FileStorage($template['name']);
  44. }
  45. $replacements = array();
  46. foreach ($template as $key => $value) {
  47. $replacements['%'.$key.'%'] = $value;
  48. }
  49. $logs = array();
  50. foreach ($this->templatePathPatterns as $templatePathPattern) {
  51. if (is_file($file = strtr($templatePathPattern, $replacements))) {
  52. if (null !== $this->debugger) {
  53. $this->debugger->log(sprintf('Loaded template file "%s"', $file));
  54. }
  55. return new FileStorage($file);
  56. }
  57. if (null !== $this->debugger) {
  58. $logs[] = sprintf('Failed loading template file "%s"', $file);
  59. }
  60. }
  61. if (null !== $this->debugger) {
  62. foreach ($logs as $log) {
  63. $this->debugger->log($log);
  64. }
  65. }
  66. return false;
  67. }
  68. /**
  69. * Returns true if the template is still fresh.
  70. *
  71. * @param array $template The template name as an array
  72. * @param timestamp $time The last modification time of the cached template
  73. */
  74. public function isFresh($template, $time)
  75. {
  76. if (false === $template = $this->load($template))
  77. {
  78. return false;
  79. }
  80. return filemtime((string) $template) < $time;
  81. }
  82. /**
  83. * Returns true if the file is an existing absolute path.
  84. *
  85. * @param string $file A path
  86. *
  87. * @return true if the path exists and is absolute, false otherwise
  88. */
  89. static protected function isAbsolutePath($file)
  90. {
  91. if ($file[0] == '/' || $file[0] == '\\'
  92. || (strlen($file) > 3 && ctype_alpha($file[0])
  93. && $file[1] == ':'
  94. && ($file[2] == '\\' || $file[2] == '/')
  95. )
  96. ) {
  97. return true;
  98. }
  99. return false;
  100. }
  101. }