Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
3 lars 1
<?php
2
 
3
define("ASSET_COMPILE_OUTPUT_DIR", BASE_PATH.'cache/asset_compile');
4
define("ASSET_COMPILE_URL_ROOT", '/cache/asset_compile');
5
//define("ASSET_COMPILE_OUTPUT_DIR", APP_ROOT.'/pcache/asset_compile');
6
//define("ASSET_COMPILE_URL_ROOT", '/assetcache');
7
//var_dump( ASSET_COMPILE_OUTPUT_DIR );
8
//define("DEBUG", true);
9
 
10
include_once(implode(DIRECTORY_SEPARATOR, array(dirname(__FILE__), 'sacy', 'sacy.php')));
11
 
12
function smarty_block_asset_compile($params, $content, &$smarty, &$repeat){
13
    if (!$repeat){
14
        // don't shoot me, but all tried using the dom-parser and removing elements
15
        // ended up with problems due to the annoying DOM API and braindead stuff
16
        // like no getDocumentElement() or getElementByID() not working even though
17
        // loadHTML clearly knows that the content is in fact HTML.
18
        //
19
        // So, let's go back to good old regexps :-)
20
 
21
        $cfg = new sacy_Config($params);
22
        if ($cfg->getDebugMode() == 1 ){
23
            return $content;
24
        }
25
 
26
        $tags = array('link', 'script');
27
        $tag_pattern = '#<\s*T\s+(.*)\s*(?:/>|>(.*)</T>)#Ui';
28
        $work = array();
29
        $aindex = 0;
30
 
31
        // first assembling all work. The idea is that, if sorted by descending
32
        // location offset, we can selectively remove tags.
33
        //
34
        // We'll need that to conditionally handle tags (like jTemplate's
35
        // <script type="text/html" that should remain)
36
        foreach($tags as $tag){
37
            $p = str_replace('T', preg_quote($tag), $tag_pattern);
38
            if(preg_match_all($p, $content, $ms, PREG_OFFSET_CAPTURE)){
39
                foreach($ms[1] as $i => $m)
40
                   $work[] = array($tag, $m[0], $ms[0][$i][1], $ms[0][$i][0], $ms[2][$i][0], $aindex++);
41
            }
42
        }
43
        // now sort task list by descending location offset
44
        // by the way: I want widespread 5.3 adoption for anonymous functions
45
        usort($work, create_function('$a,$b', 'if ($a[2] == $b[2]) return 0; return ($a[2] < $b[2]) ? 1 : -1;'));
46
        $ex = new sacy_FileExtractor($cfg);
47
        $files = array();
48
        $patched_content = $content;
49
        foreach($work as $unit){
50
            $r = $ex->extractFile($unit[0], $unit[1], $unit[4]);
51
            if ($r === false) continue; // handler has declined
52
            $r[] = $unit[5]; //append appearance order index
53
 
54
            // remove tag
55
            $patched_content = substr_replace($patched_content, '', $unit[2], strlen($unit[3]));
56
            $files[$unit[0]][] = $r;
57
        }
58
 
59
        $renderer = new sacy_CacheRenderer($cfg, $smarty);
60
        $rendered_content = "";
61
 
62
        // now put the files back in order of appearance in the original template
63
        foreach($files as $tag => &$f){
64
            usort($f, create_function('$a,$b', 'if ($a[2] == $b[2]) return 0; return ($a[2] > $b[2]) ? 1 : -1;'));
65
            $render = array();
66
            $curr_cat = $f[0][0];
67
            foreach($f as $fileentry){
68
                // the moment the category changes, render all we have so far
69
                // this makes it IMPERATIVE to keep links of the same category
70
                // together.
71
                if ($curr_cat != $fileentry[0]){
72
                    $res = $renderer->renderFiles($tag, $curr_cat, $render);
73
                    if ($res === false){
74
                        // rendering failed.
75
                        // because we don't know which one, we just enter emergency mode
76
                        // and return the initial content unharmed:
77
                        return $content;
78
                    }
79
                    // add redered stuff to patched content
80
                    $rendered_content .= $res;
81
                    $curr_cat = $fileentry[0];
82
                    $render = array($fileentry[1]);
83
                }else{
84
                    $render[] = $fileentry[1];
85
                }
86
            }
87
            $res = $renderer->renderFiles($tag, $curr_cat, $render);
88
            if ($res === false){
89
                // see last comment
90
                return $content;
91
            }
92
            $rendered_content .= $res;
93
        }
94
 
95
        return $rendered_content.$patched_content;
96
    }
97
}
98
 
99
?>