1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Marks the contents of the block as dynamic. Which means that it will not be cached. |
---|
5 | * |
---|
6 | * This software is provided 'as-is', without any express or implied warranty. |
---|
7 | * In no event will the authors be held liable for any damages arising from the use of this software. |
---|
8 | * |
---|
9 | * @author Jordi Boggiano <j.boggiano@seld.be> |
---|
10 | * @copyright Copyright (c) 2008, Jordi Boggiano |
---|
11 | * @license http://dwoo.org/LICENSE Modified BSD License |
---|
12 | * @link http://dwoo.org/ |
---|
13 | * @version 1.0.0 |
---|
14 | * @date 2008-10-23 |
---|
15 | * @package Dwoo |
---|
16 | */ |
---|
17 | class Dwoo_Plugin_dynamic extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block |
---|
18 | { |
---|
19 | public function init() |
---|
20 | { |
---|
21 | } |
---|
22 | |
---|
23 | public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type) |
---|
24 | { |
---|
25 | return ''; |
---|
26 | } |
---|
27 | |
---|
28 | public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content) |
---|
29 | { |
---|
30 | try { |
---|
31 | $compiler->findBlock('dynamic'); |
---|
32 | return $content; |
---|
33 | } catch (Dwoo_Compilation_Exception $e) { |
---|
34 | } |
---|
35 | $output = Dwoo_Compiler::PHP_OPEN . |
---|
36 | 'if($doCache) {'."\n\t". |
---|
37 | 'echo \'<dwoo:dynamic_\'.$dynamicId.\'>'. |
---|
38 | str_replace('\'', '\\\'', $content) . |
---|
39 | '</dwoo:dynamic_\'.$dynamicId.\'>\';'. |
---|
40 | "\n} else {\n\t"; |
---|
41 | if(substr($content, 0, strlen(Dwoo_Compiler::PHP_OPEN)) == Dwoo_Compiler::PHP_OPEN) { |
---|
42 | $output .= substr($content, strlen(Dwoo_Compiler::PHP_OPEN)); |
---|
43 | } else { |
---|
44 | $output .= Dwoo_Compiler::PHP_CLOSE . $content; |
---|
45 | } |
---|
46 | if(substr($output, -strlen(Dwoo_Compiler::PHP_CLOSE)) == Dwoo_Compiler::PHP_CLOSE) { |
---|
47 | $output = substr($output, 0, -strlen(Dwoo_Compiler::PHP_CLOSE)); |
---|
48 | } else { |
---|
49 | $output .= Dwoo_Compiler::PHP_OPEN; |
---|
50 | } |
---|
51 | $output .= "\n}". Dwoo_Compiler::PHP_CLOSE; |
---|
52 | |
---|
53 | return $output; |
---|
54 | } |
---|
55 | |
---|
56 | public static function unescape($output, $dynamicId, $compiledFile) |
---|
57 | { |
---|
58 | $output = preg_replace_callback('/<dwoo:dynamic_('.$dynamicId.')>(.+?)<\/dwoo:dynamic_'.$dynamicId.'>/s', array('self', 'unescapePhp'), $output, -1, $count); |
---|
59 | // re-add the includes on top of the file |
---|
60 | if ($count && preg_match('#/\* template head \*/(.+?)/\* end template head \*/#s', file_get_contents($compiledFile), $m)) { |
---|
61 | $output = '<?php '.$m[1].' ?>'.$output; |
---|
62 | } |
---|
63 | return $output; |
---|
64 | } |
---|
65 | |
---|
66 | public static function unescapePhp($match) |
---|
67 | { |
---|
68 | return preg_replace('{<\?php /\*'.$match[1].'\*/ echo \'(.+?)\'; \?>}s', '$1', $match[2]); |
---|
69 | } |
---|
70 | } |
---|