123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace Symfony\Component\Templating\Loader;
- use Symfony\Component\Templating\Storage\Storage;
- use Symfony\Component\Templating\Storage\FileStorage;
- use Symfony\Component\Templating\TemplateReferenceInterface;
- class FilesystemLoader extends Loader
- {
- protected $templatePathPatterns;
-
- public function __construct($templatePathPatterns)
- {
- $this->templatePathPatterns = (array) $templatePathPatterns;
- }
-
- public function load(TemplateReferenceInterface $template)
- {
- $file = $template->get('name');
- if (self::isAbsolutePath($file) && is_file($file)) {
- return new FileStorage($file);
- }
- $replacements = array();
- foreach ($template->all() as $key => $value) {
- $replacements['%'.$key.'%'] = $value;
- }
- $fileFailures = array();
- foreach ($this->templatePathPatterns as $templatePathPattern) {
- if (is_file($file = strtr($templatePathPattern, $replacements)) && is_readable($file)) {
- if (null !== $this->logger) {
- $this->logger->debug('Loaded template file.', array('file' => $file));
- }
- return new FileStorage($file);
- }
- if (null !== $this->logger) {
- $fileFailures[] = $file;
- }
- }
-
- foreach ($fileFailures as $file) {
- if (null !== $this->logger) {
- $this->logger->debug('Failed loading template file.', array('file' => $file));
- }
- }
- return false;
- }
-
- public function isFresh(TemplateReferenceInterface $template, $time)
- {
- if (false === $storage = $this->load($template)) {
- return false;
- }
- return filemtime((string) $storage) < $time;
- }
-
- protected static function isAbsolutePath($file)
- {
- if ($file[0] == '/' || $file[0] == '\\'
- || (strlen($file) > 3 && ctype_alpha($file[0])
- && $file[1] == ':'
- && ($file[2] == '\\' || $file[2] == '/')
- )
- || null !== parse_url($file, PHP_URL_SCHEME)
- ) {
- return true;
- }
- return false;
- }
- }
|