8 * A content source to be minified by Minify.
10 * This allows per-source minification options and the mixing of files with
11 * content from other sources.
14 * @author Stephen Clay <steve@mrclay.org>
19 * @var int time of last modification
21 public $lastModified = null;
24 * @var callback minifier function specifically for this source.
26 public $minifier = null;
29 * @var array minification options specific to this source.
31 public $minifyOptions = null;
34 * @var string full path of file
36 public $filepath = null;
39 * @var string HTTP Content Type (Minify requires one of the constants Minify::TYPE_*)
41 public $contentType = null;
44 * Create a Minify_Source
46 * In the $spec array(), you can either provide a 'filepath' to an existing
47 * file (existence will not be checked!) or give 'id' (unique string for
48 * the content), 'content' (the string content) and 'lastModified'
49 * (unixtime of last update).
51 * As a shortcut, the controller will replace "//" at the beginning
52 * of a filepath with $_SERVER['DOCUMENT_ROOT'] . '/'.
54 * @param array $spec options
56 public function __construct($spec)
58 if (isset($spec['filepath'])) {
59 if (0 === strpos($spec['filepath'], '//')) {
60 $spec['filepath'] = $_SERVER['DOCUMENT_ROOT'] . substr($spec['filepath'], 1);
62 $segments = explode('.', $spec['filepath']);
63 $ext = strtolower(array_pop($segments));
65 case 'js' : $this->contentType = 'application/x-javascript';
67 case 'css' : $this->contentType = 'text/css';
69 case 'htm' : // fallthrough
70 case 'html' : $this->contentType = 'text/html';
73 $this->filepath = $spec['filepath'];
74 $this->_id = $spec['filepath'];
75 $this->lastModified = filemtime($spec['filepath'])
76 // offset for Windows uploaders with out of sync clocks
77 + round(Minify::$uploaderHoursBehind * 3600);
78 } elseif (isset($spec['id'])) {
79 $this->_id = 'id::' . $spec['id'];
80 if (isset($spec['content'])) {
81 $this->_content = $spec['content'];
83 $this->_getContentFunc = $spec['getContentFunc'];
85 $this->lastModified = isset($spec['lastModified'])
86 ? $spec['lastModified']
89 if (isset($spec['contentType'])) {
90 $this->contentType = $spec['contentType'];
92 if (isset($spec['minifier'])) {
93 $this->minifier = $spec['minifier'];
95 if (isset($spec['minifyOptions'])) {
96 $this->minifyOptions = $spec['minifyOptions'];
105 public function getContent()
107 $content = (null !== $this->filepath)
108 ? file_get_contents($this->filepath)
109 : ((null !== $this->_content)
111 : call_user_func($this->_getContentFunc, $this->_id)
113 // remove UTF-8 BOM if present
114 return (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3))
115 ? substr($content, 3)
124 public function getId()
130 * Verifies a single minification call can handle all sources
132 * @param array $sources Minify_Source instances
134 * @return bool true iff there no sources with specific minifier preferences.
136 public static function haveNoMinifyPrefs($sources)
138 foreach ($sources as $source) {
139 if (null !== $source->minifier
140 || null !== $source->minifyOptions) {
148 * Get unique string for a set of sources
150 * @param array $sources Minify_Source instances
154 public static function getDigest($sources)
156 foreach ($sources as $source) {
158 $source->_id, $source->minifier, $source->minifyOptions
161 return md5(serialize($info));
165 * Get content type from a group of sources
167 * This is called if the user doesn't pass in a 'contentType' options
169 * @param array $sources Minify_Source instances
171 * @return string content type. e.g. 'text/css'
173 public static function getContentType($sources)
175 foreach ($sources as $source) {
176 if ($source->contentType !== null) {
177 return $source->contentType;
183 protected $_content = null;
184 protected $_getContentFunc = null;
185 protected $_id = null;