| 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 |
/** |
| 41 |
* Last modification time of all files in the build |
| 42 |
* |
| 43 |
* @var int |
| 44 |
*/ |
| 45 |
public $lastModified = 0; |
| 46 |
|
| 47 |
/** |
| 48 |
* String to use as ampersand in uri(). Set this to '&' if |
| 49 |
* you are not HTML-escaping URIs. |
| 50 |
* |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
public static $ampersand = '&'; |
| 54 |
|
| 55 |
/** |
| 56 |
* Get a time-stamped URI |
| 57 |
* |
| 58 |
* <code> |
| 59 |
* echo $b->uri('/site.js'); |
| 60 |
* // outputs "/site.js?1678242" |
| 61 |
* |
| 62 |
* echo $b->uri('/scriptaculous.js?load=effects'); |
| 63 |
* // outputs "/scriptaculous.js?load=effects&1678242" |
| 64 |
* </code> |
| 65 |
* |
| 66 |
* @param string $uri |
| 67 |
* @param boolean $forceAmpersand (default = false) Force the use of ampersand to |
| 68 |
* append the timestamp to the URI. |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public function uri($uri, $forceAmpersand = false) |
| 72 |
{ |
| 73 |
$sep = ($forceAmpersand || strpos($uri, '?') !== false) ? self::$ampersand : '?'; |
| 74 |
|
| 75 |
return "{$uri}{$sep}{$this->lastModified}"; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Create a build object |
| 80 |
* |
| 81 |
* @param array $sources array of Minify_Source objects and/or file paths |
| 82 |
* |
| 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->getLastModified()); |
| 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 |
} |
| 102 |
|