FilesystemLoader.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. foreach ($this->templatePathPatterns as $templatePathPattern)
  59. {
  60. if (is_file($file = strtr($templatePathPattern, $replacements)))
  61. {
  62. if ($this->debugger)
  63. {
  64. $this->debugger->log(sprintf('Loaded template file "%s" (renderer: %s)', $file, $options['renderer']));
  65. }
  66. return new FileStorage($file);
  67. }
  68. if ($this->debugger)
  69. {
  70. $this->debugger->log(sprintf('Failed loading template file "%s" (renderer: %s)', $file, $options['renderer']));
  71. }
  72. }
  73. return false;
  74. }
  75. /**
  76. * Returns true if the file is an existing absolute path.
  77. *
  78. * @param string $file A path
  79. *
  80. * @return true if the path exists and is absolute, false otherwise
  81. */
  82. static protected function isAbsolutePath($file)
  83. {
  84. if ($file[0] == '/' || $file[0] == '\\' ||
  85. (strlen($file) > 3 && ctype_alpha($file[0]) &&
  86. $file[1] == ':' &&
  87. ($file[2] == '\\' || $file[2] == '/')
  88. )
  89. )
  90. {
  91. return true;
  92. }
  93. return false;
  94. }
  95. }