initial commit
[namibia] / public / min / lib / Minify / Build.php
1 <?php
2 /**
3  * Class Minify_Build  
4  * @package Minify
5  */
6
7 /**
8  * Maintain a single last modification time for a group of Minify sources to
9  * allow use of far off Expires headers in Minify.
10  * 
11  * <code>
12  * // in config file
13  * $groupSources = array(
14  *   'js' => array('file1.js', 'file2.js')
15  *   ,'css' => array('file1.css', 'file2.css', 'file3.css')
16  * )
17  * 
18  * // during HTML generation
19  * $jsBuild = new Minify_Build($groupSources['js']);
20  * $cssBuild = new Minify_Build($groupSources['css']);
21  * 
22  * $script = "<script type='text/javascript' src='"
23  *     . $jsBuild->uri('/min.php/js') . "'></script>";
24  * $link = "<link rel='stylesheet' type='text/css' href='"
25  *     . $cssBuild->uri('/min.php/css') . "'>";
26  * 
27  * // in min.php
28  * Minify::serve('Groups', array(
29  *   'groups' => $groupSources
30  *   ,'setExpires' => (time() + 86400 * 365)
31  * ));
32  * </code>
33  * 
34  * @package Minify
35  * @author Stephen Clay <steve@mrclay.org>
36  */
37 class Minify_Build {
38     
39     /**
40      * Last modification time of all files in the build
41      * 
42      * @var int 
43      */
44     public $lastModified = 0;
45     
46     /**
47      * String to use as ampersand in uri(). Set this to '&' if
48      * you are not HTML-escaping URIs.
49      *
50      * @var string
51      */
52     public static $ampersand = '&amp;';
53     
54     /**
55      * Get a time-stamped URI
56      * 
57      * <code>
58      * echo $b->uri('/site.js');
59      * // outputs "/site.js?1678242"
60      * 
61      * echo $b->uri('/scriptaculous.js?load=effects');
62      * // outputs "/scriptaculous.js?load=effects&amp1678242"
63      * </code>
64      *
65      * @param string $uri
66      * @param boolean $forceAmpersand (default = false) Force the use of ampersand to 
67      * append the timestamp to the URI.
68      * @return string
69      */
70     public function uri($uri, $forceAmpersand = false) {
71         $sep = ($forceAmpersand || strpos($uri, '?') !== false)
72             ? self::$ampersand
73             : '?';
74         return "{$uri}{$sep}{$this->lastModified}";
75     }
76
77         /**
78      * Create a build object
79      * 
80      * @param array $sources array of Minify_Source objects and/or file paths
81      * 
82      * @return null
83      */
84     public function __construct($sources) 
85     {
86         $max = 0;
87         foreach ((array)$sources as $source) {
88             if ($source instanceof Minify_Source) {
89                 $max = max($max, $source->lastModified);
90             } elseif (is_string($source)) {
91                 if (0 === strpos($source, '//')) {
92                     $source = $_SERVER['DOCUMENT_ROOT'] . substr($source, 1);
93                 }
94                 if (is_file($source)) {
95                     $max = max($max, filemtime($source));
96                 }
97             }
98         }
99         $this->lastModified = $max;
100     }
101 }