private function _getContent($file, $is_imported = false)
{
$file = realpath($file);
if (!$file || in_array($file, self::$filesIncluded) || false === ($content = @file_get_contents($file))) {
// file missing, already included, or failed read
return '';
}
self::$filesIncluded[] = realpath($file);
$this->_currentDir = dirname($file);
// remove UTF-8 BOM if present
if (pack("CCC", 0xef, 0xbb, 0xbf) === substr($content, 0, 3)) {
$content = substr($content, 3);
}
// ensure uniform EOLs
$content = str_replace("\r\n", "\n", $content);
// process @imports
$pattern = '/
@import\\s+
(?:url\\(\\s*)? # maybe url(
[\'"]? # maybe quote
(.*?) # 1 = URI
[\'"]? # maybe end quote
(?:\\s*\\))? # maybe )
([a-zA-Z,\\s]*)? # 2 = media list
; # end token
/x';
$content = preg_replace_callback($pattern, array($this, '_importCB'), $content);
// You only need to rework the import-path if the script is imported
if (self::$_isCss && $is_imported) {
// rewrite remaining relative URIs
$pattern = '/url\\(\\s*([^\\)\\s]+)\\s*\\)/';
$content = preg_replace_callback($pattern, array($this, '_urlCB'), $content);
}
return $this->_importedContent . $content;
}
private function _importCB($m) { $url = $m[1]; $mediaList = preg_replace('/\\s+/', '', $m[2]); if (strpos($url, '://') > 0) { // protocol, leave in place for CSS, comment for JS return self::$_isCss ? $m[0] : "/* Minify_ImportProcessor will not include remote content */"; } if ('/' === $url[0]) { // protocol-relative or root path $url = ltrim($url, '/'); $file = realpath($_SERVER['DOCUMENT_ROOT']) . DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR); } else { // relative to current path $file = $this->_currentDir . DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR); } $obj = new Minify_ImportProcessor(dirname($file)); $content = $obj->_getContent($file); if ('' === $content) { // failed. leave in place for CSS, comment for JS return self::$_isCss ? $m[0] : "/* Minify_ImportProcessor could not fetch '{$file}' */"; } return !self::$_isCss || preg_match('@(?:^$|\\ball\\b)@', $mediaList) ? $content : "@media {$mediaList} {\n{$content}\n}\n"; }