aboutsummaryrefslogtreecommitdiff
path: root/mod/minify/lib/min/utils.php
blob: c7359415207969498952a5b85d0ad635af1ff721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
 * Utility functions for generating group URIs in HTML files
 *
 * Before including this file, /min/lib must be in your include_path.
 * 
 * @package Minify
 */

require_once 'Minify/Build.php';


/**
 * Get a timestamped URI to a minified resource using the default Minify install
 *
 * <code>
 * <link rel="stylesheet" type="text/css" href="<?php echo Minify_groupUri('css'); ?>" />
 * <script type="text/javascript" src="<?php echo Minify_groupUri('js'); ?>"></script>
 * </code>
 *
 * If you do not want ampersands as HTML entities, set Minify_Build::$ampersand = "&" 
 * before using this function.
 *
 * @param string $group a key from groupsConfig.php
 * @param boolean $forceAmpersand (default false) Set to true if the RewriteRule
 * directives in .htaccess are functional. This will remove the "?" from URIs, making them
 * more cacheable by proxies.
 * @return string
 */ 
function Minify_groupUri($group, $forceAmpersand = false)
{
    $path = $forceAmpersand
        ? "/g={$group}"
        : "/?g={$group}";
    return _Minify_getBuild($group)->uri(
        '/' . basename(dirname(__FILE__)) . $path
        ,$forceAmpersand
    );
}


/**
 * Get the last modification time of the source js/css files used by Minify to
 * build the page.
 * 
 * If you're caching the output of Minify_groupUri(), you'll want to rebuild 
 * the cache if it's older than this timestamp.
 * 
 * <code>
 * // simplistic HTML cache system
 * $file = '/path/to/cache/file';
 * if (! file_exists($file) || filemtime($file) < Minify_groupsMtime(array('js', 'css'))) {
 *     // (re)build cache
 *     $page = buildPage(); // this calls Minify_groupUri() for js and css
 *     file_put_contents($file, $page);
 *     echo $page;
 *     exit();
 * }
 * readfile($file);
 * </code>
 *
 * @param array $groups an array of keys from groupsConfig.php
 * @return int Unix timestamp of the latest modification
 */ 
function Minify_groupsMtime($groups)
{
    $max = 0;
    foreach ((array)$groups as $group) {
        $max = max($max, _Minify_getBuild($group)->lastModified);
    }
    return $max;
}

/**
 * @param string $group a key from groupsConfig.php
 * @return Minify_Build
 * @private
 */
function _Minify_getBuild($group)
{
    static $builds = array();
    static $gc = false;
    if (false === $gc) {
        $gc = (require dirname(__FILE__) . '/groupsConfig.php');
    }
    if (! isset($builds[$group])) {
        $builds[$group] = new Minify_Build($gc[$group]);
    }
    return $builds[$group];
}