@@ -29,507 +29,507 @@ |
||
29 | 29 | */ |
30 | 30 | class Str implements ITemplate |
31 | 31 | { |
32 | - /** |
|
33 | - * Template name. |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - protected $name; |
|
38 | - |
|
39 | - /** |
|
40 | - * Template compilation id. |
|
41 | - * |
|
42 | - * @var string |
|
43 | - */ |
|
44 | - protected $compileId; |
|
45 | - |
|
46 | - /** |
|
47 | - * Template cache id, if not provided in the constructor, it is set to |
|
48 | - * the md4 hash of the request_uri. it is however highly recommended to |
|
49 | - * provide one that will fit your needs. |
|
50 | - * in all cases, the compilation id is prepended to the cache id to separate |
|
51 | - * templates with similar cache ids from one another |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
55 | - protected $cacheId; |
|
56 | - |
|
57 | - /** |
|
58 | - * Validity duration of the generated cache file (in seconds). |
|
59 | - * set to -1 for infinite cache, 0 to disable and null to inherit the Dwoo instance's cache time |
|
60 | - * |
|
61 | - * @var int |
|
62 | - */ |
|
63 | - protected $cacheTime; |
|
64 | - |
|
65 | - /** |
|
66 | - * Boolean flag that defines whether the compilation should be enforced (once) or |
|
67 | - * not use this if you have issues with the compiled templates not being updated |
|
68 | - * but if you do need this it's most likely that you should file a bug report. |
|
69 | - * |
|
70 | - * @var bool |
|
71 | - */ |
|
72 | - protected $compilationEnforced; |
|
73 | - |
|
74 | - /** |
|
75 | - * Caches the results of the file checks to save some time when the same |
|
76 | - * templates is rendered several times. |
|
77 | - * |
|
78 | - * @var array |
|
79 | - */ |
|
80 | - protected static $cache = [ |
|
81 | - 'cached' => [], |
|
82 | - 'compiled' => [] |
|
83 | - ]; |
|
84 | - |
|
85 | - /** |
|
86 | - * Holds the compiler that built this template. |
|
87 | - * |
|
88 | - * @var ICompiler |
|
89 | - */ |
|
90 | - protected $compiler; |
|
91 | - |
|
92 | - /** |
|
93 | - * Chmod value for all files written (cached or compiled ones). |
|
94 | - * set to null if you don't want any chmod operation to happen |
|
95 | - * |
|
96 | - * @var int |
|
97 | - */ |
|
98 | - protected $chmod = 0777; |
|
99 | - |
|
100 | - /** |
|
101 | - * Containing template string. |
|
102 | - * |
|
103 | - * @var string |
|
104 | - */ |
|
105 | - protected $template; |
|
106 | - |
|
107 | - /** |
|
108 | - * Creates a template from a string. |
|
109 | - * |
|
110 | - * @param string $templateString the template to use |
|
111 | - * @param int $cacheTime duration of the cache validity for this template, |
|
112 | - * if null it defaults to the Dwoo instance that will |
|
113 | - * render this template, set to -1 for infinite cache or 0 to disable |
|
114 | - * @param string $cacheId the unique cache identifier of this page or anything else that |
|
115 | - * makes this template's content unique, if null it defaults |
|
116 | - * to the current url |
|
117 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
118 | - * template from others, if null it defaults to the md4 hash of the template |
|
119 | - */ |
|
120 | - public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) |
|
121 | - { |
|
122 | - $this->template = $templateString; |
|
123 | - $this->name = hash('md4', $templateString); |
|
124 | - $this->cacheTime = $cacheTime; |
|
125 | - |
|
126 | - if ($compileId !== null) { |
|
127 | - $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
128 | - } else { |
|
129 | - $this->compileId = $templateString; |
|
130 | - } |
|
131 | - |
|
132 | - if ($cacheId !== null) { |
|
133 | - $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Returns the cache duration for this template. |
|
139 | - * defaults to null if it was not provided |
|
140 | - * |
|
141 | - * @return int|null |
|
142 | - */ |
|
143 | - public function getCacheTime() |
|
144 | - { |
|
145 | - return $this->cacheTime; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Sets the cache duration for this template. |
|
150 | - * can be used to set it after the object is created if you did not provide |
|
151 | - * it in the constructor |
|
152 | - * |
|
153 | - * @param int $seconds duration of the cache validity for this template, if |
|
154 | - * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
155 | - * -1 = infinite cache |
|
156 | - */ |
|
157 | - public function setCacheTime($seconds = null) |
|
158 | - { |
|
159 | - $this->cacheTime = $seconds; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Returns the chmod value for all files written (cached or compiled ones). |
|
164 | - * defaults to 0777 |
|
165 | - * |
|
166 | - * @return int|null |
|
167 | - */ |
|
168 | - public function getChmod() |
|
169 | - { |
|
170 | - return $this->chmod; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Set the chmod value for all files written (cached or compiled ones). |
|
175 | - * set to null if you don't want to do any chmod() operation |
|
176 | - * |
|
177 | - * @param int $mask new bitmask to use for all files |
|
178 | - */ |
|
179 | - public function setChmod($mask = null) |
|
180 | - { |
|
181 | - $this->chmod = $mask; |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Returns the template name. |
|
186 | - * |
|
187 | - * @return string |
|
188 | - */ |
|
189 | - public function getName() |
|
190 | - { |
|
191 | - return $this->name; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Returns the resource name for this template class. |
|
196 | - * |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public function getResourceName() |
|
200 | - { |
|
201 | - return 'string'; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Returns the resource identifier for this template, false here as strings don't have identifiers. |
|
206 | - * |
|
207 | - * @return false |
|
208 | - */ |
|
209 | - public function getResourceIdentifier() |
|
210 | - { |
|
211 | - return false; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Returns the template source of this template. |
|
216 | - * |
|
217 | - * @return string |
|
218 | - */ |
|
219 | - public function getSource() |
|
220 | - { |
|
221 | - return $this->template; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Returns an unique value identifying the current version of this template, |
|
226 | - * in this case it's the md4 hash of the content. |
|
227 | - * |
|
228 | - * @return string |
|
229 | - */ |
|
230 | - public function getUid() |
|
231 | - { |
|
232 | - return $this->name; |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * Returns the compiler used by this template, if it was just compiled, or null. |
|
237 | - * |
|
238 | - * @return ICompiler |
|
239 | - */ |
|
240 | - public function getCompiler() |
|
241 | - { |
|
242 | - return $this->compiler; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Marks this template as compile-forced, which means it will be recompiled even if it |
|
247 | - * was already saved and wasn't modified since the last compilation. do not use this in production, |
|
248 | - * it's only meant to be used in development (and the development of dwoo particularly). |
|
249 | - */ |
|
250 | - public function forceCompilation() |
|
251 | - { |
|
252 | - $this->compilationEnforced = true; |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Returns the cached template output file name, true if it's cache-able but not cached |
|
257 | - * or false if it's not cached. |
|
258 | - * |
|
259 | - * @param Core $core the dwoo instance that requests it |
|
260 | - * |
|
261 | - * @return string|bool |
|
262 | - */ |
|
263 | - public function getCachedTemplate(Core $core) |
|
264 | - { |
|
265 | - $cacheLength = $core->getCacheTime(); |
|
266 | - if ($this->cacheTime !== null) { |
|
267 | - $cacheLength = $this->cacheTime; |
|
268 | - } |
|
269 | - |
|
270 | - // file is not cacheable |
|
271 | - if ($cacheLength == 0) { |
|
272 | - return false; |
|
273 | - } |
|
274 | - |
|
275 | - $cachedFile = $this->getCacheFilename($core); |
|
276 | - |
|
277 | - if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
|
278 | - // already checked, return cache file |
|
279 | - return $cachedFile; |
|
280 | - } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
281 | - // cache is still valid and can be loaded |
|
282 | - self::$cache['cached'][$this->cacheId] = true; |
|
283 | - |
|
284 | - return $cachedFile; |
|
285 | - } |
|
286 | - |
|
287 | - // file is cacheable |
|
288 | - return true; |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * Caches the provided output into the cache file. |
|
293 | - * |
|
294 | - * @param Core $core the dwoo instance that requests it |
|
295 | - * @param string $output the template output |
|
296 | - * |
|
297 | - * @return mixed full path of the cached file or false upon failure |
|
298 | - */ |
|
299 | - public function cache(Core $core, $output) |
|
300 | - { |
|
301 | - $cacheDir = $core->getCacheDir(); |
|
302 | - $cachedFile = $this->getCacheFilename($core); |
|
303 | - |
|
304 | - // the code below is courtesy of Rasmus Schultz, |
|
305 | - // thanks for his help on avoiding concurency issues |
|
306 | - $temp = tempnam($cacheDir, 'temp'); |
|
307 | - if (!($file = @fopen($temp, 'wb'))) { |
|
308 | - $temp = $cacheDir . uniqid('temp'); |
|
309 | - if (!($file = @fopen($temp, 'wb'))) { |
|
310 | - trigger_error('Error writing temporary file \'' . $temp . '\'', E_USER_WARNING); |
|
311 | - |
|
312 | - return false; |
|
313 | - } |
|
314 | - } |
|
315 | - |
|
316 | - fwrite($file, $output); |
|
317 | - fclose($file); |
|
318 | - |
|
319 | - $this->makeDirectory(dirname($cachedFile), $cacheDir); |
|
320 | - if (!@rename($temp, $cachedFile)) { |
|
321 | - @unlink($cachedFile); |
|
322 | - @rename($temp, $cachedFile); |
|
323 | - } |
|
324 | - |
|
325 | - if ($this->chmod !== null) { |
|
326 | - chmod($cachedFile, $this->chmod); |
|
327 | - } |
|
328 | - |
|
329 | - self::$cache['cached'][$this->cacheId] = true; |
|
330 | - |
|
331 | - return $cachedFile; |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Clears the cached template if it's older than the given time. |
|
336 | - * |
|
337 | - * @param Core $core the dwoo instance that was used to cache that template |
|
338 | - * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
339 | - * |
|
340 | - * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
341 | - */ |
|
342 | - public function clearCache(Core $core, $olderThan = - 1) |
|
343 | - { |
|
344 | - $cachedFile = $this->getCacheFilename($core); |
|
345 | - |
|
346 | - return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Returns the compiled template file name. |
|
351 | - * |
|
352 | - * @param Core $core the dwoo instance that requests it |
|
353 | - * @param ICompiler $compiler the compiler that must be used |
|
354 | - * |
|
355 | - * @return string |
|
356 | - */ |
|
357 | - public function getCompiledTemplate(Core $core, ICompiler $compiler = null) |
|
358 | - { |
|
359 | - $compiledFile = $this->getCompiledFilename($core); |
|
360 | - |
|
361 | - if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { |
|
362 | - // already checked, return compiled file |
|
363 | - } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { |
|
364 | - // template is compiled |
|
365 | - self::$cache['compiled'][$this->compileId] = true; |
|
366 | - } else { |
|
367 | - // compiles the template |
|
368 | - $this->compilationEnforced = false; |
|
369 | - |
|
370 | - if ($compiler === null) { |
|
371 | - $compiler = $core->getDefaultCompilerFactory($this->getResourceName()); |
|
372 | - |
|
373 | - if ($compiler === null || $compiler === ['Dwoo\Compiler', 'compilerFactory']) { |
|
374 | - $compiler = Compiler::compilerFactory(); |
|
375 | - } else { |
|
376 | - $compiler = call_user_func($compiler); |
|
377 | - } |
|
378 | - } |
|
379 | - |
|
380 | - $this->compiler = $compiler; |
|
381 | - |
|
382 | - $compiler->setCustomPlugins($core->getCustomPlugins()); |
|
383 | - $compiler->setSecurityPolicy($core->getSecurityPolicy()); |
|
384 | - $this->makeDirectory(dirname($compiledFile), $core->getCompileDir()); |
|
385 | - file_put_contents($compiledFile, $compiler->compile($core, $this)); |
|
386 | - if ($this->chmod !== null) { |
|
387 | - chmod($compiledFile, $this->chmod); |
|
388 | - } |
|
389 | - |
|
390 | - if (extension_loaded('Zend OPcache')) { |
|
391 | - opcache_invalidate($compiledFile); |
|
392 | - } elseif (extension_loaded('apc') && ini_get('apc.enabled')) { |
|
393 | - apc_delete_file($compiledFile); |
|
394 | - } |
|
395 | - |
|
396 | - self::$cache['compiled'][$this->compileId] = true; |
|
397 | - } |
|
398 | - |
|
399 | - return $compiledFile; |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Checks if compiled file is valid (it exists). |
|
404 | - * |
|
405 | - * @param string $file |
|
406 | - * |
|
407 | - * @return bool True cache file existence |
|
408 | - */ |
|
409 | - protected function isValidCompiledFile($file) |
|
410 | - { |
|
411 | - return file_exists($file); |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Returns a new template string object with the resource id being the template source code. |
|
416 | - * |
|
417 | - * @param Core $core the dwoo instance requiring it |
|
418 | - * @param mixed $resourceId the filename (relative to this template's dir) of the template to include |
|
419 | - * @param int $cacheTime duration of the cache validity for this template, if null it defaults to the |
|
420 | - * Dwoo instance that will render this template if null it defaults to the Dwoo |
|
421 | - * instance that will render this template |
|
422 | - * @param string $cacheId the unique cache identifier of this page or anything else that makes this |
|
423 | - * template's content unique, if null it defaults to the current url makes this |
|
424 | - * template's content unique, if null it defaults to the current url |
|
425 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this template from |
|
426 | - * others, if null it defaults to the filename+bits of the path template from |
|
427 | - * others, if null it defaults to the filename+bits of the path |
|
428 | - * @param ITemplate $parentTemplate the template that is requesting a new template object (through an include, |
|
429 | - * extends or any other plugin) an include, extends or any other plugin) |
|
430 | - * |
|
431 | - * @return $this |
|
432 | - */ |
|
433 | - public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, |
|
434 | - $compileId = null, ITemplate $parentTemplate = null) |
|
435 | - { |
|
436 | - return new self($resourceId, $cacheTime, $cacheId, $compileId); |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * Returns the full compiled file name and assigns a default value to it if |
|
441 | - * required. |
|
442 | - * |
|
443 | - * @param Core $core the Core instance that requests the file name |
|
444 | - * |
|
445 | - * @return string the full path to the compiled file |
|
446 | - */ |
|
447 | - protected function getCompiledFilename(Core $core) |
|
448 | - { |
|
449 | - return $core->getCompileDir() . hash('md4', $this->compileId) . '.d' . Core::RELEASE_TAG . '.php'; |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * Returns the full cached file name and assigns a default value to it if |
|
454 | - * required. |
|
455 | - * |
|
456 | - * @param Core $core the dwoo instance that requests the file name |
|
457 | - * |
|
458 | - * @return string the full path to the cached file |
|
459 | - */ |
|
460 | - protected function getCacheFilename(Core $core) |
|
461 | - { |
|
462 | - // no cache id provided, use request_uri as default |
|
463 | - if ($this->cacheId === null) { |
|
464 | - if (isset($_SERVER['REQUEST_URI']) === true) { |
|
465 | - $cacheId = $_SERVER['REQUEST_URI']; |
|
466 | - } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
|
467 | - $cacheId = $_SERVER['SCRIPT_FILENAME'] . '-' . implode('-', $_SERVER['argv']); |
|
468 | - } else { |
|
469 | - $cacheId = ''; |
|
470 | - } |
|
471 | - // force compiled id generation |
|
472 | - $this->getCompiledFilename($core); |
|
473 | - |
|
474 | - $this->cacheId = str_replace('../', '__', |
|
475 | - $this->compileId . strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
476 | - } |
|
477 | - |
|
478 | - return $core->getCacheDir() . $this->cacheId . '.html'; |
|
479 | - } |
|
480 | - |
|
481 | - /** |
|
482 | - * Returns some php code that will check if this template has been modified or not. |
|
483 | - * if the function returns null, the template will be instanciated and then the Uid checked |
|
484 | - * |
|
485 | - * @return string |
|
486 | - */ |
|
487 | - public function getIsModifiedCode() |
|
488 | - { |
|
489 | - return null; |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Ensures the given path exists. |
|
494 | - * |
|
495 | - * @param string $path any path |
|
496 | - * @param string $baseDir the base directory where the directory is created |
|
497 | - * ($path must still contain the full path, $baseDir |
|
498 | - * is only used for unix permissions) |
|
499 | - * |
|
500 | - * @throws Exception |
|
501 | - */ |
|
502 | - protected function makeDirectory($path, $baseDir = null) |
|
503 | - { |
|
504 | - if (is_dir($path) === true) { |
|
505 | - return; |
|
506 | - } |
|
507 | - |
|
508 | - if ($this->chmod === null) { |
|
509 | - $chmod = 0777; |
|
510 | - } else { |
|
511 | - $chmod = $this->chmod; |
|
512 | - } |
|
513 | - |
|
514 | - $retries = 3; |
|
515 | - while ($retries --) { |
|
516 | - @mkdir($path, $chmod, true); |
|
517 | - if (is_dir($path)) { |
|
518 | - break; |
|
519 | - } |
|
520 | - usleep(20); |
|
521 | - } |
|
522 | - |
|
523 | - // enforce the correct mode for all directories created |
|
524 | - if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { |
|
525 | - $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
|
526 | - $folders = explode('/', trim($path, '/')); |
|
527 | - foreach ($folders as $folder) { |
|
528 | - $baseDir .= $folder . DIRECTORY_SEPARATOR; |
|
529 | - if (!chmod($baseDir, $chmod)) { |
|
530 | - throw new Exception('Unable to chmod ' . "$baseDir to $chmod: " . print_r(error_get_last(), true)); |
|
531 | - } |
|
532 | - } |
|
533 | - } |
|
534 | - } |
|
32 | + /** |
|
33 | + * Template name. |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + protected $name; |
|
38 | + |
|
39 | + /** |
|
40 | + * Template compilation id. |
|
41 | + * |
|
42 | + * @var string |
|
43 | + */ |
|
44 | + protected $compileId; |
|
45 | + |
|
46 | + /** |
|
47 | + * Template cache id, if not provided in the constructor, it is set to |
|
48 | + * the md4 hash of the request_uri. it is however highly recommended to |
|
49 | + * provide one that will fit your needs. |
|
50 | + * in all cases, the compilation id is prepended to the cache id to separate |
|
51 | + * templates with similar cache ids from one another |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | + protected $cacheId; |
|
56 | + |
|
57 | + /** |
|
58 | + * Validity duration of the generated cache file (in seconds). |
|
59 | + * set to -1 for infinite cache, 0 to disable and null to inherit the Dwoo instance's cache time |
|
60 | + * |
|
61 | + * @var int |
|
62 | + */ |
|
63 | + protected $cacheTime; |
|
64 | + |
|
65 | + /** |
|
66 | + * Boolean flag that defines whether the compilation should be enforced (once) or |
|
67 | + * not use this if you have issues with the compiled templates not being updated |
|
68 | + * but if you do need this it's most likely that you should file a bug report. |
|
69 | + * |
|
70 | + * @var bool |
|
71 | + */ |
|
72 | + protected $compilationEnforced; |
|
73 | + |
|
74 | + /** |
|
75 | + * Caches the results of the file checks to save some time when the same |
|
76 | + * templates is rendered several times. |
|
77 | + * |
|
78 | + * @var array |
|
79 | + */ |
|
80 | + protected static $cache = [ |
|
81 | + 'cached' => [], |
|
82 | + 'compiled' => [] |
|
83 | + ]; |
|
84 | + |
|
85 | + /** |
|
86 | + * Holds the compiler that built this template. |
|
87 | + * |
|
88 | + * @var ICompiler |
|
89 | + */ |
|
90 | + protected $compiler; |
|
91 | + |
|
92 | + /** |
|
93 | + * Chmod value for all files written (cached or compiled ones). |
|
94 | + * set to null if you don't want any chmod operation to happen |
|
95 | + * |
|
96 | + * @var int |
|
97 | + */ |
|
98 | + protected $chmod = 0777; |
|
99 | + |
|
100 | + /** |
|
101 | + * Containing template string. |
|
102 | + * |
|
103 | + * @var string |
|
104 | + */ |
|
105 | + protected $template; |
|
106 | + |
|
107 | + /** |
|
108 | + * Creates a template from a string. |
|
109 | + * |
|
110 | + * @param string $templateString the template to use |
|
111 | + * @param int $cacheTime duration of the cache validity for this template, |
|
112 | + * if null it defaults to the Dwoo instance that will |
|
113 | + * render this template, set to -1 for infinite cache or 0 to disable |
|
114 | + * @param string $cacheId the unique cache identifier of this page or anything else that |
|
115 | + * makes this template's content unique, if null it defaults |
|
116 | + * to the current url |
|
117 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
118 | + * template from others, if null it defaults to the md4 hash of the template |
|
119 | + */ |
|
120 | + public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) |
|
121 | + { |
|
122 | + $this->template = $templateString; |
|
123 | + $this->name = hash('md4', $templateString); |
|
124 | + $this->cacheTime = $cacheTime; |
|
125 | + |
|
126 | + if ($compileId !== null) { |
|
127 | + $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
128 | + } else { |
|
129 | + $this->compileId = $templateString; |
|
130 | + } |
|
131 | + |
|
132 | + if ($cacheId !== null) { |
|
133 | + $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Returns the cache duration for this template. |
|
139 | + * defaults to null if it was not provided |
|
140 | + * |
|
141 | + * @return int|null |
|
142 | + */ |
|
143 | + public function getCacheTime() |
|
144 | + { |
|
145 | + return $this->cacheTime; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Sets the cache duration for this template. |
|
150 | + * can be used to set it after the object is created if you did not provide |
|
151 | + * it in the constructor |
|
152 | + * |
|
153 | + * @param int $seconds duration of the cache validity for this template, if |
|
154 | + * null it defaults to the Dwoo instance's cache time. 0 = disable and |
|
155 | + * -1 = infinite cache |
|
156 | + */ |
|
157 | + public function setCacheTime($seconds = null) |
|
158 | + { |
|
159 | + $this->cacheTime = $seconds; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Returns the chmod value for all files written (cached or compiled ones). |
|
164 | + * defaults to 0777 |
|
165 | + * |
|
166 | + * @return int|null |
|
167 | + */ |
|
168 | + public function getChmod() |
|
169 | + { |
|
170 | + return $this->chmod; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Set the chmod value for all files written (cached or compiled ones). |
|
175 | + * set to null if you don't want to do any chmod() operation |
|
176 | + * |
|
177 | + * @param int $mask new bitmask to use for all files |
|
178 | + */ |
|
179 | + public function setChmod($mask = null) |
|
180 | + { |
|
181 | + $this->chmod = $mask; |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Returns the template name. |
|
186 | + * |
|
187 | + * @return string |
|
188 | + */ |
|
189 | + public function getName() |
|
190 | + { |
|
191 | + return $this->name; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Returns the resource name for this template class. |
|
196 | + * |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public function getResourceName() |
|
200 | + { |
|
201 | + return 'string'; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Returns the resource identifier for this template, false here as strings don't have identifiers. |
|
206 | + * |
|
207 | + * @return false |
|
208 | + */ |
|
209 | + public function getResourceIdentifier() |
|
210 | + { |
|
211 | + return false; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Returns the template source of this template. |
|
216 | + * |
|
217 | + * @return string |
|
218 | + */ |
|
219 | + public function getSource() |
|
220 | + { |
|
221 | + return $this->template; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Returns an unique value identifying the current version of this template, |
|
226 | + * in this case it's the md4 hash of the content. |
|
227 | + * |
|
228 | + * @return string |
|
229 | + */ |
|
230 | + public function getUid() |
|
231 | + { |
|
232 | + return $this->name; |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * Returns the compiler used by this template, if it was just compiled, or null. |
|
237 | + * |
|
238 | + * @return ICompiler |
|
239 | + */ |
|
240 | + public function getCompiler() |
|
241 | + { |
|
242 | + return $this->compiler; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Marks this template as compile-forced, which means it will be recompiled even if it |
|
247 | + * was already saved and wasn't modified since the last compilation. do not use this in production, |
|
248 | + * it's only meant to be used in development (and the development of dwoo particularly). |
|
249 | + */ |
|
250 | + public function forceCompilation() |
|
251 | + { |
|
252 | + $this->compilationEnforced = true; |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Returns the cached template output file name, true if it's cache-able but not cached |
|
257 | + * or false if it's not cached. |
|
258 | + * |
|
259 | + * @param Core $core the dwoo instance that requests it |
|
260 | + * |
|
261 | + * @return string|bool |
|
262 | + */ |
|
263 | + public function getCachedTemplate(Core $core) |
|
264 | + { |
|
265 | + $cacheLength = $core->getCacheTime(); |
|
266 | + if ($this->cacheTime !== null) { |
|
267 | + $cacheLength = $this->cacheTime; |
|
268 | + } |
|
269 | + |
|
270 | + // file is not cacheable |
|
271 | + if ($cacheLength == 0) { |
|
272 | + return false; |
|
273 | + } |
|
274 | + |
|
275 | + $cachedFile = $this->getCacheFilename($core); |
|
276 | + |
|
277 | + if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
|
278 | + // already checked, return cache file |
|
279 | + return $cachedFile; |
|
280 | + } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
281 | + // cache is still valid and can be loaded |
|
282 | + self::$cache['cached'][$this->cacheId] = true; |
|
283 | + |
|
284 | + return $cachedFile; |
|
285 | + } |
|
286 | + |
|
287 | + // file is cacheable |
|
288 | + return true; |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * Caches the provided output into the cache file. |
|
293 | + * |
|
294 | + * @param Core $core the dwoo instance that requests it |
|
295 | + * @param string $output the template output |
|
296 | + * |
|
297 | + * @return mixed full path of the cached file or false upon failure |
|
298 | + */ |
|
299 | + public function cache(Core $core, $output) |
|
300 | + { |
|
301 | + $cacheDir = $core->getCacheDir(); |
|
302 | + $cachedFile = $this->getCacheFilename($core); |
|
303 | + |
|
304 | + // the code below is courtesy of Rasmus Schultz, |
|
305 | + // thanks for his help on avoiding concurency issues |
|
306 | + $temp = tempnam($cacheDir, 'temp'); |
|
307 | + if (!($file = @fopen($temp, 'wb'))) { |
|
308 | + $temp = $cacheDir . uniqid('temp'); |
|
309 | + if (!($file = @fopen($temp, 'wb'))) { |
|
310 | + trigger_error('Error writing temporary file \'' . $temp . '\'', E_USER_WARNING); |
|
311 | + |
|
312 | + return false; |
|
313 | + } |
|
314 | + } |
|
315 | + |
|
316 | + fwrite($file, $output); |
|
317 | + fclose($file); |
|
318 | + |
|
319 | + $this->makeDirectory(dirname($cachedFile), $cacheDir); |
|
320 | + if (!@rename($temp, $cachedFile)) { |
|
321 | + @unlink($cachedFile); |
|
322 | + @rename($temp, $cachedFile); |
|
323 | + } |
|
324 | + |
|
325 | + if ($this->chmod !== null) { |
|
326 | + chmod($cachedFile, $this->chmod); |
|
327 | + } |
|
328 | + |
|
329 | + self::$cache['cached'][$this->cacheId] = true; |
|
330 | + |
|
331 | + return $cachedFile; |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Clears the cached template if it's older than the given time. |
|
336 | + * |
|
337 | + * @param Core $core the dwoo instance that was used to cache that template |
|
338 | + * @param int $olderThan minimum time (in seconds) required for the cache to be cleared |
|
339 | + * |
|
340 | + * @return bool true if the cache was not present or if it was deleted, false if it remains there |
|
341 | + */ |
|
342 | + public function clearCache(Core $core, $olderThan = - 1) |
|
343 | + { |
|
344 | + $cachedFile = $this->getCacheFilename($core); |
|
345 | + |
|
346 | + return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Returns the compiled template file name. |
|
351 | + * |
|
352 | + * @param Core $core the dwoo instance that requests it |
|
353 | + * @param ICompiler $compiler the compiler that must be used |
|
354 | + * |
|
355 | + * @return string |
|
356 | + */ |
|
357 | + public function getCompiledTemplate(Core $core, ICompiler $compiler = null) |
|
358 | + { |
|
359 | + $compiledFile = $this->getCompiledFilename($core); |
|
360 | + |
|
361 | + if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { |
|
362 | + // already checked, return compiled file |
|
363 | + } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { |
|
364 | + // template is compiled |
|
365 | + self::$cache['compiled'][$this->compileId] = true; |
|
366 | + } else { |
|
367 | + // compiles the template |
|
368 | + $this->compilationEnforced = false; |
|
369 | + |
|
370 | + if ($compiler === null) { |
|
371 | + $compiler = $core->getDefaultCompilerFactory($this->getResourceName()); |
|
372 | + |
|
373 | + if ($compiler === null || $compiler === ['Dwoo\Compiler', 'compilerFactory']) { |
|
374 | + $compiler = Compiler::compilerFactory(); |
|
375 | + } else { |
|
376 | + $compiler = call_user_func($compiler); |
|
377 | + } |
|
378 | + } |
|
379 | + |
|
380 | + $this->compiler = $compiler; |
|
381 | + |
|
382 | + $compiler->setCustomPlugins($core->getCustomPlugins()); |
|
383 | + $compiler->setSecurityPolicy($core->getSecurityPolicy()); |
|
384 | + $this->makeDirectory(dirname($compiledFile), $core->getCompileDir()); |
|
385 | + file_put_contents($compiledFile, $compiler->compile($core, $this)); |
|
386 | + if ($this->chmod !== null) { |
|
387 | + chmod($compiledFile, $this->chmod); |
|
388 | + } |
|
389 | + |
|
390 | + if (extension_loaded('Zend OPcache')) { |
|
391 | + opcache_invalidate($compiledFile); |
|
392 | + } elseif (extension_loaded('apc') && ini_get('apc.enabled')) { |
|
393 | + apc_delete_file($compiledFile); |
|
394 | + } |
|
395 | + |
|
396 | + self::$cache['compiled'][$this->compileId] = true; |
|
397 | + } |
|
398 | + |
|
399 | + return $compiledFile; |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Checks if compiled file is valid (it exists). |
|
404 | + * |
|
405 | + * @param string $file |
|
406 | + * |
|
407 | + * @return bool True cache file existence |
|
408 | + */ |
|
409 | + protected function isValidCompiledFile($file) |
|
410 | + { |
|
411 | + return file_exists($file); |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Returns a new template string object with the resource id being the template source code. |
|
416 | + * |
|
417 | + * @param Core $core the dwoo instance requiring it |
|
418 | + * @param mixed $resourceId the filename (relative to this template's dir) of the template to include |
|
419 | + * @param int $cacheTime duration of the cache validity for this template, if null it defaults to the |
|
420 | + * Dwoo instance that will render this template if null it defaults to the Dwoo |
|
421 | + * instance that will render this template |
|
422 | + * @param string $cacheId the unique cache identifier of this page or anything else that makes this |
|
423 | + * template's content unique, if null it defaults to the current url makes this |
|
424 | + * template's content unique, if null it defaults to the current url |
|
425 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this template from |
|
426 | + * others, if null it defaults to the filename+bits of the path template from |
|
427 | + * others, if null it defaults to the filename+bits of the path |
|
428 | + * @param ITemplate $parentTemplate the template that is requesting a new template object (through an include, |
|
429 | + * extends or any other plugin) an include, extends or any other plugin) |
|
430 | + * |
|
431 | + * @return $this |
|
432 | + */ |
|
433 | + public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, |
|
434 | + $compileId = null, ITemplate $parentTemplate = null) |
|
435 | + { |
|
436 | + return new self($resourceId, $cacheTime, $cacheId, $compileId); |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * Returns the full compiled file name and assigns a default value to it if |
|
441 | + * required. |
|
442 | + * |
|
443 | + * @param Core $core the Core instance that requests the file name |
|
444 | + * |
|
445 | + * @return string the full path to the compiled file |
|
446 | + */ |
|
447 | + protected function getCompiledFilename(Core $core) |
|
448 | + { |
|
449 | + return $core->getCompileDir() . hash('md4', $this->compileId) . '.d' . Core::RELEASE_TAG . '.php'; |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * Returns the full cached file name and assigns a default value to it if |
|
454 | + * required. |
|
455 | + * |
|
456 | + * @param Core $core the dwoo instance that requests the file name |
|
457 | + * |
|
458 | + * @return string the full path to the cached file |
|
459 | + */ |
|
460 | + protected function getCacheFilename(Core $core) |
|
461 | + { |
|
462 | + // no cache id provided, use request_uri as default |
|
463 | + if ($this->cacheId === null) { |
|
464 | + if (isset($_SERVER['REQUEST_URI']) === true) { |
|
465 | + $cacheId = $_SERVER['REQUEST_URI']; |
|
466 | + } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
|
467 | + $cacheId = $_SERVER['SCRIPT_FILENAME'] . '-' . implode('-', $_SERVER['argv']); |
|
468 | + } else { |
|
469 | + $cacheId = ''; |
|
470 | + } |
|
471 | + // force compiled id generation |
|
472 | + $this->getCompiledFilename($core); |
|
473 | + |
|
474 | + $this->cacheId = str_replace('../', '__', |
|
475 | + $this->compileId . strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
476 | + } |
|
477 | + |
|
478 | + return $core->getCacheDir() . $this->cacheId . '.html'; |
|
479 | + } |
|
480 | + |
|
481 | + /** |
|
482 | + * Returns some php code that will check if this template has been modified or not. |
|
483 | + * if the function returns null, the template will be instanciated and then the Uid checked |
|
484 | + * |
|
485 | + * @return string |
|
486 | + */ |
|
487 | + public function getIsModifiedCode() |
|
488 | + { |
|
489 | + return null; |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Ensures the given path exists. |
|
494 | + * |
|
495 | + * @param string $path any path |
|
496 | + * @param string $baseDir the base directory where the directory is created |
|
497 | + * ($path must still contain the full path, $baseDir |
|
498 | + * is only used for unix permissions) |
|
499 | + * |
|
500 | + * @throws Exception |
|
501 | + */ |
|
502 | + protected function makeDirectory($path, $baseDir = null) |
|
503 | + { |
|
504 | + if (is_dir($path) === true) { |
|
505 | + return; |
|
506 | + } |
|
507 | + |
|
508 | + if ($this->chmod === null) { |
|
509 | + $chmod = 0777; |
|
510 | + } else { |
|
511 | + $chmod = $this->chmod; |
|
512 | + } |
|
513 | + |
|
514 | + $retries = 3; |
|
515 | + while ($retries --) { |
|
516 | + @mkdir($path, $chmod, true); |
|
517 | + if (is_dir($path)) { |
|
518 | + break; |
|
519 | + } |
|
520 | + usleep(20); |
|
521 | + } |
|
522 | + |
|
523 | + // enforce the correct mode for all directories created |
|
524 | + if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { |
|
525 | + $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
|
526 | + $folders = explode('/', trim($path, '/')); |
|
527 | + foreach ($folders as $folder) { |
|
528 | + $baseDir .= $folder . DIRECTORY_SEPARATOR; |
|
529 | + if (!chmod($baseDir, $chmod)) { |
|
530 | + throw new Exception('Unable to chmod ' . "$baseDir to $chmod: " . print_r(error_get_last(), true)); |
|
531 | + } |
|
532 | + } |
|
533 | + } |
|
534 | + } |
|
535 | 535 | } |
@@ -124,13 +124,13 @@ discard block |
||
124 | 124 | $this->cacheTime = $cacheTime; |
125 | 125 | |
126 | 126 | if ($compileId !== null) { |
127 | - $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
127 | + $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
128 | 128 | } else { |
129 | 129 | $this->compileId = $templateString; |
130 | 130 | } |
131 | 131 | |
132 | 132 | if ($cacheId !== null) { |
133 | - $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
133 | + $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
134 | 134 | } |
135 | 135 | } |
136 | 136 | |
@@ -277,7 +277,7 @@ discard block |
||
277 | 277 | if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { |
278 | 278 | // already checked, return cache file |
279 | 279 | return $cachedFile; |
280 | - } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
280 | + } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === - 1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME']-$cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($core))) { |
|
281 | 281 | // cache is still valid and can be loaded |
282 | 282 | self::$cache['cached'][$this->cacheId] = true; |
283 | 283 | |
@@ -305,9 +305,9 @@ discard block |
||
305 | 305 | // thanks for his help on avoiding concurency issues |
306 | 306 | $temp = tempnam($cacheDir, 'temp'); |
307 | 307 | if (!($file = @fopen($temp, 'wb'))) { |
308 | - $temp = $cacheDir . uniqid('temp'); |
|
308 | + $temp = $cacheDir.uniqid('temp'); |
|
309 | 309 | if (!($file = @fopen($temp, 'wb'))) { |
310 | - trigger_error('Error writing temporary file \'' . $temp . '\'', E_USER_WARNING); |
|
310 | + trigger_error('Error writing temporary file \''.$temp.'\'', E_USER_WARNING); |
|
311 | 311 | |
312 | 312 | return false; |
313 | 313 | } |
@@ -343,7 +343,7 @@ discard block |
||
343 | 343 | { |
344 | 344 | $cachedFile = $this->getCacheFilename($core); |
345 | 345 | |
346 | - return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); |
|
346 | + return !file_exists($cachedFile) || (filectime($cachedFile) < (time()-$olderThan) && unlink($cachedFile)); |
|
347 | 347 | } |
348 | 348 | |
349 | 349 | /** |
@@ -446,7 +446,7 @@ discard block |
||
446 | 446 | */ |
447 | 447 | protected function getCompiledFilename(Core $core) |
448 | 448 | { |
449 | - return $core->getCompileDir() . hash('md4', $this->compileId) . '.d' . Core::RELEASE_TAG . '.php'; |
|
449 | + return $core->getCompileDir().hash('md4', $this->compileId).'.d'.Core::RELEASE_TAG.'.php'; |
|
450 | 450 | } |
451 | 451 | |
452 | 452 | /** |
@@ -464,7 +464,7 @@ discard block |
||
464 | 464 | if (isset($_SERVER['REQUEST_URI']) === true) { |
465 | 465 | $cacheId = $_SERVER['REQUEST_URI']; |
466 | 466 | } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { |
467 | - $cacheId = $_SERVER['SCRIPT_FILENAME'] . '-' . implode('-', $_SERVER['argv']); |
|
467 | + $cacheId = $_SERVER['SCRIPT_FILENAME'].'-'.implode('-', $_SERVER['argv']); |
|
468 | 468 | } else { |
469 | 469 | $cacheId = ''; |
470 | 470 | } |
@@ -472,10 +472,10 @@ discard block |
||
472 | 472 | $this->getCompiledFilename($core); |
473 | 473 | |
474 | 474 | $this->cacheId = str_replace('../', '__', |
475 | - $this->compileId . strtr($cacheId, '\\%?=!:;' . PATH_SEPARATOR, '/-------')); |
|
475 | + $this->compileId.strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); |
|
476 | 476 | } |
477 | 477 | |
478 | - return $core->getCacheDir() . $this->cacheId . '.html'; |
|
478 | + return $core->getCacheDir().$this->cacheId.'.html'; |
|
479 | 479 | } |
480 | 480 | |
481 | 481 | /** |
@@ -512,7 +512,7 @@ discard block |
||
512 | 512 | } |
513 | 513 | |
514 | 514 | $retries = 3; |
515 | - while ($retries --) { |
|
515 | + while ($retries--) { |
|
516 | 516 | @mkdir($path, $chmod, true); |
517 | 517 | if (is_dir($path)) { |
518 | 518 | break; |
@@ -525,9 +525,9 @@ discard block |
||
525 | 525 | $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); |
526 | 526 | $folders = explode('/', trim($path, '/')); |
527 | 527 | foreach ($folders as $folder) { |
528 | - $baseDir .= $folder . DIRECTORY_SEPARATOR; |
|
528 | + $baseDir .= $folder.DIRECTORY_SEPARATOR; |
|
529 | 529 | if (!chmod($baseDir, $chmod)) { |
530 | - throw new Exception('Unable to chmod ' . "$baseDir to $chmod: " . print_r(error_get_last(), true)); |
|
530 | + throw new Exception('Unable to chmod '."$baseDir to $chmod: ".print_r(error_get_last(), true)); |
|
531 | 531 | } |
532 | 532 | } |
533 | 533 | } |
@@ -30,247 +30,247 @@ |
||
30 | 30 | */ |
31 | 31 | class File extends Str |
32 | 32 | { |
33 | - /** |
|
34 | - * Template filename. |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $file; |
|
33 | + /** |
|
34 | + * Template filename. |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $file; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Include path(s) to look into to find this template. |
|
42 | - * |
|
43 | - * @var array |
|
44 | - */ |
|
45 | - protected $includePath = []; |
|
40 | + /** |
|
41 | + * Include path(s) to look into to find this template. |
|
42 | + * |
|
43 | + * @var array |
|
44 | + */ |
|
45 | + protected $includePath = []; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Resolved path cache when looking for a file in multiple include paths. |
|
49 | - * this is reset when the include path is changed |
|
50 | - * |
|
51 | - * @var string |
|
52 | - */ |
|
53 | - protected $resolvedPath = null; |
|
47 | + /** |
|
48 | + * Resolved path cache when looking for a file in multiple include paths. |
|
49 | + * this is reset when the include path is changed |
|
50 | + * |
|
51 | + * @var string |
|
52 | + */ |
|
53 | + protected $resolvedPath = null; |
|
54 | 54 | |
55 | - /** |
|
56 | - * Creates a template from a file. |
|
57 | - * |
|
58 | - * @param string $file the path to the template file, make sure it exists |
|
59 | - * @param int $cacheTime duration of the cache validity for this template, |
|
60 | - * if null it defaults to the Dwoo instance that will |
|
61 | - * render this template |
|
62 | - * @param string $cacheId the unique cache identifier of this page or anything else that |
|
63 | - * makes this template's content unique, if null it defaults |
|
64 | - * to the current url |
|
65 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
66 | - * template from others, if null it defaults to the filename+bits of the path |
|
67 | - * @param mixed $includePath a string for a single path to look into for the given file, or an array of paths |
|
68 | - */ |
|
69 | - public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null, $includePath = []) |
|
70 | - { |
|
71 | - parent::__construct($file, $cacheTime, $cacheId, $compileId); |
|
72 | - $this->template = null; |
|
73 | - $this->file = $file; |
|
74 | - $this->name = basename($file); |
|
75 | - $this->setIncludePath($includePath); |
|
76 | - $this->compileId = $this->getResourceIdentifier(); |
|
77 | - } |
|
55 | + /** |
|
56 | + * Creates a template from a file. |
|
57 | + * |
|
58 | + * @param string $file the path to the template file, make sure it exists |
|
59 | + * @param int $cacheTime duration of the cache validity for this template, |
|
60 | + * if null it defaults to the Dwoo instance that will |
|
61 | + * render this template |
|
62 | + * @param string $cacheId the unique cache identifier of this page or anything else that |
|
63 | + * makes this template's content unique, if null it defaults |
|
64 | + * to the current url |
|
65 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
66 | + * template from others, if null it defaults to the filename+bits of the path |
|
67 | + * @param mixed $includePath a string for a single path to look into for the given file, or an array of paths |
|
68 | + */ |
|
69 | + public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null, $includePath = []) |
|
70 | + { |
|
71 | + parent::__construct($file, $cacheTime, $cacheId, $compileId); |
|
72 | + $this->template = null; |
|
73 | + $this->file = $file; |
|
74 | + $this->name = basename($file); |
|
75 | + $this->setIncludePath($includePath); |
|
76 | + $this->compileId = $this->getResourceIdentifier(); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Sets the include path(s) to where the given template filename must be looked up. |
|
81 | - * |
|
82 | - * @param mixed $paths the path to look into, can be string for a single path or an array of paths |
|
83 | - */ |
|
84 | - public function setIncludePath($paths) |
|
85 | - { |
|
86 | - if (is_array($paths) === false) { |
|
87 | - $paths = [$paths]; |
|
88 | - } |
|
79 | + /** |
|
80 | + * Sets the include path(s) to where the given template filename must be looked up. |
|
81 | + * |
|
82 | + * @param mixed $paths the path to look into, can be string for a single path or an array of paths |
|
83 | + */ |
|
84 | + public function setIncludePath($paths) |
|
85 | + { |
|
86 | + if (is_array($paths) === false) { |
|
87 | + $paths = [$paths]; |
|
88 | + } |
|
89 | 89 | |
90 | - $this->includePath = $paths; |
|
91 | - $this->resolvedPath = null; |
|
92 | - } |
|
90 | + $this->includePath = $paths; |
|
91 | + $this->resolvedPath = null; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Return the current include path(s). |
|
96 | - * |
|
97 | - * @return array |
|
98 | - */ |
|
99 | - public function getIncludePath() |
|
100 | - { |
|
101 | - return $this->includePath; |
|
102 | - } |
|
94 | + /** |
|
95 | + * Return the current include path(s). |
|
96 | + * |
|
97 | + * @return array |
|
98 | + */ |
|
99 | + public function getIncludePath() |
|
100 | + { |
|
101 | + return $this->includePath; |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * Checks if compiled file is valid (exists and it's the modification is greater or |
|
106 | - * equal to the modification time of the template file). |
|
107 | - * |
|
108 | - * @param string file |
|
109 | - * |
|
110 | - * @return bool True cache file existance and it's modification time |
|
111 | - */ |
|
112 | - protected function isValidCompiledFile($file) |
|
113 | - { |
|
114 | - return parent::isValidCompiledFile($file) && (int)$this->getUid() <= filemtime($file); |
|
115 | - } |
|
104 | + /** |
|
105 | + * Checks if compiled file is valid (exists and it's the modification is greater or |
|
106 | + * equal to the modification time of the template file). |
|
107 | + * |
|
108 | + * @param string file |
|
109 | + * |
|
110 | + * @return bool True cache file existance and it's modification time |
|
111 | + */ |
|
112 | + protected function isValidCompiledFile($file) |
|
113 | + { |
|
114 | + return parent::isValidCompiledFile($file) && (int)$this->getUid() <= filemtime($file); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Returns the template source of this template. |
|
119 | - * |
|
120 | - * @return string |
|
121 | - */ |
|
122 | - public function getSource() |
|
123 | - { |
|
124 | - return file_get_contents($this->getResourceIdentifier()); |
|
125 | - } |
|
117 | + /** |
|
118 | + * Returns the template source of this template. |
|
119 | + * |
|
120 | + * @return string |
|
121 | + */ |
|
122 | + public function getSource() |
|
123 | + { |
|
124 | + return file_get_contents($this->getResourceIdentifier()); |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * Returns the resource name for this template class. |
|
129 | - * |
|
130 | - * @return string |
|
131 | - */ |
|
132 | - public function getResourceName() |
|
133 | - { |
|
134 | - return 'file'; |
|
135 | - } |
|
127 | + /** |
|
128 | + * Returns the resource name for this template class. |
|
129 | + * |
|
130 | + * @return string |
|
131 | + */ |
|
132 | + public function getResourceName() |
|
133 | + { |
|
134 | + return 'file'; |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * Returns this template's source filename. |
|
139 | - * |
|
140 | - * @return string |
|
141 | - * @throws DwooException |
|
142 | - */ |
|
143 | - public function getResourceIdentifier() |
|
144 | - { |
|
145 | - if ($this->resolvedPath !== null) { |
|
146 | - return $this->resolvedPath; |
|
147 | - } elseif (array_filter($this->getIncludePath()) == []) { |
|
148 | - return $this->file; |
|
149 | - } else { |
|
150 | - foreach ($this->getIncludePath() as $path) { |
|
151 | - $path = rtrim($path, DIRECTORY_SEPARATOR); |
|
152 | - if (file_exists($path . DIRECTORY_SEPARATOR . $this->file) === true) { |
|
153 | - return $this->resolvedPath = $path . DIRECTORY_SEPARATOR . $this->file; |
|
154 | - } |
|
155 | - } |
|
137 | + /** |
|
138 | + * Returns this template's source filename. |
|
139 | + * |
|
140 | + * @return string |
|
141 | + * @throws DwooException |
|
142 | + */ |
|
143 | + public function getResourceIdentifier() |
|
144 | + { |
|
145 | + if ($this->resolvedPath !== null) { |
|
146 | + return $this->resolvedPath; |
|
147 | + } elseif (array_filter($this->getIncludePath()) == []) { |
|
148 | + return $this->file; |
|
149 | + } else { |
|
150 | + foreach ($this->getIncludePath() as $path) { |
|
151 | + $path = rtrim($path, DIRECTORY_SEPARATOR); |
|
152 | + if (file_exists($path . DIRECTORY_SEPARATOR . $this->file) === true) { |
|
153 | + return $this->resolvedPath = $path . DIRECTORY_SEPARATOR . $this->file; |
|
154 | + } |
|
155 | + } |
|
156 | 156 | |
157 | - throw new DwooException('Template "' . $this->file . '" could not be found in any of your include path(s)'); |
|
158 | - } |
|
159 | - } |
|
157 | + throw new DwooException('Template "' . $this->file . '" could not be found in any of your include path(s)'); |
|
158 | + } |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * Returns an unique value identifying the current version of this template, |
|
163 | - * in this case it's the unix timestamp of the last modification. |
|
164 | - * |
|
165 | - * @return string |
|
166 | - */ |
|
167 | - public function getUid() |
|
168 | - { |
|
169 | - return (string)filemtime($this->getResourceIdentifier()); |
|
170 | - } |
|
161 | + /** |
|
162 | + * Returns an unique value identifying the current version of this template, |
|
163 | + * in this case it's the unix timestamp of the last modification. |
|
164 | + * |
|
165 | + * @return string |
|
166 | + */ |
|
167 | + public function getUid() |
|
168 | + { |
|
169 | + return (string)filemtime($this->getResourceIdentifier()); |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * Returns a new template object from the given include name, null if no include is |
|
174 | - * possible (resource not found), or false if include is not permitted by this resource type. |
|
175 | - * |
|
176 | - * @param Core $core the dwoo instance requiring it |
|
177 | - * @param mixed $resourceId the filename (relative to this template's dir) of the template to |
|
178 | - * include |
|
179 | - * @param int $cacheTime duration of the cache validity for this template, if null it defaults |
|
180 | - * to the Dwoo instance that will render this template if null it |
|
181 | - * defaults to the Dwoo instance that will render this template if null |
|
182 | - * it defaults to the Dwoo instance that will render this template |
|
183 | - * @param string $cacheId the unique cache identifier of this page or anything else that makes |
|
184 | - * this template's content unique, if null it defaults to the current |
|
185 | - * url makes this template's content unique, if null it defaults to the |
|
186 | - * current url makes this template's content unique, if null it defaults |
|
187 | - * to the current url |
|
188 | - * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
189 | - * template from others, if null it defaults to the filename+bits of the |
|
190 | - * path template from others, if null it defaults to the filename+bits |
|
191 | - * of the path template from others, if null it defaults to the |
|
192 | - * filename+bits of the path |
|
193 | - * @param ITemplate $parentTemplate the template that is requesting a new template object (through an |
|
194 | - * include, extends or any other plugin) an include, extends or any |
|
195 | - * other plugin) an include, extends or any other plugin) |
|
196 | - * |
|
197 | - * @return TemplateFile|null |
|
198 | - * @throws DwooException |
|
199 | - * @throws SecurityException |
|
200 | - */ |
|
201 | - public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, |
|
202 | - $compileId = null, ITemplate $parentTemplate = null) |
|
203 | - { |
|
204 | - if (DIRECTORY_SEPARATOR === '\\') { |
|
205 | - $resourceId = str_replace(["\t", "\n", "\r", "\f", "\v"], [ |
|
206 | - '\\t', |
|
207 | - '\\n', |
|
208 | - '\\r', |
|
209 | - '\\f', |
|
210 | - '\\v' |
|
211 | - ], $resourceId); |
|
212 | - } |
|
213 | - $resourceId = strtr($resourceId, '\\', '/'); |
|
172 | + /** |
|
173 | + * Returns a new template object from the given include name, null if no include is |
|
174 | + * possible (resource not found), or false if include is not permitted by this resource type. |
|
175 | + * |
|
176 | + * @param Core $core the dwoo instance requiring it |
|
177 | + * @param mixed $resourceId the filename (relative to this template's dir) of the template to |
|
178 | + * include |
|
179 | + * @param int $cacheTime duration of the cache validity for this template, if null it defaults |
|
180 | + * to the Dwoo instance that will render this template if null it |
|
181 | + * defaults to the Dwoo instance that will render this template if null |
|
182 | + * it defaults to the Dwoo instance that will render this template |
|
183 | + * @param string $cacheId the unique cache identifier of this page or anything else that makes |
|
184 | + * this template's content unique, if null it defaults to the current |
|
185 | + * url makes this template's content unique, if null it defaults to the |
|
186 | + * current url makes this template's content unique, if null it defaults |
|
187 | + * to the current url |
|
188 | + * @param string $compileId the unique compiled identifier, which is used to distinguish this |
|
189 | + * template from others, if null it defaults to the filename+bits of the |
|
190 | + * path template from others, if null it defaults to the filename+bits |
|
191 | + * of the path template from others, if null it defaults to the |
|
192 | + * filename+bits of the path |
|
193 | + * @param ITemplate $parentTemplate the template that is requesting a new template object (through an |
|
194 | + * include, extends or any other plugin) an include, extends or any |
|
195 | + * other plugin) an include, extends or any other plugin) |
|
196 | + * |
|
197 | + * @return TemplateFile|null |
|
198 | + * @throws DwooException |
|
199 | + * @throws SecurityException |
|
200 | + */ |
|
201 | + public static function templateFactory(Core $core, $resourceId, $cacheTime = null, $cacheId = null, |
|
202 | + $compileId = null, ITemplate $parentTemplate = null) |
|
203 | + { |
|
204 | + if (DIRECTORY_SEPARATOR === '\\') { |
|
205 | + $resourceId = str_replace(["\t", "\n", "\r", "\f", "\v"], [ |
|
206 | + '\\t', |
|
207 | + '\\n', |
|
208 | + '\\r', |
|
209 | + '\\f', |
|
210 | + '\\v' |
|
211 | + ], $resourceId); |
|
212 | + } |
|
213 | + $resourceId = strtr($resourceId, '\\', '/'); |
|
214 | 214 | |
215 | - $includePath = null; |
|
215 | + $includePath = null; |
|
216 | 216 | |
217 | - if (file_exists($resourceId) === false) { |
|
218 | - if ($parentTemplate === null) { |
|
219 | - $parentTemplate = $core->getTemplate(); |
|
220 | - } |
|
221 | - if ($parentTemplate instanceof self) { |
|
222 | - if ($includePath = $parentTemplate->getIncludePath()) { |
|
223 | - if (strstr($resourceId, '../')) { |
|
224 | - throw new DwooException('When using an include path you can not reference a template into a parent directory (using ../)'); |
|
225 | - } |
|
226 | - } else { |
|
227 | - $resourceId = dirname($parentTemplate->getResourceIdentifier()) . DIRECTORY_SEPARATOR . $resourceId; |
|
228 | - if (file_exists($resourceId) === false) { |
|
229 | - return null; |
|
230 | - } |
|
231 | - } |
|
232 | - } else { |
|
233 | - return null; |
|
234 | - } |
|
235 | - } |
|
217 | + if (file_exists($resourceId) === false) { |
|
218 | + if ($parentTemplate === null) { |
|
219 | + $parentTemplate = $core->getTemplate(); |
|
220 | + } |
|
221 | + if ($parentTemplate instanceof self) { |
|
222 | + if ($includePath = $parentTemplate->getIncludePath()) { |
|
223 | + if (strstr($resourceId, '../')) { |
|
224 | + throw new DwooException('When using an include path you can not reference a template into a parent directory (using ../)'); |
|
225 | + } |
|
226 | + } else { |
|
227 | + $resourceId = dirname($parentTemplate->getResourceIdentifier()) . DIRECTORY_SEPARATOR . $resourceId; |
|
228 | + if (file_exists($resourceId) === false) { |
|
229 | + return null; |
|
230 | + } |
|
231 | + } |
|
232 | + } else { |
|
233 | + return null; |
|
234 | + } |
|
235 | + } |
|
236 | 236 | |
237 | - if ($policy = $core->getSecurityPolicy()) { |
|
238 | - while (true) { |
|
239 | - if (preg_match('{^([a-z]+?)://}i', $resourceId)) { |
|
240 | - throw new SecurityException('The security policy prevents you to read files from external sources : <em>' . $resourceId . '</em>.'); |
|
241 | - } |
|
237 | + if ($policy = $core->getSecurityPolicy()) { |
|
238 | + while (true) { |
|
239 | + if (preg_match('{^([a-z]+?)://}i', $resourceId)) { |
|
240 | + throw new SecurityException('The security policy prevents you to read files from external sources : <em>' . $resourceId . '</em>.'); |
|
241 | + } |
|
242 | 242 | |
243 | - if ($includePath) { |
|
244 | - break; |
|
245 | - } |
|
243 | + if ($includePath) { |
|
244 | + break; |
|
245 | + } |
|
246 | 246 | |
247 | - $resourceId = realpath($resourceId); |
|
248 | - $dirs = $policy->getAllowedDirectories(); |
|
249 | - foreach ($dirs as $dir => $dummy) { |
|
250 | - if (strpos($resourceId, $dir) === 0) { |
|
251 | - break 2; |
|
252 | - } |
|
253 | - } |
|
254 | - throw new SecurityException('The security policy prevents you to read <em>' . $resourceId . '</em>'); |
|
255 | - } |
|
256 | - } |
|
247 | + $resourceId = realpath($resourceId); |
|
248 | + $dirs = $policy->getAllowedDirectories(); |
|
249 | + foreach ($dirs as $dir => $dummy) { |
|
250 | + if (strpos($resourceId, $dir) === 0) { |
|
251 | + break 2; |
|
252 | + } |
|
253 | + } |
|
254 | + throw new SecurityException('The security policy prevents you to read <em>' . $resourceId . '</em>'); |
|
255 | + } |
|
256 | + } |
|
257 | 257 | |
258 | - $class = 'Dwoo\Template\File'; |
|
259 | - if ($parentTemplate) { |
|
260 | - $class = get_class($parentTemplate); |
|
261 | - } |
|
258 | + $class = 'Dwoo\Template\File'; |
|
259 | + if ($parentTemplate) { |
|
260 | + $class = get_class($parentTemplate); |
|
261 | + } |
|
262 | 262 | |
263 | - return new $class($resourceId, $cacheTime, $cacheId, $compileId, $includePath); |
|
264 | - } |
|
263 | + return new $class($resourceId, $cacheTime, $cacheId, $compileId, $includePath); |
|
264 | + } |
|
265 | 265 | |
266 | - /** |
|
267 | - * Returns some php code that will check if this template has been modified or not. |
|
268 | - * if the function returns null, the template will be instanciated and then the Uid checked |
|
269 | - * |
|
270 | - * @return string |
|
271 | - */ |
|
272 | - public function getIsModifiedCode() |
|
273 | - { |
|
274 | - return '"' . $this->getUid() . '" == filemtime(' . var_export($this->getResourceIdentifier(), true) . ')'; |
|
275 | - } |
|
266 | + /** |
|
267 | + * Returns some php code that will check if this template has been modified or not. |
|
268 | + * if the function returns null, the template will be instanciated and then the Uid checked |
|
269 | + * |
|
270 | + * @return string |
|
271 | + */ |
|
272 | + public function getIsModifiedCode() |
|
273 | + { |
|
274 | + return '"' . $this->getUid() . '" == filemtime(' . var_export($this->getResourceIdentifier(), true) . ')'; |
|
275 | + } |
|
276 | 276 | } |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | */ |
112 | 112 | protected function isValidCompiledFile($file) |
113 | 113 | { |
114 | - return parent::isValidCompiledFile($file) && (int)$this->getUid() <= filemtime($file); |
|
114 | + return parent::isValidCompiledFile($file) && (int) $this->getUid() <= filemtime($file); |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | /** |
@@ -149,12 +149,12 @@ discard block |
||
149 | 149 | } else { |
150 | 150 | foreach ($this->getIncludePath() as $path) { |
151 | 151 | $path = rtrim($path, DIRECTORY_SEPARATOR); |
152 | - if (file_exists($path . DIRECTORY_SEPARATOR . $this->file) === true) { |
|
153 | - return $this->resolvedPath = $path . DIRECTORY_SEPARATOR . $this->file; |
|
152 | + if (file_exists($path.DIRECTORY_SEPARATOR.$this->file) === true) { |
|
153 | + return $this->resolvedPath = $path.DIRECTORY_SEPARATOR.$this->file; |
|
154 | 154 | } |
155 | 155 | } |
156 | 156 | |
157 | - throw new DwooException('Template "' . $this->file . '" could not be found in any of your include path(s)'); |
|
157 | + throw new DwooException('Template "'.$this->file.'" could not be found in any of your include path(s)'); |
|
158 | 158 | } |
159 | 159 | } |
160 | 160 | |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | */ |
167 | 167 | public function getUid() |
168 | 168 | { |
169 | - return (string)filemtime($this->getResourceIdentifier()); |
|
169 | + return (string) filemtime($this->getResourceIdentifier()); |
|
170 | 170 | } |
171 | 171 | |
172 | 172 | /** |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | throw new DwooException('When using an include path you can not reference a template into a parent directory (using ../)'); |
225 | 225 | } |
226 | 226 | } else { |
227 | - $resourceId = dirname($parentTemplate->getResourceIdentifier()) . DIRECTORY_SEPARATOR . $resourceId; |
|
227 | + $resourceId = dirname($parentTemplate->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId; |
|
228 | 228 | if (file_exists($resourceId) === false) { |
229 | 229 | return null; |
230 | 230 | } |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | if ($policy = $core->getSecurityPolicy()) { |
238 | 238 | while (true) { |
239 | 239 | if (preg_match('{^([a-z]+?)://}i', $resourceId)) { |
240 | - throw new SecurityException('The security policy prevents you to read files from external sources : <em>' . $resourceId . '</em>.'); |
|
240 | + throw new SecurityException('The security policy prevents you to read files from external sources : <em>'.$resourceId.'</em>.'); |
|
241 | 241 | } |
242 | 242 | |
243 | 243 | if ($includePath) { |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | break 2; |
252 | 252 | } |
253 | 253 | } |
254 | - throw new SecurityException('The security policy prevents you to read <em>' . $resourceId . '</em>'); |
|
254 | + throw new SecurityException('The security policy prevents you to read <em>'.$resourceId.'</em>'); |
|
255 | 255 | } |
256 | 256 | } |
257 | 257 | |
@@ -271,6 +271,6 @@ discard block |
||
271 | 271 | */ |
272 | 272 | public function getIsModifiedCode() |
273 | 273 | { |
274 | - return '"' . $this->getUid() . '" == filemtime(' . var_export($this->getResourceIdentifier(), true) . ')'; |
|
274 | + return '"'.$this->getUid().'" == filemtime('.var_export($this->getResourceIdentifier(), true).')'; |
|
275 | 275 | } |
276 | 276 | } |