mirror of
https://github.com/mrclay/minify.git
synced 2025-08-18 11:51:27 +02:00
add CSS URL rewriting
This commit is contained in:
93
minify.php
93
minify.php
@@ -2,6 +2,8 @@
|
|||||||
/**
|
/**
|
||||||
* Minify - Combines, minifies, and caches JavaScript and CSS files on demand.
|
* Minify - Combines, minifies, and caches JavaScript and CSS files on demand.
|
||||||
*
|
*
|
||||||
|
* See http://code.google.com/p/minify/ for usage instructions.
|
||||||
|
*
|
||||||
* This library was inspired by jscsscomp by Maxim Martynyuk <flashkot@mail.ru>
|
* This library was inspired by jscsscomp by Maxim Martynyuk <flashkot@mail.ru>
|
||||||
* and by the article "Supercharged JavaScript" by Patrick Hunlock
|
* and by the article "Supercharged JavaScript" by Patrick Hunlock
|
||||||
* <wb@hunlock.com>.
|
* <wb@hunlock.com>.
|
||||||
@@ -16,7 +18,7 @@
|
|||||||
* @author Ryan Grove <ryan@wonko.com>
|
* @author Ryan Grove <ryan@wonko.com>
|
||||||
* @copyright 2007 Ryan Grove. All rights reserved.
|
* @copyright 2007 Ryan Grove. All rights reserved.
|
||||||
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
||||||
* @version 1.0.0 (2007-05-01)
|
* @version 1.0.0 (2007-05-02)
|
||||||
* @link http://code.google.com/p/minify/
|
* @link http://code.google.com/p/minify/
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -43,6 +45,27 @@ if (!defined('MINIFY_MAX_FILES')) {
|
|||||||
define('MINIFY_MAX_FILES', 16);
|
define('MINIFY_MAX_FILES', 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!defined('MINIFY_REWRITE_CSS_URLS')) {
|
||||||
|
/**
|
||||||
|
* Whether or not Minify should attempt to rewrite relative URLs used in CSS
|
||||||
|
* files so that they continue to point to the correct location after the file
|
||||||
|
* is combined and minified.
|
||||||
|
*
|
||||||
|
* Minify is pretty good at getting this right, but occasionally it can make
|
||||||
|
* mistakes. If you find that URL rewriting results in problems, you should
|
||||||
|
* disable it.
|
||||||
|
*/
|
||||||
|
define('MINIFY_REWRITE_CSS_URLS', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!defined('MINIFY_USE_CACHE')) {
|
||||||
|
/**
|
||||||
|
* Whether or not Minify should use a disk-based cache to increase
|
||||||
|
* performance.
|
||||||
|
*/
|
||||||
|
define('MINIFY_USE_CACHE', true);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Minify is a library for combining, minifying, and caching JavaScript and CSS
|
* Minify is a library for combining, minifying, and caching JavaScript and CSS
|
||||||
* files on demand before sending them to a web browser.
|
* files on demand before sending them to a web browser.
|
||||||
@@ -51,16 +74,15 @@ if (!defined('MINIFY_MAX_FILES')) {
|
|||||||
* @author Ryan Grove <ryan@wonko.com>
|
* @author Ryan Grove <ryan@wonko.com>
|
||||||
* @copyright 2007 Ryan Grove. All rights reserved.
|
* @copyright 2007 Ryan Grove. All rights reserved.
|
||||||
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
* @license http://opensource.org/licenses/bsd-license.php New BSD License
|
||||||
* @version 1.0.0 (2007-05-01)
|
* @version 1.0.0 (2007-05-02)
|
||||||
* @link http://code.google.com/p/minify/
|
* @link http://code.google.com/p/minify/
|
||||||
*/
|
*/
|
||||||
class Minify {
|
class Minify {
|
||||||
const TYPE_CSS = 'text/css';
|
const TYPE_CSS = 'text/css';
|
||||||
const TYPE_JS = 'text/javascript';
|
const TYPE_JS = 'text/javascript';
|
||||||
|
|
||||||
protected $files = array();
|
protected $files = array();
|
||||||
protected $type = TYPE_JS;
|
protected $type = TYPE_JS;
|
||||||
protected $useCache = true;
|
|
||||||
|
|
||||||
// -- Public Static Methods --------------------------------------------------
|
// -- Public Static Methods --------------------------------------------------
|
||||||
|
|
||||||
@@ -124,36 +146,59 @@ class Minify {
|
|||||||
/**
|
/**
|
||||||
* Minifies the specified CSS string and returns it.
|
* Minifies the specified CSS string and returns it.
|
||||||
*
|
*
|
||||||
* @param string $string CSS string
|
* @param string $css CSS string
|
||||||
* @return string minified string
|
* @return string minified string
|
||||||
* @see minify()
|
* @see minify()
|
||||||
* @see minifyJS()
|
* @see minifyJS()
|
||||||
*/
|
*/
|
||||||
protected static function minifyCSS($string) {
|
protected static function minifyCSS($css) {
|
||||||
// Compress whitespace.
|
// Compress whitespace.
|
||||||
$string = preg_replace('/\s+/', ' ', $string);
|
$css = preg_replace('/\s+/', ' ', $css);
|
||||||
|
|
||||||
// Remove comments.
|
// Remove comments.
|
||||||
$string = preg_replace('/\/\*.*?\*\//', '', $string);
|
$css = preg_replace('/\/\*.*?\*\//', '', $css);
|
||||||
|
|
||||||
return trim($string);
|
return trim($css);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Minifies the specified JavaScript string and returns it.
|
* Minifies the specified JavaScript string and returns it.
|
||||||
*
|
*
|
||||||
* @param string $string JavaScript string
|
* @param string $js JavaScript string
|
||||||
* @return string minified string
|
* @return string minified string
|
||||||
* @see minify()
|
* @see minify()
|
||||||
* @see minifyCSS()
|
* @see minifyCSS()
|
||||||
*/
|
*/
|
||||||
protected static function minifyJS($string) {
|
protected static function minifyJS($js) {
|
||||||
define('JSMIN_AS_LIB', true);
|
define('JSMIN_AS_LIB', true);
|
||||||
|
|
||||||
require_once dirname(__FILE__).'/lib/JSMin_lib.php';
|
require_once dirname(__FILE__).'/lib/JSMin_lib.php';
|
||||||
|
|
||||||
$jsMin = new JSMin($string, false);
|
$jsMin = new JSMin($js, false);
|
||||||
return $jsMin->minify();
|
return trim($jsMin->minify());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rewrites relative URLs in the specified CSS string to point to the correct
|
||||||
|
* location. URLs are assumed to be relative to the absolute path specified in
|
||||||
|
* the $path parameter.
|
||||||
|
*
|
||||||
|
* @param string $css CSS string
|
||||||
|
* @param string $path absolute path to which URLs are relative (should be a
|
||||||
|
* directory, not a file)
|
||||||
|
* @return string CSS string with rewritten URLs
|
||||||
|
*/
|
||||||
|
protected static function rewriteCSSUrls($css, $path) {
|
||||||
|
/*
|
||||||
|
Parentheses, commas, whitespace chars, single quotes, and double quotes are
|
||||||
|
escaped with a backslash as described in the CSS spec:
|
||||||
|
http://www.w3.org/TR/REC-CSS1#url
|
||||||
|
*/
|
||||||
|
$relativePath = preg_replace('/([\(\),\s\'"])/', '\\\$1',
|
||||||
|
str_replace(MINIFY_BASE_DIR, '', $path));
|
||||||
|
|
||||||
|
return preg_replace('/url\(\s*[\'"]?\/?(.+?)[\'"]?\s*\)/i', 'url('.
|
||||||
|
$relativePath.'/$1)', $css);
|
||||||
}
|
}
|
||||||
|
|
||||||
// -- Public Instance Methods ------------------------------------------------
|
// -- Public Instance Methods ------------------------------------------------
|
||||||
@@ -165,18 +210,15 @@ class Minify {
|
|||||||
* @param array|string $files filename or array of filenames to be minified
|
* @param array|string $files filename or array of filenames to be minified
|
||||||
* @param string $type content type of the specified files (either
|
* @param string $type content type of the specified files (either
|
||||||
* Minify::TYPE_CSS or Minify::TYPE_JS)
|
* Minify::TYPE_CSS or Minify::TYPE_JS)
|
||||||
* @param bool $useCache whether or not to use the disk-based cache
|
|
||||||
*/
|
*/
|
||||||
public function __construct($files = array(), $type = self::TYPE_JS,
|
public function __construct($files = array(), $type = self::TYPE_JS) {
|
||||||
$useCache = true) {
|
|
||||||
|
|
||||||
if ($type !== self::TYPE_JS && $type !== self::TYPE_CSS) {
|
if ($type !== self::TYPE_JS && $type !== self::TYPE_CSS) {
|
||||||
throw new MinifyInvalidArgumentException('Invalid argument ($type): '.
|
throw new MinifyInvalidArgumentException('Invalid argument ($type): '.
|
||||||
$type);
|
$type);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->type = $type;
|
$this->type = $type;
|
||||||
$this->useCache = (bool) $useCache;
|
|
||||||
|
|
||||||
if (count((array) $files)) {
|
if (count((array) $files)) {
|
||||||
$this->addFile($files);
|
$this->addFile($files);
|
||||||
@@ -249,7 +291,7 @@ class Minify {
|
|||||||
header("Last-Modified: $lastModifiedGMT");
|
header("Last-Modified: $lastModifiedGMT");
|
||||||
|
|
||||||
// Check the server-side cache.
|
// Check the server-side cache.
|
||||||
if ($this->useCache) {
|
if (MINIFY_USE_CACHE) {
|
||||||
$cacheFile = MINIFY_CACHE_DIR.'/minify_'.$hash;
|
$cacheFile = MINIFY_CACHE_DIR.'/minify_'.$hash;
|
||||||
|
|
||||||
if (is_file($cacheFile) && $lastModified <= filemtime($cacheFile)) {
|
if (is_file($cacheFile) && $lastModified <= filemtime($cacheFile)) {
|
||||||
@@ -273,14 +315,21 @@ class Minify {
|
|||||||
$combined = array();
|
$combined = array();
|
||||||
|
|
||||||
foreach($this->files as $file) {
|
foreach($this->files as $file) {
|
||||||
$combined[] = file_get_contents($file);
|
if ($this->type === self::TYPE_CSS && MINIFY_REWRITE_CSS_URLS) {
|
||||||
|
// Rewrite relative CSS URLs.
|
||||||
|
$combined[] = self::rewriteCSSUrls(file_get_contents($file),
|
||||||
|
dirname($file));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$combined[] = file_get_contents($file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$combined = $minify ? self::minify(implode("\n", $combined), $this->type) :
|
$combined = $minify ? self::minify(implode("\n", $combined), $this->type) :
|
||||||
implode("\n", $combined);
|
implode("\n", $combined);
|
||||||
|
|
||||||
// Save combined contents to the cache.
|
// Save combined contents to the cache.
|
||||||
if ($this->useCache) {
|
if (MINIFY_USE_CACHE) {
|
||||||
$cacheFile = MINIFY_CACHE_DIR.'/minify_'.$this->getHash();
|
$cacheFile = MINIFY_CACHE_DIR.'/minify_'.$this->getHash();
|
||||||
@file_put_contents($cacheFile, $combined, LOCK_EX);
|
@file_put_contents($cacheFile, $combined, LOCK_EX);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user