Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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