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