FilesystemLoader.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Symfony\Components\Templating\Loader;
  3. use Symfony\Components\Templating\Storage\Storage;
  4. use Symfony\Components\Templating\Storage\FileStorage;
  5. /*
  6. * This file is part of the symfony package.
  7. *
  8. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. /**
  14. * FilesystemLoader is a loader that read templates from the filesystem.
  15. *
  16. * @package symfony
  17. * @subpackage templating
  18. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  19. */
  20. class FilesystemLoader extends Loader
  21. {
  22. protected $templatePathPatterns;
  23. /**
  24. * Constructor.
  25. *
  26. * @param array $templatePathPatterns An array of path patterns to look for templates
  27. */
  28. public function __construct($templatePathPatterns)
  29. {
  30. if (!is_array($templatePathPatterns))
  31. {
  32. $templatePathPatterns = array($templatePathPatterns);
  33. }
  34. $this->templatePathPatterns = $templatePathPatterns;
  35. parent::__construct();
  36. }
  37. /**
  38. * Loads a template.
  39. *
  40. * @param string $template The logical template name
  41. * @param array $options An array of options
  42. *
  43. * @return Storage|Boolean false if the template cannot be loaded, a Storage instance otherwise
  44. */
  45. public function load($template, array $options = array())
  46. {
  47. if (self::isAbsolutePath($template) && file_exists($template))
  48. {
  49. return new FileStorage($template);
  50. }
  51. $options = $this->mergeDefaultOptions($options);
  52. $options['name'] = $template;
  53. $replacements = array();
  54. foreach ($options as $key => $value)
  55. {
  56. $replacements['%'.$key.'%'] = $value;
  57. }
  58. $logs = array();
  59. foreach ($this->templatePathPatterns as $templatePathPattern)
  60. {
  61. if (is_file($file = strtr($templatePathPattern, $replacements)))
  62. {
  63. if (null !== $this->debugger)
  64. {
  65. $this->debugger->log(sprintf('Loaded template file "%s" (renderer: %s)', $file, $options['renderer']));
  66. }
  67. return new FileStorage($file);
  68. }
  69. if (null !== $this->debugger)
  70. {
  71. $logs[] = sprintf('Failed loading template file "%s" (renderer: %s)', $file, $options['renderer']);
  72. }
  73. }
  74. if (null !== $this->debugger)
  75. {
  76. foreach ($logs as $log)
  77. {
  78. $this->debugger->log($log);
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. * Returns true if the file is an existing absolute path.
  85. *
  86. * @param string $file A path
  87. *
  88. * @return true if the path exists and is absolute, false otherwise
  89. */
  90. static protected 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. )
  98. {
  99. return true;
  100. }
  101. return false;
  102. }
  103. }