|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00016 class Minify_Source { 00017 00021 public $lastModified = null; 00022 00026 public $minifier = null; 00027 00031 public $minifyOptions = null; 00032 00036 public $filepath = null; 00037 00041 public $contentType = null; 00042 00056 public function __construct($spec) 00057 { 00058 if (isset($spec['filepath'])) { 00059 if (0 === strpos($spec['filepath'], '//')) { 00060 $spec['filepath'] = $_SERVER['DOCUMENT_ROOT'] . substr($spec['filepath'], 1); 00061 } 00062 $segments = explode('.', $spec['filepath']); 00063 $ext = strtolower(array_pop($segments)); 00064 switch ($ext) { 00065 case 'js' : $this->contentType = 'application/x-javascript'; 00066 break; 00067 case 'css' : $this->contentType = 'text/css'; 00068 break; 00069 case 'htm' : // fallthrough 00070 case 'html' : $this->contentType = 'text/html'; 00071 break; 00072 } 00073 $this->filepath = $spec['filepath']; 00074 $this->_id = $spec['filepath']; 00075 $this->lastModified = filemtime($spec['filepath']) 00076 // offset for Windows uploaders with out of sync clocks 00077 + round(Minify::$uploaderHoursBehind * 3600); 00078 } elseif (isset($spec['id'])) { 00079 $this->_id = 'id::' . $spec['id']; 00080 if (isset($spec['content'])) { 00081 $this->_content = $spec['content']; 00082 } else { 00083 $this->_getContentFunc = $spec['getContentFunc']; 00084 } 00085 $this->lastModified = isset($spec['lastModified']) 00086 ? $spec['lastModified'] 00087 : time(); 00088 } 00089 if (isset($spec['contentType'])) { 00090 $this->contentType = $spec['contentType']; 00091 } 00092 if (isset($spec['minifier'])) { 00093 $this->minifier = $spec['minifier']; 00094 } 00095 if (isset($spec['minifyOptions'])) { 00096 $this->minifyOptions = $spec['minifyOptions']; 00097 } 00098 } 00099 00105 public function getContent() 00106 { 00107 $content = (null !== $this->filepath) 00108 ? file_get_contents($this->filepath) 00109 : ((null !== $this->_content) 00110 ? $this->_content 00111 : call_user_func($this->_getContentFunc, $this->_id) 00112 ); 00113 // remove UTF-8 BOM if present 00114 return (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3)) 00115 ? substr($content, 3) 00116 : $content; 00117 } 00118 00124 public function getId() 00125 { 00126 return $this->_id; 00127 } 00128 00136 public static function haveNoMinifyPrefs($sources) 00137 { 00138 foreach ($sources as $source) { 00139 if (null !== $source->minifier 00140 || null !== $source->minifyOptions) { 00141 return false; 00142 } 00143 } 00144 return true; 00145 } 00146 00154 public static function getDigest($sources) 00155 { 00156 foreach ($sources as $source) { 00157 $info[] = array( 00158 $source->_id, $source->minifier, $source->minifyOptions 00159 ); 00160 } 00161 return md5(serialize($info)); 00162 } 00163 00173 public static function getContentType($sources) 00174 { 00175 foreach ($sources as $source) { 00176 if ($source->contentType !== null) { 00177 return $source->contentType; 00178 } 00179 } 00180 return 'text/plain'; 00181 } 00182 00183 protected $_content = null; 00184 protected $_getContentFunc = null; 00185 protected $_id = null; 00186 } 00187