|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Component\Templating\Loader; |
|
13 |
|
14 use Symfony\Component\Templating\Storage\Storage; |
|
15 use Symfony\Component\Templating\Storage\FileStorage; |
|
16 use Symfony\Component\Templating\TemplateReferenceInterface; |
|
17 |
|
18 /** |
|
19 * FilesystemLoader is a loader that read templates from the filesystem. |
|
20 * |
|
21 * @author Fabien Potencier <fabien@symfony.com> |
|
22 * |
|
23 * @api |
|
24 */ |
|
25 class FilesystemLoader extends Loader |
|
26 { |
|
27 protected $templatePathPatterns; |
|
28 |
|
29 /** |
|
30 * Constructor. |
|
31 * |
|
32 * @param array $templatePathPatterns An array of path patterns to look for templates |
|
33 * |
|
34 * @api |
|
35 */ |
|
36 public function __construct($templatePathPatterns) |
|
37 { |
|
38 $this->templatePathPatterns = (array) $templatePathPatterns; |
|
39 } |
|
40 |
|
41 /** |
|
42 * Loads a template. |
|
43 * |
|
44 * @param TemplateReferenceInterface $template A template |
|
45 * |
|
46 * @return Storage|Boolean false if the template cannot be loaded, a Storage instance otherwise |
|
47 * |
|
48 * @api |
|
49 */ |
|
50 public function load(TemplateReferenceInterface $template) |
|
51 { |
|
52 $file = $template->get('name'); |
|
53 |
|
54 if (self::isAbsolutePath($file) && file_exists($file)) { |
|
55 return new FileStorage($file); |
|
56 } |
|
57 |
|
58 $replacements = array(); |
|
59 foreach ($template->all() as $key => $value) { |
|
60 $replacements['%'.$key.'%'] = $value; |
|
61 } |
|
62 |
|
63 $logs = array(); |
|
64 foreach ($this->templatePathPatterns as $templatePathPattern) { |
|
65 if (is_file($file = strtr($templatePathPattern, $replacements)) && is_readable($file)) { |
|
66 if (null !== $this->debugger) { |
|
67 $this->debugger->log(sprintf('Loaded template file "%s"', $file)); |
|
68 } |
|
69 |
|
70 return new FileStorage($file); |
|
71 } |
|
72 |
|
73 if (null !== $this->debugger) { |
|
74 $logs[] = sprintf('Failed loading template file "%s"', $file); |
|
75 } |
|
76 } |
|
77 |
|
78 if (null !== $this->debugger) { |
|
79 foreach ($logs as $log) { |
|
80 $this->debugger->log($log); |
|
81 } |
|
82 } |
|
83 |
|
84 return false; |
|
85 } |
|
86 |
|
87 /** |
|
88 * Returns true if the template is still fresh. |
|
89 * |
|
90 * @param TemplateReferenceInterface $template A template |
|
91 * @param integer $time The last modification time of the cached template (timestamp) |
|
92 * |
|
93 * @api |
|
94 */ |
|
95 public function isFresh(TemplateReferenceInterface $template, $time) |
|
96 { |
|
97 if (false === $storage = $this->load($template)) { |
|
98 return false; |
|
99 } |
|
100 |
|
101 return filemtime((string) $storage) < $time; |
|
102 } |
|
103 |
|
104 /** |
|
105 * Returns true if the file is an existing absolute path. |
|
106 * |
|
107 * @param string $file A path |
|
108 * |
|
109 * @return true if the path exists and is absolute, false otherwise |
|
110 */ |
|
111 static protected function isAbsolutePath($file) |
|
112 { |
|
113 if ($file[0] == '/' || $file[0] == '\\' |
|
114 || (strlen($file) > 3 && ctype_alpha($file[0]) |
|
115 && $file[1] == ':' |
|
116 && ($file[2] == '\\' || $file[2] == '/') |
|
117 ) |
|
118 ) { |
|
119 return true; |
|
120 } |
|
121 |
|
122 return false; |
|
123 } |
|
124 } |