1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-10 16:14:18 +02:00

do some trivial codestyle fixes

unix newlines, trailing spaces
This commit is contained in:
Elan Ruusamäe
2016-01-22 00:30:38 +02:00
parent 90bf31f53b
commit 379feaba99
40 changed files with 1424 additions and 1327 deletions

View File

@@ -38,7 +38,7 @@ class DooDigestAuth{
* @param string $fail_url URL to be redirect if the User cancel the login * @param string $fail_url URL to be redirect if the User cancel the login
* @return string The username if login success. * @return string The username if login success.
*/ */
public static function http_auth($realm, $users, $fail_msg=NULL, $fail_url=NULL){ public static function http_auth($realm, $users, $fail_msg=NULL, $fail_url=NULL) {
$realm = "Restricted area - $realm"; $realm = "Restricted area - $realm";
//user => password //user => password
@@ -114,8 +114,8 @@ class DooDigestAuth{
$data['uri'] = $match[1]; $data['uri'] = $match[1];
$res = preg_match('/response=\"([^\"]+)\"/i', $txt, $match); $res = preg_match('/response=\"([^\"]+)\"/i', $txt, $match);
$data['response'] = $match[1]; $data['response'] = $match[1];
return $data; return $data;
} }
} }

View File

@@ -1,6 +1,6 @@
<?php <?php
/** /**
* Class HTTP_ConditionalGet * Class HTTP_ConditionalGet
* @package Minify * @package Minify
* @subpackage HTTP * @subpackage HTTP
*/ */
@@ -21,7 +21,7 @@
* } * }
* echo $content; * echo $content;
* </code> * </code>
* *
* E.g. Shortcut for the above * E.g. Shortcut for the above
* <code> * <code>
* HTTP_ConditionalGet::check($updateTime, true); // exits if client has cache * HTTP_ConditionalGet::check($updateTime, true); // exits if client has cache
@@ -40,7 +40,7 @@
* } * }
* echo $content; * echo $content;
* </code> * </code>
* *
* E.g. Static content with some static includes: * E.g. Static content with some static includes:
* <code> * <code>
* // before content * // before content
@@ -64,7 +64,7 @@ class HTTP_ConditionalGet {
/** /**
* Does the client have a valid copy of the requested resource? * Does the client have a valid copy of the requested resource?
* *
* You'll want to check this after instantiating the object. If true, do * You'll want to check this after instantiating the object. If true, do
* not send content, just call sendHeaders() if you haven't already. * not send content, just call sendHeaders() if you haven't already.
* *
@@ -74,36 +74,36 @@ class HTTP_ConditionalGet {
/** /**
* @param array $spec options * @param array $spec options
* *
* 'isPublic': (bool) if false, the Cache-Control header will contain * 'isPublic': (bool) if false, the Cache-Control header will contain
* "private", allowing only browser caching. (default false) * "private", allowing only browser caching. (default false)
* *
* 'lastModifiedTime': (int) if given, both ETag AND Last-Modified headers * 'lastModifiedTime': (int) if given, both ETag AND Last-Modified headers
* will be sent with content. This is recommended. * will be sent with content. This is recommended.
* *
* 'encoding': (string) if set, the header "Vary: Accept-Encoding" will * 'encoding': (string) if set, the header "Vary: Accept-Encoding" will
* always be sent and a truncated version of the encoding will be appended * always be sent and a truncated version of the encoding will be appended
* to the ETag. E.g. "pub123456;gz". This will also trigger a more lenient * to the ETag. E.g. "pub123456;gz". This will also trigger a more lenient
* checking of the client's If-None-Match header, as the encoding portion of * checking of the client's If-None-Match header, as the encoding portion of
* the ETag will be stripped before comparison. * the ETag will be stripped before comparison.
* *
* 'contentHash': (string) if given, only the ETag header can be sent with * 'contentHash': (string) if given, only the ETag header can be sent with
* content (only HTTP1.1 clients can conditionally GET). The given string * content (only HTTP1.1 clients can conditionally GET). The given string
* should be short with no quote characters and always change when the * should be short with no quote characters and always change when the
* resource changes (recommend md5()). This is not needed/used if * resource changes (recommend md5()). This is not needed/used if
* lastModifiedTime is given. * lastModifiedTime is given.
* *
* 'eTag': (string) if given, this will be used as the ETag header rather * 'eTag': (string) if given, this will be used as the ETag header rather
* than values based on lastModifiedTime or contentHash. Also the encoding * than values based on lastModifiedTime or contentHash. Also the encoding
* string will not be appended to the given value as described above. * string will not be appended to the given value as described above.
* *
* 'invalidate': (bool) if true, the client cache will be considered invalid * 'invalidate': (bool) if true, the client cache will be considered invalid
* without testing. Effectively this disables conditional GET. * without testing. Effectively this disables conditional GET.
* (default false) * (default false)
* *
* 'maxAge': (int) if given, this will set the Cache-Control max-age in * 'maxAge': (int) if given, this will set the Cache-Control max-age in
* seconds, and also set the Expires header to the equivalent GMT date. * seconds, and also set the Expires header to the equivalent GMT date.
* After the max-age period has passed, the browser will again send a * After the max-age period has passed, the browser will again send a
* conditional GET to revalidate its cache. * conditional GET to revalidate its cache.
*/ */
public function __construct($spec) public function __construct($spec)
@@ -113,7 +113,7 @@ class HTTP_ConditionalGet {
: 'private'; : 'private';
$maxAge = 0; $maxAge = 0;
// backwards compatibility (can be removed later) // backwards compatibility (can be removed later)
if (isset($spec['setExpires']) if (isset($spec['setExpires'])
&& is_numeric($spec['setExpires']) && is_numeric($spec['setExpires'])
&& ! isset($spec['maxAge'])) { && ! isset($spec['maxAge'])) {
$spec['maxAge'] = $spec['setExpires'] - $_SERVER['REQUEST_TIME']; $spec['maxAge'] = $spec['setExpires'] - $_SERVER['REQUEST_TIME'];
@@ -121,7 +121,7 @@ class HTTP_ConditionalGet {
if (isset($spec['maxAge'])) { if (isset($spec['maxAge'])) {
$maxAge = $spec['maxAge']; $maxAge = $spec['maxAge'];
$this->_headers['Expires'] = self::gmtDate( $this->_headers['Expires'] = self::gmtDate(
$_SERVER['REQUEST_TIME'] + $spec['maxAge'] $_SERVER['REQUEST_TIME'] + $spec['maxAge']
); );
} }
$etagAppend = ''; $etagAppend = '';
@@ -156,14 +156,14 @@ class HTTP_ConditionalGet {
? false ? false
: $this->_isCacheValid(); : $this->_isCacheValid();
} }
/** /**
* Get array of output headers to be sent * Get array of output headers to be sent
* *
* In the case of 304 responses, this array will only contain the response * In the case of 304 responses, this array will only contain the response
* code header: array('_responseCode' => 'HTTP/1.0 304 Not Modified') * code header: array('_responseCode' => 'HTTP/1.0 304 Not Modified')
* *
* Otherwise something like: * Otherwise something like:
* <code> * <code>
* array( * array(
* 'Cache-Control' => 'max-age=0, public' * 'Cache-Control' => 'max-age=0, public'
@@ -171,7 +171,7 @@ class HTTP_ConditionalGet {
* ) * )
* </code> * </code>
* *
* @return array * @return array
*/ */
public function getHeaders() public function getHeaders()
{ {
@@ -180,13 +180,13 @@ class HTTP_ConditionalGet {
/** /**
* Set the Content-Length header in bytes * Set the Content-Length header in bytes
* *
* With most PHP configs, as long as you don't flush() output, this method * With most PHP configs, as long as you don't flush() output, this method
* is not needed and PHP will buffer all output and set Content-Length for * is not needed and PHP will buffer all output and set Content-Length for
* you. Otherwise you'll want to call this to let the client know up front. * you. Otherwise you'll want to call this to let the client know up front.
* *
* @param int $bytes * @param int $bytes
* *
* @return int copy of input $bytes * @return int copy of input $bytes
*/ */
public function setContentLength($bytes) public function setContentLength($bytes)
@@ -196,9 +196,9 @@ class HTTP_ConditionalGet {
/** /**
* Send headers * Send headers
* *
* @see getHeaders() * @see getHeaders()
* *
* Note this doesn't "clear" the headers. Calling sendHeaders() will * Note this doesn't "clear" the headers. Calling sendHeaders() will
* call header() again (but probably have not effect) and getHeaders() will * call header() again (but probably have not effect) and getHeaders() will
* still return the headers. * still return the headers.
@@ -218,7 +218,7 @@ class HTTP_ConditionalGet {
header($name . ': ' . $val); header($name . ': ' . $val);
} }
} }
/** /**
* Exit if the client's cache is valid for this resource * Exit if the client's cache is valid for this resource
* *
@@ -227,8 +227,8 @@ class HTTP_ConditionalGet {
* @param int $lastModifiedTime if given, both ETag AND Last-Modified headers * @param int $lastModifiedTime if given, both ETag AND Last-Modified headers
* will be sent with content. This is recommended. * will be sent with content. This is recommended.
* *
* @param bool $isPublic (default false) if true, the Cache-Control header * @param bool $isPublic (default false) if true, the Cache-Control header
* will contain "public", allowing proxies to cache the content. Otherwise * will contain "public", allowing proxies to cache the content. Otherwise
* "private" will be sent, allowing only browser caching. * "private" will be sent, allowing only browser caching.
* *
* @param array $options (default empty) additional options for constructor * @param array $options (default empty) additional options for constructor
@@ -245,24 +245,23 @@ class HTTP_ConditionalGet {
exit(); exit();
} }
} }
/** /**
* Get a GMT formatted date for use in HTTP headers * Get a GMT formatted date for use in HTTP headers
* *
* <code> * <code>
* header('Expires: ' . HTTP_ConditionalGet::gmtdate($time)); * header('Expires: ' . HTTP_ConditionalGet::gmtdate($time));
* </code> * </code>
* *
* @param int $time unix timestamp * @param int $time unix timestamp
* *
* @return string * @return string
*/ */
public static function gmtDate($time) public static function gmtDate($time)
{ {
return gmdate('D, d M Y H:i:s \G\M\T', $time); return gmdate('D, d M Y H:i:s \G\M\T', $time);
} }
protected $_headers = array(); protected $_headers = array();
protected $_lmTime = null; protected $_lmTime = null;
protected $_etag = null; protected $_etag = null;
@@ -297,7 +296,7 @@ class HTTP_ConditionalGet {
{ {
if (null === $this->_etag) { if (null === $this->_etag) {
// lmTime is copied to ETag, so this condition implies that the // lmTime is copied to ETag, so this condition implies that the
// server sent neither ETag nor Last-Modified, so the client can't // server sent neither ETag nor Last-Modified, so the client can't
// possibly has a valid cache. // possibly has a valid cache.
return false; return false;
} }
@@ -305,6 +304,7 @@ class HTTP_ConditionalGet {
if ($isValid) { if ($isValid) {
$this->_headers['_responseCode'] = 'HTTP/1.0 304 Not Modified'; $this->_headers['_responseCode'] = 'HTTP/1.0 304 Not Modified';
} }
return $isValid; return $isValid;
} }
@@ -320,16 +320,18 @@ class HTTP_ConditionalGet {
? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])
: $_SERVER['HTTP_IF_NONE_MATCH']; : $_SERVER['HTTP_IF_NONE_MATCH'];
$clientEtags = explode(',', $clientEtagList); $clientEtags = explode(',', $clientEtagList);
$compareTo = $this->normalizeEtag($this->_etag); $compareTo = $this->normalizeEtag($this->_etag);
foreach ($clientEtags as $clientEtag) { foreach ($clientEtags as $clientEtag) {
if ($this->normalizeEtag($clientEtag) === $compareTo) { if ($this->normalizeEtag($clientEtag) === $compareTo) {
// respond with the client's matched ETag, even if it's not what // respond with the client's matched ETag, even if it's not what
// we would've sent by default // we would've sent by default
$this->_headers['ETag'] = trim($clientEtag); $this->_headers['ETag'] = trim($clientEtag);
return true; return true;
} }
} }
return false; return false;
} }
@@ -340,6 +342,7 @@ class HTTP_ConditionalGet {
*/ */
protected function normalizeEtag($etag) { protected function normalizeEtag($etag) {
$etag = trim($etag); $etag = trim($etag);
return $this->_stripEtag return $this->_stripEtag
? preg_replace('/;\\w\\w"$/', '"', $etag) ? preg_replace('/;\\w\\w"$/', '"', $etag)
: $etag; : $etag;
@@ -356,11 +359,13 @@ class HTTP_ConditionalGet {
// strip off IE's extra data (semicolon) // strip off IE's extra data (semicolon)
list($ifModifiedSince) = explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'], 2); list($ifModifiedSince) = explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'], 2);
if (strtotime($ifModifiedSince) >= $this->_lmTime) { if (strtotime($ifModifiedSince) >= $this->_lmTime) {
// Apache 2.2's behavior. If there was no ETag match, send the // Apache 2.2's behavior. If there was no ETag match, send the
// non-encoded version of the ETag value. // non-encoded version of the ETag value.
$this->_headers['ETag'] = $this->normalizeEtag($this->_etag); $this->_headers['ETag'] = $this->normalizeEtag($this->_etag);
return true; return true;
} }
return false; return false;
} }
} }

View File

@@ -1,14 +1,14 @@
<?php <?php
/** /**
* Class HTTP_Encoder * Class HTTP_Encoder
* @package Minify * @package Minify
* @subpackage HTTP * @subpackage HTTP
*/ */
/** /**
* Encode and send gzipped/deflated content * Encode and send gzipped/deflated content
* *
* The "Vary: Accept-Encoding" header is sent. If the client allows encoding, * The "Vary: Accept-Encoding" header is sent. If the client allows encoding,
* Content-Encoding and Content-Length are added. * Content-Encoding and Content-Length are added.
* *
* <code> * <code>
@@ -26,7 +26,7 @@
* header('Content-Type: text/css'); // needed if not HTML * header('Content-Type: text/css'); // needed if not HTML
* HTTP_Encoder::output($css); * HTTP_Encoder::output($css);
* </code> * </code>
* *
* <code> * <code>
* // Just sniff for the accepted encoding * // Just sniff for the accepted encoding
* $encoding = HTTP_Encoder::getAcceptedEncoding(); * $encoding = HTTP_Encoder::getAcceptedEncoding();
@@ -34,11 +34,11 @@
* *
* For more control over headers, use getHeaders() and getData() and send your * For more control over headers, use getHeaders() and getData() and send your
* own output. * own output.
* *
* Note: If you don't need header mgmt, use PHP's native gzencode, gzdeflate, * Note: If you don't need header mgmt, use PHP's native gzencode, gzdeflate,
* and gzcompress functions for gzip, deflate, and compress-encoding * and gzcompress functions for gzip, deflate, and compress-encoding
* respectively. * respectively.
* *
* @package Minify * @package Minify
* @subpackage HTTP * @subpackage HTTP
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
@@ -46,47 +46,45 @@
class HTTP_Encoder { class HTTP_Encoder {
/** /**
* Should the encoder allow HTTP encoding to IE6? * Should the encoder allow HTTP encoding to IE6?
* *
* If you have many IE6 users and the bandwidth savings is worth troubling * If you have many IE6 users and the bandwidth savings is worth troubling
* some of them, set this to true. * some of them, set this to true.
* *
* By default, encoding is only offered to IE7+. When this is true, * By default, encoding is only offered to IE7+. When this is true,
* getAcceptedEncoding() will return an encoding for IE6 if its user agent * getAcceptedEncoding() will return an encoding for IE6 if its user agent
* string contains "SV1". This has been documented in many places as "safe", * string contains "SV1". This has been documented in many places as "safe",
* but there seem to be remaining, intermittent encoding bugs in patched * but there seem to be remaining, intermittent encoding bugs in patched
* IE6 on the wild web. * IE6 on the wild web.
* *
* @var bool * @var bool
*/ */
public static $encodeToIe6 = true; public static $encodeToIe6 = true;
/** /**
* Default compression level for zlib operations * Default compression level for zlib operations
* *
* This level is used if encode() is not given a $compressionLevel * This level is used if encode() is not given a $compressionLevel
* *
* @var int * @var int
*/ */
public static $compressionLevel = 6; public static $compressionLevel = 6;
/** /**
* Get an HTTP Encoder object * Get an HTTP Encoder object
* *
* @param array $spec options * @param array $spec options
* *
* 'content': (string required) content to be encoded * 'content': (string required) content to be encoded
* *
* 'type': (string) if set, the Content-Type header will have this value. * 'type': (string) if set, the Content-Type header will have this value.
* *
* 'method: (string) only set this if you are forcing a particular encoding * 'method: (string) only set this if you are forcing a particular encoding
* method. If not set, the best method will be chosen by getAcceptedEncoding() * method. If not set, the best method will be chosen by getAcceptedEncoding()
* The available methods are 'gzip', 'deflate', 'compress', and '' (no * The available methods are 'gzip', 'deflate', 'compress', and '' (no
* encoding) * encoding)
*/ */
public function __construct($spec) public function __construct($spec)
{ {
$this->_useMbStrlen = (function_exists('mb_strlen') $this->_useMbStrlen = (function_exists('mb_strlen')
&& (ini_get('mbstring.func_overload') !== '') && (ini_get('mbstring.func_overload') !== '')
@@ -109,19 +107,19 @@ class HTTP_Encoder {
/** /**
* Get content in current form * Get content in current form
* *
* Call after encode() for encoded content. * Call after encode() for encoded content.
* *
* @return string * @return string
*/ */
public function getContent() public function getContent()
{ {
return $this->_content; return $this->_content;
} }
/** /**
* Get array of output headers to be sent * Get array of output headers to be sent
* *
* E.g. * E.g.
* <code> * <code>
* array( * array(
@@ -131,7 +129,7 @@ class HTTP_Encoder {
* ) * )
* </code> * </code>
* *
* @return array * @return array
*/ */
public function getHeaders() public function getHeaders()
{ {
@@ -140,11 +138,11 @@ class HTTP_Encoder {
/** /**
* Send output headers * Send output headers
* *
* You must call this before headers are sent and it probably cannot be * You must call this before headers are sent and it probably cannot be
* used in conjunction with zlib output buffering / mod_gzip. Errors are * used in conjunction with zlib output buffering / mod_gzip. Errors are
* not handled purposefully. * not handled purposefully.
* *
* @see getHeaders() * @see getHeaders()
*/ */
public function sendHeaders() public function sendHeaders()
@@ -153,10 +151,10 @@ class HTTP_Encoder {
header($name . ': ' . $val); header($name . ': ' . $val);
} }
} }
/** /**
* Send output headers and content * Send output headers and content
* *
* A shortcut for sendHeaders() and echo getContent() * A shortcut for sendHeaders() and echo getContent()
* *
* You must call this before headers are sent and it probably cannot be * You must call this before headers are sent and it probably cannot be
@@ -170,21 +168,21 @@ class HTTP_Encoder {
} }
/** /**
* Determine the client's best encoding method from the HTTP Accept-Encoding * Determine the client's best encoding method from the HTTP Accept-Encoding
* header. * header.
* *
* If no Accept-Encoding header is set, or the browser is IE before v6 SP2, * If no Accept-Encoding header is set, or the browser is IE before v6 SP2,
* this will return ('', ''), the "identity" encoding. * this will return ('', ''), the "identity" encoding.
* *
* A syntax-aware scan is done of the Accept-Encoding, so the method must * A syntax-aware scan is done of the Accept-Encoding, so the method must
* be non 0. The methods are favored in order of gzip, deflate, then * be non 0. The methods are favored in order of gzip, deflate, then
* compress. Deflate is always smallest and generally faster, but is * compress. Deflate is always smallest and generally faster, but is
* rarely sent by servers, so client support could be buggier. * rarely sent by servers, so client support could be buggier.
* *
* @param bool $allowCompress allow the older compress encoding * @param bool $allowCompress allow the older compress encoding
* *
* @param bool $allowDeflate allow the more recent deflate encoding * @param bool $allowDeflate allow the more recent deflate encoding
* *
* @return array two values, 1st is the actual encoding method, 2nd is the * @return array two values, 1st is the actual encoding method, 2nd is the
* alias of that method to use in the Content-Encoding header (some browsers * alias of that method to use in the Content-Encoding header (some browsers
* call gzip "x-gzip" etc.) * call gzip "x-gzip" etc.)
@@ -192,7 +190,7 @@ class HTTP_Encoder {
public static function getAcceptedEncoding($allowCompress = true, $allowDeflate = true) public static function getAcceptedEncoding($allowCompress = true, $allowDeflate = true)
{ {
// @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html // @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
if (! isset($_SERVER['HTTP_ACCEPT_ENCODING']) if (! isset($_SERVER['HTTP_ACCEPT_ENCODING'])
|| self::isBuggyIe()) || self::isBuggyIe())
{ {
@@ -213,7 +211,7 @@ class HTTP_Encoder {
return array('gzip', $m[1]); return array('gzip', $m[1]);
} }
if ($allowDeflate) { if ($allowDeflate) {
// deflate checks // deflate checks
$aeRev = strrev($ae); $aeRev = strrev($ae);
if (0 === strpos($aeRev, 'etalfed ,') // ie, webkit if (0 === strpos($aeRev, 'etalfed ,') // ie, webkit
|| 0 === strpos($aeRev, 'etalfed,') // gecko || 0 === strpos($aeRev, 'etalfed,') // gecko
@@ -230,24 +228,25 @@ class HTTP_Encoder {
,$m)) { ,$m)) {
return array('compress', $m[1]); return array('compress', $m[1]);
} }
return array('', ''); return array('', '');
} }
/** /**
* Encode (compress) the content * Encode (compress) the content
* *
* If the encode method is '' (none) or compression level is 0, or the 'zlib' * If the encode method is '' (none) or compression level is 0, or the 'zlib'
* extension isn't loaded, we return false. * extension isn't loaded, we return false.
* *
* Then the appropriate gz_* function is called to compress the content. If * Then the appropriate gz_* function is called to compress the content. If
* this fails, false is returned. * this fails, false is returned.
* *
* The header "Vary: Accept-Encoding" is added. If encoding is successful, * The header "Vary: Accept-Encoding" is added. If encoding is successful,
* the Content-Length header is updated, and Content-Encoding is also added. * the Content-Length header is updated, and Content-Encoding is also added.
* *
* @param int $compressionLevel given to zlib functions. If not given, the * @param int $compressionLevel given to zlib functions. If not given, the
* class default will be used. * class default will be used.
* *
* @return bool success true if the content was actually compressed * @return bool success true if the content was actually compressed
*/ */
public function encode($compressionLevel = null) public function encode($compressionLevel = null)
@@ -279,19 +278,20 @@ class HTTP_Encoder {
: (string)strlen($encoded); : (string)strlen($encoded);
$this->_headers['Content-Encoding'] = $this->_encodeMethod[1]; $this->_headers['Content-Encoding'] = $this->_encodeMethod[1];
$this->_content = $encoded; $this->_content = $encoded;
return true; return true;
} }
/** /**
* Encode and send appropriate headers and content * Encode and send appropriate headers and content
* *
* This is a convenience method for common use of the class * This is a convenience method for common use of the class
* *
* @param string $content * @param string $content
* *
* @param int $compressionLevel given to zlib functions. If not given, the * @param int $compressionLevel given to zlib functions. If not given, the
* class default will be used. * class default will be used.
* *
* @return bool success true if the content was actually compressed * @return bool success true if the content was actually compressed
*/ */
public static function output($content, $compressionLevel = null) public static function output($content, $compressionLevel = null)
@@ -302,6 +302,7 @@ class HTTP_Encoder {
$he = new HTTP_Encoder(array('content' => $content)); $he = new HTTP_Encoder(array('content' => $content));
$ret = $he->encode($compressionLevel); $ret = $he->encode($compressionLevel);
$he->sendAll(); $he->sendAll();
return $ret; return $ret;
} }
@@ -323,11 +324,12 @@ class HTTP_Encoder {
} }
// no regex = faaast // no regex = faaast
$version = (float)substr($ua, 30); $version = (float)substr($ua, 30);
return self::$encodeToIe6 return self::$encodeToIe6
? ($version < 6 || ($version == 6 && false === strpos($ua, 'SV1'))) ? ($version < 6 || ($version == 6 && false === strpos($ua, 'SV1')))
: ($version < 7); : ($version < 7);
} }
protected $_content = ''; protected $_content = '';
protected $_headers = array(); protected $_headers = array();
protected $_encodeMethod = array('', ''); protected $_encodeMethod = array('', '');

View File

@@ -1,9 +1,9 @@
<?php <?php
/** /**
* Class Minify * Class Minify
* @package Minify * @package Minify
*/ */
/** /**
* Minify - Combines, minifies, and caches JavaScript and CSS files on demand. * Minify - Combines, minifies, and caches JavaScript and CSS files on demand.
* *
@@ -23,7 +23,7 @@
* @link https://github.com/mrclay/minify * @link https://github.com/mrclay/minify
*/ */
class Minify { class Minify {
const VERSION = '3.0.0'; const VERSION = '3.0.0';
const TYPE_CSS = 'text/css'; const TYPE_CSS = 'text/css';
const TYPE_HTML = 'text/html'; const TYPE_HTML = 'text/html';
@@ -117,68 +117,68 @@ class Minify {
'importWarning' => "/* See https://github.com/mrclay/minify/blob/master/docs/CommonProblems.wiki.md#imports-can-appear-in-invalid-locations-in-combined-css-files */\n" 'importWarning' => "/* See https://github.com/mrclay/minify/blob/master/docs/CommonProblems.wiki.md#imports-can-appear-in-invalid-locations-in-combined-css-files */\n"
); );
} }
/** /**
* Serve a request for a minified file. * Serve a request for a minified file.
* *
* Here are the available options and defaults: * Here are the available options and defaults:
* *
* 'isPublic' : send "public" instead of "private" in Cache-Control * 'isPublic' : send "public" instead of "private" in Cache-Control
* headers, allowing shared caches to cache the output. (default true) * headers, allowing shared caches to cache the output. (default true)
* *
* 'quiet' : set to true to have serve() return an array rather than sending * 'quiet' : set to true to have serve() return an array rather than sending
* any headers/output (default false) * any headers/output (default false)
* *
* 'encodeOutput' : set to false to disable content encoding, and not send * 'encodeOutput' : set to false to disable content encoding, and not send
* the Vary header (default true) * the Vary header (default true)
* *
* 'encodeMethod' : generally you should let this be determined by * 'encodeMethod' : generally you should let this be determined by
* HTTP_Encoder (leave null), but you can force a particular encoding * HTTP_Encoder (leave null), but you can force a particular encoding
* to be returned, by setting this to 'gzip' or '' (no encoding) * to be returned, by setting this to 'gzip' or '' (no encoding)
* *
* 'encodeLevel' : level of encoding compression (0 to 9, default 9) * 'encodeLevel' : level of encoding compression (0 to 9, default 9)
* *
* 'contentTypeCharset' : appended to the Content-Type header sent. Set to a falsey * 'contentTypeCharset' : appended to the Content-Type header sent. Set to a falsey
* value to remove. (default 'utf-8') * value to remove. (default 'utf-8')
* *
* 'maxAge' : set this to the number of seconds the client should use its cache * 'maxAge' : set this to the number of seconds the client should use its cache
* before revalidating with the server. This sets Cache-Control: max-age and the * before revalidating with the server. This sets Cache-Control: max-age and the
* Expires header. Unlike the old 'setExpires' setting, this setting will NOT * Expires header. Unlike the old 'setExpires' setting, this setting will NOT
* prevent conditional GETs. Note this has nothing to do with server-side caching. * prevent conditional GETs. Note this has nothing to do with server-side caching.
* *
* 'rewriteCssUris' : If true, serve() will automatically set the 'currentDir' * 'rewriteCssUris' : If true, serve() will automatically set the 'currentDir'
* minifier option to enable URI rewriting in CSS files (default true) * minifier option to enable URI rewriting in CSS files (default true)
* *
* 'bubbleCssImports' : If true, all @import declarations in combined CSS * 'bubbleCssImports' : If true, all @import declarations in combined CSS
* files will be move to the top. Note this may alter effective CSS values * files will be move to the top. Note this may alter effective CSS values
* due to a change in order. (default false) * due to a change in order. (default false)
* *
* 'debug' : set to true to minify all sources with the 'Lines' controller, which * 'debug' : set to true to minify all sources with the 'Lines' controller, which
* eases the debugging of combined files. This also prevents 304 responses. * eases the debugging of combined files. This also prevents 304 responses.
* @see Minify_Lines::minify() * @see Minify_Lines::minify()
* *
* 'concatOnly' : set to true to disable minification and simply concatenate the files. * 'concatOnly' : set to true to disable minification and simply concatenate the files.
* For JS, no minifier will be used. For CSS, only URI rewriting is still performed. * For JS, no minifier will be used. For CSS, only URI rewriting is still performed.
* *
* 'minifiers' : to override Minify's default choice of minifier function for * 'minifiers' : to override Minify's default choice of minifier function for
* a particular content-type, specify your callback under the key of the * a particular content-type, specify your callback under the key of the
* content-type: * content-type:
* <code> * <code>
* // call customCssMinifier($css) for all CSS minification * // call customCssMinifier($css) for all CSS minification
* $options['minifiers'][Minify::TYPE_CSS] = 'customCssMinifier'; * $options['minifiers'][Minify::TYPE_CSS] = 'customCssMinifier';
* *
* // don't minify Javascript at all * // don't minify Javascript at all
* $options['minifiers'][Minify::TYPE_JS] = ''; * $options['minifiers'][Minify::TYPE_JS] = '';
* </code> * </code>
* *
* 'minifierOptions' : to send options to the minifier function, specify your options * 'minifierOptions' : to send options to the minifier function, specify your options
* under the key of the content-type. E.g. To send the CSS minifier an option: * under the key of the content-type. E.g. To send the CSS minifier an option:
* <code> * <code>
* // give CSS minifier array('optionName' => 'optionValue') as 2nd argument * // give CSS minifier array('optionName' => 'optionValue') as 2nd argument
* $options['minifierOptions'][Minify::TYPE_CSS]['optionName'] = 'optionValue'; * $options['minifierOptions'][Minify::TYPE_CSS]['optionName'] = 'optionValue';
* </code> * </code>
* *
* 'contentType' : (optional) this is only needed if your file extension is not * 'contentType' : (optional) this is only needed if your file extension is not
* js/css/html. The given content-type will be sent regardless of source file * js/css/html. The given content-type will be sent regardless of source file
* extension, so this should not be used in a Groups config with other * extension, so this should not be used in a Groups config with other
* Javascript/CSS files. * Javascript/CSS files.
@@ -186,13 +186,13 @@ class Minify {
* 'importWarning' : serve() will check CSS files for @import declarations that * 'importWarning' : serve() will check CSS files for @import declarations that
* appear too late in the combined stylesheet. If found, serve() will prepend * appear too late in the combined stylesheet. If found, serve() will prepend
* the output with this warning. To disable this, set this option to empty string. * the output with this warning. To disable this, set this option to empty string.
* *
* Any controller options are documented in that controller's createConfiguration() method. * Any controller options are documented in that controller's createConfiguration() method.
* *
* @param Minify_ControllerInterface $controller instance of subclass of Minify_Controller_Base * @param Minify_ControllerInterface $controller instance of subclass of Minify_Controller_Base
* *
* @param array $options controller/serve options * @param array $options controller/serve options
* *
* @return null|array if the 'quiet' option is set to true, an array * @return null|array if the 'quiet' option is set to true, an array
* with keys "success" (bool), "statusCode" (int), "content" (string), and * with keys "success" (bool), "statusCode" (int), "content" (string), and
* "headers" (array). * "headers" (array).
@@ -216,6 +216,7 @@ class Minify {
$this->errorExit($this->options['badRequestHeader'], self::URL_DEBUG); $this->errorExit($this->options['badRequestHeader'], self::URL_DEBUG);
} else { } else {
list(,$statusCode) = explode(' ', $this->options['badRequestHeader']); list(,$statusCode) = explode(' ', $this->options['badRequestHeader']);
return array( return array(
'success' => false, 'success' => false,
'statusCode' => (int)$statusCode, 'statusCode' => (int)$statusCode,
@@ -224,14 +225,14 @@ class Minify {
); );
} }
} }
$this->controller = $controller; $this->controller = $controller;
if ($this->options['debug']) { if ($this->options['debug']) {
$this->setupDebug(); $this->setupDebug();
$this->options['maxAge'] = 0; $this->options['maxAge'] = 0;
} }
// determine encoding // determine encoding
if ($this->options['encodeOutput']) { if ($this->options['encodeOutput']) {
$sendVary = true; $sendVary = true;
@@ -249,7 +250,7 @@ class Minify {
} else { } else {
$this->options['encodeMethod'] = ''; // identity (no encoding) $this->options['encodeMethod'] = ''; // identity (no encoding)
} }
// check client cache // check client cache
$cgOptions = array( $cgOptions = array(
'lastModifiedTime' => $this->options['lastModifiedTime'], 'lastModifiedTime' => $this->options['lastModifiedTime'],
@@ -268,6 +269,7 @@ class Minify {
// client's cache is valid // client's cache is valid
if (! $this->options['quiet']) { if (! $this->options['quiet']) {
$cg->sendHeaders(); $cg->sendHeaders();
return; return;
} else { } else {
return array( return array(
@@ -282,7 +284,7 @@ class Minify {
$headers = $cg->getHeaders(); $headers = $cg->getHeaders();
unset($cg); unset($cg);
} }
if ($this->options['contentType'] === self::TYPE_CSS && $this->options['rewriteCssUris']) { if ($this->options['contentType'] === self::TYPE_CSS && $this->options['rewriteCssUris']) {
$this->setupUriRewrites(); $this->setupUriRewrites();
} }
@@ -300,20 +302,20 @@ class Minify {
} }
} }
} }
// check server cache // check server cache
if (! $this->options['debug']) { if (! $this->options['debug']) {
// using cache // using cache
// the goal is to use only the cache methods to sniff the length and // the goal is to use only the cache methods to sniff the length and
// output the content, as they do not require ever loading the file into // output the content, as they do not require ever loading the file into
// memory. // memory.
$cacheId = $this->_getCacheId(); $cacheId = $this->_getCacheId();
$fullCacheId = ($this->options['encodeMethod']) ? $cacheId . '.gz' : $cacheId; $fullCacheId = ($this->options['encodeMethod']) ? $cacheId . '.gz' : $cacheId;
// check cache for valid entry // check cache for valid entry
$cacheIsReady = $this->cache->isValid($fullCacheId, $this->options['lastModifiedTime']); $cacheIsReady = $this->cache->isValid($fullCacheId, $this->options['lastModifiedTime']);
if ($cacheIsReady) { if ($cacheIsReady) {
$cacheContentLength = $this->cache->getSize($fullCacheId); $cacheContentLength = $this->cache->getSize($fullCacheId);
} else { } else {
// generate & cache content // generate & cache content
try { try {
@@ -347,7 +349,7 @@ class Minify {
// still need to encode // still need to encode
$content = gzencode($content, $this->options['encodeLevel']); $content = gzencode($content, $this->options['encodeLevel']);
} }
// add headers // add headers
if ($cacheIsReady) { if ($cacheIsReady) {
$headers['Content-Length'] = $cacheContentLength; $headers['Content-Length'] = $cacheContentLength;
@@ -395,11 +397,11 @@ class Minify {
* Return combined minified content for a set of sources * Return combined minified content for a set of sources
* *
* No internal caching will be used and the content will not be HTTP encoded. * No internal caching will be used and the content will not be HTTP encoded.
* *
* @param array $sources array of filepaths and/or Minify_Source objects * @param array $sources array of filepaths and/or Minify_Source objects
* *
* @param array $options (optional) array of options for serve. * @param array $options (optional) array of options for serve.
* *
* @return string * @return string
*/ */
public function combine($sources, $options = array()) public function combine($sources, $options = array())
@@ -422,6 +424,7 @@ class Minify {
$out = $this->serve($controller, $options); $out = $this->serve($controller, $options);
$this->cache = $tmpCache; $this->cache = $tmpCache;
return $out['content']; return $out['content'];
} }
@@ -487,7 +490,7 @@ class Minify {
)); ));
} }
} }
/** /**
* Combines sources and minifies the result. * Combines sources and minifies the result.
* *
@@ -498,7 +501,7 @@ class Minify {
protected function combineMinify() protected function combineMinify()
{ {
$type = $this->options['contentType']; // ease readability $type = $this->options['contentType']; // ease readability
// when combining scripts, make sure all statements separated and // when combining scripts, make sure all statements separated and
// trailing single line comment is terminated // trailing single line comment is terminated
$implodeSeparator = ($type === self::TYPE_JS) ? "\n;" : ''; $implodeSeparator = ($type === self::TYPE_JS) ? "\n;" : '';
@@ -573,11 +576,11 @@ class Minify {
} while ($source); } while ($source);
$content = implode($implodeSeparator, $content); $content = implode($implodeSeparator, $content);
if ($type === self::TYPE_CSS && false !== strpos($content, '@import')) { if ($type === self::TYPE_CSS && false !== strpos($content, '@import')) {
$content = $this->handleCssImports($content); $content = $this->handleCssImports($content);
} }
// do any post-processing (esp. for editing build URIs) // do any post-processing (esp. for editing build URIs)
if ($this->options['postprocessorRequire']) { if ($this->options['postprocessorRequire']) {
require_once $this->options['postprocessorRequire']; require_once $this->options['postprocessorRequire'];
@@ -585,13 +588,14 @@ class Minify {
if ($this->options['postprocessor']) { if ($this->options['postprocessor']) {
$content = call_user_func($this->options['postprocessor'], $content, $type); $content = call_user_func($this->options['postprocessor'], $content, $type);
} }
return $content; return $content;
} }
/** /**
* Make a unique cache id for for this request. * Make a unique cache id for for this request.
* *
* Any settings that could affect output are taken into consideration * Any settings that could affect output are taken into consideration
* *
* @param string $prefix * @param string $prefix
* *
@@ -610,9 +614,10 @@ class Minify {
$this->options['bubbleCssImports'], $this->options['bubbleCssImports'],
Minify::VERSION, Minify::VERSION,
))); )));
return "{$prefix}_{$name}_{$md5}"; return "{$prefix}_{$name}_{$md5}";
} }
/** /**
* Bubble CSS @imports to the top or prepend a warning if an import is detected not at the top. * Bubble CSS @imports to the top or prepend a warning if an import is detected not at the top.
* *
@@ -626,6 +631,7 @@ class Minify {
// bubble CSS imports // bubble CSS imports
preg_match_all('/@import.*?;/', $css, $imports); preg_match_all('/@import.*?;/', $css, $imports);
$css = implode('', $imports[0]) . preg_replace('/@import.*?;/', '', $css); $css = implode('', $imports[0]) . preg_replace('/@import.*?;/', '', $css);
return $css; return $css;
} }
@@ -644,6 +650,7 @@ class Minify {
// { appears before @import : prepend warning // { appears before @import : prepend warning
$css = $this->options['importWarning'] . $css; $css = $this->options['importWarning'] . $css;
} }
return $css; return $css;
} }
@@ -672,6 +679,7 @@ class Minify {
Minify_Logger::log('ContentType mismatch'); Minify_Logger::log('ContentType mismatch');
$this->sources = array(); $this->sources = array();
return $options; return $options;
} }
@@ -686,6 +694,7 @@ class Minify {
Minify_Logger::log('ContentType mismatch'); Minify_Logger::log('ContentType mismatch');
$this->sources = array(); $this->sources = array();
return $options; return $options;
} }
} }

View File

@@ -1,48 +1,48 @@
<?php <?php
/** /**
* Class Minify_Build * Class Minify_Build
* @package Minify * @package Minify
*/ */
/** /**
* Maintain a single last modification time for a group of Minify sources to * Maintain a single last modification time for a group of Minify sources to
* allow use of far off Expires headers in Minify. * allow use of far off Expires headers in Minify.
* *
* <code> * <code>
* // in config file * // in config file
* $groupSources = array( * $groupSources = array(
* 'js' => array('file1.js', 'file2.js') * 'js' => array('file1.js', 'file2.js')
* ,'css' => array('file1.css', 'file2.css', 'file3.css') * ,'css' => array('file1.css', 'file2.css', 'file3.css')
* ) * )
* *
* // during HTML generation * // during HTML generation
* $jsBuild = new Minify_Build($groupSources['js']); * $jsBuild = new Minify_Build($groupSources['js']);
* $cssBuild = new Minify_Build($groupSources['css']); * $cssBuild = new Minify_Build($groupSources['css']);
* *
* $script = "<script type='text/javascript' src='" * $script = "<script type='text/javascript' src='"
* . $jsBuild->uri('/min.php/js') . "'></script>"; * . $jsBuild->uri('/min.php/js') . "'></script>";
* $link = "<link rel='stylesheet' type='text/css' href='" * $link = "<link rel='stylesheet' type='text/css' href='"
* . $cssBuild->uri('/min.php/css') . "'>"; * . $cssBuild->uri('/min.php/css') . "'>";
* *
* // in min.php * // in min.php
* Minify::serve('Groups', array( * Minify::serve('Groups', array(
* 'groups' => $groupSources * 'groups' => $groupSources
* ,'setExpires' => (time() + 86400 * 365) * ,'setExpires' => (time() + 86400 * 365)
* )); * ));
* </code> * </code>
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */
class Minify_Build { class Minify_Build {
/** /**
* Last modification time of all files in the build * Last modification time of all files in the build
* *
* @var int * @var int
*/ */
public $lastModified = 0; public $lastModified = 0;
/** /**
* String to use as ampersand in uri(). Set this to '&' if * String to use as ampersand in uri(). Set this to '&' if
* you are not HTML-escaping URIs. * you are not HTML-escaping URIs.
@@ -50,20 +50,20 @@ class Minify_Build {
* @var string * @var string
*/ */
public static $ampersand = '&amp;'; public static $ampersand = '&amp;';
/** /**
* Get a time-stamped URI * Get a time-stamped URI
* *
* <code> * <code>
* echo $b->uri('/site.js'); * echo $b->uri('/site.js');
* // outputs "/site.js?1678242" * // outputs "/site.js?1678242"
* *
* echo $b->uri('/scriptaculous.js?load=effects'); * echo $b->uri('/scriptaculous.js?load=effects');
* // outputs "/scriptaculous.js?load=effects&amp1678242" * // outputs "/scriptaculous.js?load=effects&amp1678242"
* </code> * </code>
* *
* @param string $uri * @param string $uri
* @param boolean $forceAmpersand (default = false) Force the use of ampersand to * @param boolean $forceAmpersand (default = false) Force the use of ampersand to
* append the timestamp to the URI. * append the timestamp to the URI.
* @return string * @return string
*/ */
@@ -71,17 +71,18 @@ class Minify_Build {
$sep = ($forceAmpersand || strpos($uri, '?') !== false) $sep = ($forceAmpersand || strpos($uri, '?') !== false)
? self::$ampersand ? self::$ampersand
: '?'; : '?';
return "{$uri}{$sep}{$this->lastModified}"; return "{$uri}{$sep}{$this->lastModified}";
} }
/** /**
* Create a build object * Create a build object
* *
* @param array $sources array of Minify_Source objects and/or file paths * @param array $sources array of Minify_Source objects and/or file paths
* *
* @return null * @return null
*/ */
public function __construct($sources) public function __construct($sources)
{ {
$max = 0; $max = 0;
foreach ((array)$sources as $source) { foreach ((array)$sources as $source) {

View File

@@ -1,101 +1,101 @@
<?php <?php
/** /**
* Class Minify_CSS * Class Minify_CSS
* @package Minify * @package Minify
*/ */
/** /**
* Minify CSS * Minify CSS
* *
* This class uses Minify_CSS_Compressor and Minify_CSS_UriRewriter to * This class uses Minify_CSS_Compressor and Minify_CSS_UriRewriter to
* minify CSS and rewrite relative URIs. * minify CSS and rewrite relative URIs.
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
* @author http://code.google.com/u/1stvamp/ (Issue 64 patch) * @author http://code.google.com/u/1stvamp/ (Issue 64 patch)
* *
* @deprecated Use Minify_CSSmin * @deprecated Use Minify_CSSmin
*/ */
class Minify_CSS { class Minify_CSS {
/** /**
* Minify a CSS string * Minify a CSS string
* *
* @param string $css * @param string $css
* *
* @param array $options available options: * @param array $options available options:
* *
* 'preserveComments': (default true) multi-line comments that begin * 'preserveComments': (default true) multi-line comments that begin
* with "/*!" will be preserved with newlines before and after to * with "/*!" will be preserved with newlines before and after to
* enhance readability. * enhance readability.
* *
* 'removeCharsets': (default true) remove all @charset at-rules * 'removeCharsets': (default true) remove all @charset at-rules
* *
* 'prependRelativePath': (default null) if given, this string will be * 'prependRelativePath': (default null) if given, this string will be
* prepended to all relative URIs in import/url declarations * prepended to all relative URIs in import/url declarations
* *
* 'currentDir': (default null) if given, this is assumed to be the * 'currentDir': (default null) if given, this is assumed to be the
* directory of the current CSS file. Using this, minify will rewrite * directory of the current CSS file. Using this, minify will rewrite
* all relative URIs in import/url declarations to correctly point to * all relative URIs in import/url declarations to correctly point to
* the desired files. For this to work, the files *must* exist and be * the desired files. For this to work, the files *must* exist and be
* visible by the PHP process. * visible by the PHP process.
* *
* 'symlinks': (default = array()) If the CSS file is stored in * 'symlinks': (default = array()) If the CSS file is stored in
* a symlink-ed directory, provide an array of link paths to * a symlink-ed directory, provide an array of link paths to
* target paths, where the link paths are within the document root. Because * target paths, where the link paths are within the document root. Because
* paths need to be normalized for this to work, use "//" to substitute * paths need to be normalized for this to work, use "//" to substitute
* the doc root in the link paths (the array keys). E.g.: * the doc root in the link paths (the array keys). E.g.:
* <code> * <code>
* array('//symlink' => '/real/target/path') // unix * array('//symlink' => '/real/target/path') // unix
* array('//static' => 'D:\\staticStorage') // Windows * array('//static' => 'D:\\staticStorage') // Windows
* </code> * </code>
* *
* 'docRoot': (default = $_SERVER['DOCUMENT_ROOT']) * 'docRoot': (default = $_SERVER['DOCUMENT_ROOT'])
* see Minify_CSS_UriRewriter::rewrite * see Minify_CSS_UriRewriter::rewrite
* *
* @return string * @return string
*/ */
public static function minify($css, $options = array()) public static function minify($css, $options = array())
{ {
$options = array_merge(array( $options = array_merge(array(
'compress' => true, 'compress' => true,
'removeCharsets' => true, 'removeCharsets' => true,
'preserveComments' => true, 'preserveComments' => true,
'currentDir' => null, 'currentDir' => null,
'docRoot' => $_SERVER['DOCUMENT_ROOT'], 'docRoot' => $_SERVER['DOCUMENT_ROOT'],
'prependRelativePath' => null, 'prependRelativePath' => null,
'symlinks' => array(), 'symlinks' => array(),
), $options); ), $options);
if ($options['removeCharsets']) { if ($options['removeCharsets']) {
$css = preg_replace('/@charset[^;]+;\\s*/', '', $css); $css = preg_replace('/@charset[^;]+;\\s*/', '', $css);
} }
if ($options['compress']) { if ($options['compress']) {
if (! $options['preserveComments']) { if (! $options['preserveComments']) {
$css = Minify_CSS_Compressor::process($css, $options); $css = Minify_CSS_Compressor::process($css, $options);
} else { } else {
$css = Minify_CommentPreserver::process( $css = Minify_CommentPreserver::process(
$css $css
,array('Minify_CSS_Compressor', 'process') ,array('Minify_CSS_Compressor', 'process')
,array($options) ,array($options)
); );
} }
} }
if (! $options['currentDir'] && ! $options['prependRelativePath']) { if (! $options['currentDir'] && ! $options['prependRelativePath']) {
return $css; return $css;
} }
if ($options['currentDir']) { if ($options['currentDir']) {
return Minify_CSS_UriRewriter::rewrite( return Minify_CSS_UriRewriter::rewrite(
$css $css
,$options['currentDir'] ,$options['currentDir']
,$options['docRoot'] ,$options['docRoot']
,$options['symlinks'] ,$options['symlinks']
); );
} else { } else {
return Minify_CSS_UriRewriter::prepend( return Minify_CSS_UriRewriter::prepend(
$css $css
,$options['prependRelativePath'] ,$options['prependRelativePath']
); );
} }
} }
} }

View File

@@ -1,257 +1,262 @@
<?php <?php
/** /**
* Class Minify_CSS_Compressor * Class Minify_CSS_Compressor
* @package Minify * @package Minify
*/ */
/** /**
* Compress CSS * Compress CSS
* *
* This is a heavy regex-based removal of whitespace, unnecessary * This is a heavy regex-based removal of whitespace, unnecessary
* comments and tokens, and some CSS value minimization, where practical. * comments and tokens, and some CSS value minimization, where practical.
* Many steps have been taken to avoid breaking comment-based hacks, * Many steps have been taken to avoid breaking comment-based hacks,
* including the ie5/mac filter (and its inversion), but expect tricky * including the ie5/mac filter (and its inversion), but expect tricky
* hacks involving comment tokens in 'content' value strings to break * hacks involving comment tokens in 'content' value strings to break
* minimization badly. A test suite is available. * minimization badly. A test suite is available.
* *
* Note: This replaces a lot of spaces with line breaks. It's rumored * Note: This replaces a lot of spaces with line breaks. It's rumored
* (https://github.com/yui/yuicompressor/blob/master/README.md#global-options) * (https://github.com/yui/yuicompressor/blob/master/README.md#global-options)
* that some source control tools and old browsers don't like very long lines. * that some source control tools and old browsers don't like very long lines.
* Compressed files with shorter lines are also easier to diff. If this is * Compressed files with shorter lines are also easier to diff. If this is
* unacceptable please use CSSmin instead. * unacceptable please use CSSmin instead.
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
* @author http://code.google.com/u/1stvamp/ (Issue 64 patch) * @author http://code.google.com/u/1stvamp/ (Issue 64 patch)
* *
* @deprecated Use CSSmin (tubalmartin/cssmin) * @deprecated Use CSSmin (tubalmartin/cssmin)
*/ */
class Minify_CSS_Compressor { class Minify_CSS_Compressor {
/** /**
* Minify a CSS string * Minify a CSS string
* *
* @param string $css * @param string $css
* *
* @param array $options (currently ignored) * @param array $options (currently ignored)
* *
* @return string * @return string
*/ */
public static function process($css, $options = array()) public static function process($css, $options = array())
{ {
$obj = new Minify_CSS_Compressor($options); $obj = new Minify_CSS_Compressor($options);
return $obj->_process($css);
} return $obj->_process($css);
}
/**
* @var array /**
*/ * @var array
protected $_options = null; */
protected $_options = null;
/**
* Are we "in" a hack? I.e. are some browsers targetted until the next comment? /**
* * Are we "in" a hack? I.e. are some browsers targetted until the next comment?
* @var bool *
*/ * @var bool
protected $_inHack = false; */
protected $_inHack = false;
/** /**
* Constructor * Constructor
* *
* @param array $options (currently ignored) * @param array $options (currently ignored)
*/ */
private function __construct($options) { private function __construct($options) {
$this->_options = $options; $this->_options = $options;
} }
/** /**
* Minify a CSS string * Minify a CSS string
* *
* @param string $css * @param string $css
* *
* @return string * @return string
*/ */
protected function _process($css) protected function _process($css)
{ {
$css = str_replace("\r\n", "\n", $css); $css = str_replace("\r\n", "\n", $css);
// preserve empty comment after '>' // preserve empty comment after '>'
// http://www.webdevout.net/css-hacks#in_css-selectors // http://www.webdevout.net/css-hacks#in_css-selectors
$css = preg_replace('@>/\\*\\s*\\*/@', '>/*keep*/', $css); $css = preg_replace('@>/\\*\\s*\\*/@', '>/*keep*/', $css);
// preserve empty comment between property and value // preserve empty comment between property and value
// http://css-discuss.incutio.com/?page=BoxModelHack // http://css-discuss.incutio.com/?page=BoxModelHack
$css = preg_replace('@/\\*\\s*\\*/\\s*:@', '/*keep*/:', $css); $css = preg_replace('@/\\*\\s*\\*/\\s*:@', '/*keep*/:', $css);
$css = preg_replace('@:\\s*/\\*\\s*\\*/@', ':/*keep*/', $css); $css = preg_replace('@:\\s*/\\*\\s*\\*/@', ':/*keep*/', $css);
// apply callback to all valid comments (and strip out surrounding ws // apply callback to all valid comments (and strip out surrounding ws
$css = preg_replace_callback('@\\s*/\\*([\\s\\S]*?)\\*/\\s*@' $css = preg_replace_callback('@\\s*/\\*([\\s\\S]*?)\\*/\\s*@'
,array($this, '_commentCB'), $css); ,array($this, '_commentCB'), $css);
// remove ws around { } and last semicolon in declaration block // remove ws around { } and last semicolon in declaration block
$css = preg_replace('/\\s*{\\s*/', '{', $css); $css = preg_replace('/\\s*{\\s*/', '{', $css);
$css = preg_replace('/;?\\s*}\\s*/', '}', $css); $css = preg_replace('/;?\\s*}\\s*/', '}', $css);
// remove ws surrounding semicolons // remove ws surrounding semicolons
$css = preg_replace('/\\s*;\\s*/', ';', $css); $css = preg_replace('/\\s*;\\s*/', ';', $css);
// remove ws around urls // remove ws around urls
$css = preg_replace('/ $css = preg_replace('/
url\\( # url( url\\( # url(
\\s* \\s*
([^\\)]+?) # 1 = the URL (really just a bunch of non right parenthesis) ([^\\)]+?) # 1 = the URL (really just a bunch of non right parenthesis)
\\s* \\s*
\\) # ) \\) # )
/x', 'url($1)', $css); /x', 'url($1)', $css);
// remove ws between rules and colons // remove ws between rules and colons
$css = preg_replace('/ $css = preg_replace('/
\\s* \\s*
([{;]) # 1 = beginning of block or rule separator ([{;]) # 1 = beginning of block or rule separator
\\s* \\s*
([\\*_]?[\\w\\-]+) # 2 = property (and maybe IE filter) ([\\*_]?[\\w\\-]+) # 2 = property (and maybe IE filter)
\\s* \\s*
: :
\\s* \\s*
(\\b|[#\'"-]) # 3 = first character of a value (\\b|[#\'"-]) # 3 = first character of a value
/x', '$1$2:$3', $css); /x', '$1$2:$3', $css);
// remove ws in selectors // remove ws in selectors
$css = preg_replace_callback('/ $css = preg_replace_callback('/
(?: # non-capture (?: # non-capture
\\s* \\s*
[^~>+,\\s]+ # selector part [^~>+,\\s]+ # selector part
\\s* \\s*
[,>+~] # combinators [,>+~] # combinators
)+ )+
\\s* \\s*
[^~>+,\\s]+ # selector part [^~>+,\\s]+ # selector part
{ # open declaration block { # open declaration block
/x' /x'
,array($this, '_selectorsCB'), $css); ,array($this, '_selectorsCB'), $css);
// minimize hex colors // minimize hex colors
$css = preg_replace('/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i' $css = preg_replace('/([^=])#([a-f\\d])\\2([a-f\\d])\\3([a-f\\d])\\4([\\s;\\}])/i'
, '$1#$2$3$4$5', $css); , '$1#$2$3$4$5', $css);
// remove spaces between font families // remove spaces between font families
$css = preg_replace_callback('/font-family:([^;}]+)([;}])/' $css = preg_replace_callback('/font-family:([^;}]+)([;}])/'
,array($this, '_fontFamilyCB'), $css); ,array($this, '_fontFamilyCB'), $css);
$css = preg_replace('/@import\\s+url/', '@import url', $css); $css = preg_replace('/@import\\s+url/', '@import url', $css);
// replace any ws involving newlines with a single newline // replace any ws involving newlines with a single newline
$css = preg_replace('/[ \\t]*\\n+\\s*/', "\n", $css); $css = preg_replace('/[ \\t]*\\n+\\s*/', "\n", $css);
// separate common descendent selectors w/ newlines (to limit line lengths) // separate common descendent selectors w/ newlines (to limit line lengths)
$css = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $css); $css = preg_replace('/([\\w#\\.\\*]+)\\s+([\\w#\\.\\*]+){/', "$1\n$2{", $css);
// Use newline after 1st numeric value (to limit line lengths). // Use newline after 1st numeric value (to limit line lengths).
$css = preg_replace('/ $css = preg_replace('/
((?:padding|margin|border|outline):\\d+(?:px|em)?) # 1 = prop : 1st numeric value ((?:padding|margin|border|outline):\\d+(?:px|em)?) # 1 = prop : 1st numeric value
\\s+ \\s+
/x' /x'
,"$1\n", $css); ,"$1\n", $css);
// prevent triggering IE6 bug: http://www.crankygeek.com/ie6pebug/ // prevent triggering IE6 bug: http://www.crankygeek.com/ie6pebug/
$css = preg_replace('/:first-l(etter|ine)\\{/', ':first-l$1 {', $css); $css = preg_replace('/:first-l(etter|ine)\\{/', ':first-l$1 {', $css);
return trim($css); return trim($css);
} }
/** /**
* Replace what looks like a set of selectors * Replace what looks like a set of selectors
* *
* @param array $m regex matches * @param array $m regex matches
* *
* @return string * @return string
*/ */
protected function _selectorsCB($m) protected function _selectorsCB($m)
{ {
// remove ws around the combinators // remove ws around the combinators
return preg_replace('/\\s*([,>+~])\\s*/', '$1', $m[0]); return preg_replace('/\\s*([,>+~])\\s*/', '$1', $m[0]);
} }
/** /**
* Process a comment and return a replacement * Process a comment and return a replacement
* *
* @param array $m regex matches * @param array $m regex matches
* *
* @return string * @return string
*/ */
protected function _commentCB($m) protected function _commentCB($m)
{ {
$hasSurroundingWs = (trim($m[0]) !== $m[1]); $hasSurroundingWs = (trim($m[0]) !== $m[1]);
$m = $m[1]; $m = $m[1];
// $m is the comment content w/o the surrounding tokens, // $m is the comment content w/o the surrounding tokens,
// but the return value will replace the entire comment. // but the return value will replace the entire comment.
if ($m === 'keep') { if ($m === 'keep') {
return '/**/'; return '/**/';
} }
if ($m === '" "') { if ($m === '" "') {
// component of http://tantek.com/CSS/Examples/midpass.html // component of http://tantek.com/CSS/Examples/midpass.html
return '/*" "*/'; return '/*" "*/';
} }
if (preg_match('@";\\}\\s*\\}/\\*\\s+@', $m)) { if (preg_match('@";\\}\\s*\\}/\\*\\s+@', $m)) {
// component of http://tantek.com/CSS/Examples/midpass.html // component of http://tantek.com/CSS/Examples/midpass.html
return '/*";}}/* */'; return '/*";}}/* */';
} }
if ($this->_inHack) { if ($this->_inHack) {
// inversion: feeding only to one browser // inversion: feeding only to one browser
if (preg_match('@ if (preg_match('@
^/ # comment started like /*/ ^/ # comment started like /*/
\\s* \\s*
(\\S[\\s\\S]+?) # has at least some non-ws content (\\S[\\s\\S]+?) # has at least some non-ws content
\\s* \\s*
/\\* # ends like /*/ or /**/ /\\* # ends like /*/ or /**/
@x', $m, $n)) { @x', $m, $n)) {
// end hack mode after this comment, but preserve the hack and comment content // end hack mode after this comment, but preserve the hack and comment content
$this->_inHack = false; $this->_inHack = false;
return "/*/{$n[1]}/**/";
} return "/*/{$n[1]}/**/";
} }
if (substr($m, -1) === '\\') { // comment ends like \*/ }
// begin hack mode and preserve hack if (substr($m, -1) === '\\') { // comment ends like \*/
$this->_inHack = true; // begin hack mode and preserve hack
return '/*\\*/'; $this->_inHack = true;
}
if ($m !== '' && $m[0] === '/') { // comment looks like /*/ foo */ return '/*\\*/';
// begin hack mode and preserve hack }
$this->_inHack = true; if ($m !== '' && $m[0] === '/') { // comment looks like /*/ foo */
return '/*/*/'; // begin hack mode and preserve hack
} $this->_inHack = true;
if ($this->_inHack) {
// a regular comment ends hack mode but should be preserved return '/*/*/';
$this->_inHack = false; }
return '/**/'; if ($this->_inHack) {
} // a regular comment ends hack mode but should be preserved
// Issue 107: if there's any surrounding whitespace, it may be important, so $this->_inHack = false;
// replace the comment with a single space
return $hasSurroundingWs // remove all other comments return '/**/';
? ' ' }
: ''; // Issue 107: if there's any surrounding whitespace, it may be important, so
} // replace the comment with a single space
return $hasSurroundingWs // remove all other comments
/** ? ' '
* Process a font-family listing and return a replacement : '';
* }
* @param array $m regex matches
* /**
* @return string * Process a font-family listing and return a replacement
*/ *
protected function _fontFamilyCB($m) * @param array $m regex matches
{ *
// Issue 210: must not eliminate WS between words in unquoted families * @return string
$pieces = preg_split('/(\'[^\']+\'|"[^"]+")/', $m[1], null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); */
$out = 'font-family:'; protected function _fontFamilyCB($m)
while (null !== ($piece = array_shift($pieces))) { {
if ($piece[0] !== '"' && $piece[0] !== "'") { // Issue 210: must not eliminate WS between words in unquoted families
$piece = preg_replace('/\\s+/', ' ', $piece); $pieces = preg_split('/(\'[^\']+\'|"[^"]+")/', $m[1], null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$piece = preg_replace('/\\s?,\\s?/', ',', $piece); $out = 'font-family:';
} while (null !== ($piece = array_shift($pieces))) {
$out .= $piece; if ($piece[0] !== '"' && $piece[0] !== "'") {
} $piece = preg_replace('/\\s+/', ' ', $piece);
return $out . $m[2]; $piece = preg_replace('/\\s?,\\s?/', ',', $piece);
} }
} $out .= $piece;
}
return $out . $m[2];
}
}

View File

@@ -1,6 +1,6 @@
<?php <?php
/** /**
* Class Minify_CSS_UriRewriter * Class Minify_CSS_UriRewriter
* @package Minify * @package Minify
*/ */
@@ -11,37 +11,37 @@
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */
class Minify_CSS_UriRewriter { class Minify_CSS_UriRewriter {
/** /**
* rewrite() and rewriteRelative() append debugging information here * rewrite() and rewriteRelative() append debugging information here
* *
* @var string * @var string
*/ */
public static $debugText = ''; public static $debugText = '';
/** /**
* In CSS content, rewrite file relative URIs as root relative * In CSS content, rewrite file relative URIs as root relative
* *
* @param string $css * @param string $css
* *
* @param string $currentDir The directory of the current CSS file. * @param string $currentDir The directory of the current CSS file.
* *
* @param string $docRoot The document root of the web site in which * @param string $docRoot The document root of the web site in which
* the CSS file resides (default = $_SERVER['DOCUMENT_ROOT']). * the CSS file resides (default = $_SERVER['DOCUMENT_ROOT']).
* *
* @param array $symlinks (default = array()) If the CSS file is stored in * @param array $symlinks (default = array()) If the CSS file is stored in
* a symlink-ed directory, provide an array of link paths to * a symlink-ed directory, provide an array of link paths to
* target paths, where the link paths are within the document root. Because * target paths, where the link paths are within the document root. Because
* paths need to be normalized for this to work, use "//" to substitute * paths need to be normalized for this to work, use "//" to substitute
* the doc root in the link paths (the array keys). E.g.: * the doc root in the link paths (the array keys). E.g.:
* <code> * <code>
* array('//symlink' => '/real/target/path') // unix * array('//symlink' => '/real/target/path') // unix
* array('//static' => 'D:\\staticStorage') // Windows * array('//static' => 'D:\\staticStorage') // Windows
* </code> * </code>
* *
* @return string * @return string
*/ */
public static function rewrite($css, $currentDir, $docRoot = null, $symlinks = array()) public static function rewrite($css, $currentDir, $docRoot = null, $symlinks = array())
{ {
self::$_docRoot = self::_realpath( self::$_docRoot = self::_realpath(
$docRoot ? $docRoot : $_SERVER['DOCUMENT_ROOT'] $docRoot ? $docRoot : $_SERVER['DOCUMENT_ROOT']
@@ -57,16 +57,16 @@ class Minify_CSS_UriRewriter {
$link = strtr($link, '/', DIRECTORY_SEPARATOR); $link = strtr($link, '/', DIRECTORY_SEPARATOR);
self::$_symlinks[$link] = self::_realpath($target); self::$_symlinks[$link] = self::_realpath($target);
} }
self::$debugText .= "docRoot : " . self::$_docRoot . "\n" self::$debugText .= "docRoot : " . self::$_docRoot . "\n"
. "currentDir : " . self::$_currentDir . "\n"; . "currentDir : " . self::$_currentDir . "\n";
if (self::$_symlinks) { if (self::$_symlinks) {
self::$debugText .= "symlinks : " . var_export(self::$_symlinks, 1) . "\n"; self::$debugText .= "symlinks : " . var_export(self::$_symlinks, 1) . "\n";
} }
self::$debugText .= "\n"; self::$debugText .= "\n";
$css = self::_trimUrls($css); $css = self::_trimUrls($css);
// rewrite // rewrite
$css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/' $css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
,array(self::$className, '_processUriCB'), $css); ,array(self::$className, '_processUriCB'), $css);
@@ -75,22 +75,22 @@ class Minify_CSS_UriRewriter {
return $css; return $css;
} }
/** /**
* In CSS content, prepend a path to relative URIs * In CSS content, prepend a path to relative URIs
* *
* @param string $css * @param string $css
* *
* @param string $path The path to prepend. * @param string $path The path to prepend.
* *
* @return string * @return string
*/ */
public static function prepend($css, $path) public static function prepend($css, $path)
{ {
self::$_prependPath = $path; self::$_prependPath = $path;
$css = self::_trimUrls($css); $css = self::_trimUrls($css);
// append // append
$css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/' $css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/'
,array(self::$className, '_processUriCB'), $css); ,array(self::$className, '_processUriCB'), $css);
@@ -98,9 +98,10 @@ class Minify_CSS_UriRewriter {
,array(self::$className, '_processUriCB'), $css); ,array(self::$className, '_processUriCB'), $css);
self::$_prependPath = null; self::$_prependPath = null;
return $css; return $css;
} }
/** /**
* Get a root relative URI from a file relative URI * Get a root relative URI from a file relative URI
* *
@@ -111,7 +112,7 @@ class Minify_CSS_UriRewriter {
* , '/home/user/www' // doc root * , '/home/user/www' // doc root
* ); * );
* // returns '/img/hello.gif' * // returns '/img/hello.gif'
* *
* // example where static files are stored in a symlinked directory * // example where static files are stored in a symlinked directory
* Minify_CSS_UriRewriter::rewriteRelative( * Minify_CSS_UriRewriter::rewriteRelative(
* 'hello.gif' * 'hello.gif'
@@ -121,55 +122,55 @@ class Minify_CSS_UriRewriter {
* ); * );
* // returns '/static/theme/hello.gif' * // returns '/static/theme/hello.gif'
* </code> * </code>
* *
* @param string $uri file relative URI * @param string $uri file relative URI
* *
* @param string $realCurrentDir realpath of the current file's directory. * @param string $realCurrentDir realpath of the current file's directory.
* *
* @param string $realDocRoot realpath of the site document root. * @param string $realDocRoot realpath of the site document root.
* *
* @param array $symlinks (default = array()) If the file is stored in * @param array $symlinks (default = array()) If the file is stored in
* a symlink-ed directory, provide an array of link paths to * a symlink-ed directory, provide an array of link paths to
* real target paths, where the link paths "appear" to be within the document * real target paths, where the link paths "appear" to be within the document
* root. E.g.: * root. E.g.:
* <code> * <code>
* array('/home/foo/www/not/real/path' => '/real/target/path') // unix * array('/home/foo/www/not/real/path' => '/real/target/path') // unix
* array('C:\\htdocs\\not\\real' => 'D:\\real\\target\\path') // Windows * array('C:\\htdocs\\not\\real' => 'D:\\real\\target\\path') // Windows
* </code> * </code>
* *
* @return string * @return string
*/ */
public static function rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks = array()) public static function rewriteRelative($uri, $realCurrentDir, $realDocRoot, $symlinks = array())
{ {
// prepend path with current dir separator (OS-independent) // prepend path with current dir separator (OS-independent)
$path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR) $path = strtr($realCurrentDir, '/', DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR); . DIRECTORY_SEPARATOR . strtr($uri, '/', DIRECTORY_SEPARATOR);
self::$debugText .= "file-relative URI : {$uri}\n" self::$debugText .= "file-relative URI : {$uri}\n"
. "path prepended : {$path}\n"; . "path prepended : {$path}\n";
// "unresolve" a symlink back to doc root // "unresolve" a symlink back to doc root
foreach ($symlinks as $link => $target) { foreach ($symlinks as $link => $target) {
if (0 === strpos($path, $target)) { if (0 === strpos($path, $target)) {
// replace $target with $link // replace $target with $link
$path = $link . substr($path, strlen($target)); $path = $link . substr($path, strlen($target));
self::$debugText .= "symlink unresolved : {$path}\n"; self::$debugText .= "symlink unresolved : {$path}\n";
break; break;
} }
} }
// strip doc root // strip doc root
$path = substr($path, strlen($realDocRoot)); $path = substr($path, strlen($realDocRoot));
self::$debugText .= "docroot stripped : {$path}\n"; self::$debugText .= "docroot stripped : {$path}\n";
// fix to root-relative URI // fix to root-relative URI
$uri = strtr($path, '/\\', '//'); $uri = strtr($path, '/\\', '//');
$uri = self::removeDots($uri); $uri = self::removeDots($uri);
self::$debugText .= "traversals removed : {$uri}\n\n"; self::$debugText .= "traversals removed : {$uri}\n\n";
return $uri; return $uri;
} }
@@ -187,9 +188,10 @@ class Minify_CSS_UriRewriter {
do { do {
$uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, 1, $changed); $uri = preg_replace('@/[^/]+/\\.\\./@', '/', $uri, 1, $changed);
} while ($changed); } while ($changed);
return $uri; return $uri;
} }
/** /**
* Defines which class to call as part of callbacks, change this * Defines which class to call as part of callbacks, change this
* if you extend Minify_CSS_UriRewriter * if you extend Minify_CSS_UriRewriter
@@ -201,9 +203,9 @@ class Minify_CSS_UriRewriter {
/** /**
* Get realpath with any trailing slash removed. If realpath() fails, * Get realpath with any trailing slash removed. If realpath() fails,
* just remove the trailing slash. * just remove the trailing slash.
* *
* @param string $path * @param string $path
* *
* @return mixed path with no trailing slash * @return mixed path with no trailing slash
*/ */
protected static function _realpath($path) protected static function _realpath($path)
@@ -212,6 +214,7 @@ class Minify_CSS_UriRewriter {
if ($realPath !== false) { if ($realPath !== false) {
$path = $realPath; $path = $realPath;
} }
return rtrim($path, '/\\'); return rtrim($path, '/\\');
} }
@@ -300,6 +303,7 @@ class Minify_CSS_UriRewriter {
} }
} }
} }
return $isImport return $isImport
? "@import {$quoteChar}{$uri}{$quoteChar}" ? "@import {$quoteChar}{$uri}{$quoteChar}"
: "url({$quoteChar}{$uri}{$quoteChar})"; : "url({$quoteChar}{$uri}{$quoteChar})";

View File

@@ -1,85 +1,85 @@
<?php <?php
/** /**
* Class Minify_CSSmin * Class Minify_CSSmin
* @package Minify * @package Minify
*/ */
/** /**
* Wrapper for CSSmin * Wrapper for CSSmin
* *
* This class uses CSSmin and Minify_CSS_UriRewriter to minify CSS and rewrite relative URIs. * This class uses CSSmin and Minify_CSS_UriRewriter to minify CSS and rewrite relative URIs.
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */
class Minify_CSSmin { class Minify_CSSmin {
/** /**
* Minify a CSS string * Minify a CSS string
* *
* @param string $css * @param string $css
* *
* @param array $options available options: * @param array $options available options:
* *
* 'removeCharsets': (default true) remove all @charset at-rules * 'removeCharsets': (default true) remove all @charset at-rules
* *
* 'prependRelativePath': (default null) if given, this string will be * 'prependRelativePath': (default null) if given, this string will be
* prepended to all relative URIs in import/url declarations * prepended to all relative URIs in import/url declarations
* *
* 'currentDir': (default null) if given, this is assumed to be the * 'currentDir': (default null) if given, this is assumed to be the
* directory of the current CSS file. Using this, minify will rewrite * directory of the current CSS file. Using this, minify will rewrite
* all relative URIs in import/url declarations to correctly point to * all relative URIs in import/url declarations to correctly point to
* the desired files. For this to work, the files *must* exist and be * the desired files. For this to work, the files *must* exist and be
* visible by the PHP process. * visible by the PHP process.
* *
* 'symlinks': (default = array()) If the CSS file is stored in * 'symlinks': (default = array()) If the CSS file is stored in
* a symlink-ed directory, provide an array of link paths to * a symlink-ed directory, provide an array of link paths to
* target paths, where the link paths are within the document root. Because * target paths, where the link paths are within the document root. Because
* paths need to be normalized for this to work, use "//" to substitute * paths need to be normalized for this to work, use "//" to substitute
* the doc root in the link paths (the array keys). E.g.: * the doc root in the link paths (the array keys). E.g.:
* <code> * <code>
* array('//symlink' => '/real/target/path') // unix * array('//symlink' => '/real/target/path') // unix
* array('//static' => 'D:\\staticStorage') // Windows * array('//static' => 'D:\\staticStorage') // Windows
* </code> * </code>
* *
* 'docRoot': (default = $_SERVER['DOCUMENT_ROOT']) * 'docRoot': (default = $_SERVER['DOCUMENT_ROOT'])
* see Minify_CSS_UriRewriter::rewrite * see Minify_CSS_UriRewriter::rewrite
* *
* @return string * @return string
*/ */
public static function minify($css, $options = array()) public static function minify($css, $options = array())
{ {
$options = array_merge(array( $options = array_merge(array(
'compress' => true, 'compress' => true,
'removeCharsets' => true, 'removeCharsets' => true,
'currentDir' => null, 'currentDir' => null,
'docRoot' => $_SERVER['DOCUMENT_ROOT'], 'docRoot' => $_SERVER['DOCUMENT_ROOT'],
'prependRelativePath' => null, 'prependRelativePath' => null,
'symlinks' => array(), 'symlinks' => array(),
), $options); ), $options);
if ($options['removeCharsets']) { if ($options['removeCharsets']) {
$css = preg_replace('/@charset[^;]+;\\s*/', '', $css); $css = preg_replace('/@charset[^;]+;\\s*/', '', $css);
} }
if ($options['compress']) { if ($options['compress']) {
$obj = new CSSmin(); $obj = new CSSmin();
$css = $obj->run($css); $css = $obj->run($css);
} }
if (! $options['currentDir'] && ! $options['prependRelativePath']) { if (! $options['currentDir'] && ! $options['prependRelativePath']) {
return $css; return $css;
} }
if ($options['currentDir']) { if ($options['currentDir']) {
return Minify_CSS_UriRewriter::rewrite( return Minify_CSS_UriRewriter::rewrite(
$css $css
,$options['currentDir'] ,$options['currentDir']
,$options['docRoot'] ,$options['docRoot']
,$options['symlinks'] ,$options['symlinks']
); );
} else { } else {
return Minify_CSS_UriRewriter::prepend( return Minify_CSS_UriRewriter::prepend(
$css $css
,$options['prependRelativePath'] ,$options['prependRelativePath']
); );
} }
} }
} }

View File

@@ -6,11 +6,11 @@
/** /**
* APC-based cache class for Minify * APC-based cache class for Minify
* *
* <code> * <code>
* Minify::setCache(new Minify_Cache_APC()); * Minify::setCache(new Minify_Cache_APC());
* </code> * </code>
* *
* @package Minify * @package Minify
* @author Chris Edwards * @author Chris Edwards
**/ **/
@@ -57,6 +57,7 @@ class Minify_Cache_APC implements Minify_CacheInterface {
if (! $this->_fetch($id)) { if (! $this->_fetch($id)) {
return false; return false;
} }
return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2))
? mb_strlen($this->_data, '8bit') ? mb_strlen($this->_data, '8bit')
: strlen($this->_data); : strlen($this->_data);
@@ -124,10 +125,12 @@ class Minify_Cache_APC implements Minify_CacheInterface {
$ret = apc_fetch($id); $ret = apc_fetch($id);
if (false === $ret) { if (false === $ret) {
$this->_id = null; $this->_id = null;
return false; return false;
} }
list($this->_lm, $this->_data) = explode('|', $ret, 2); list($this->_lm, $this->_data) = explode('|', $ret, 2);
$this->_id = $id; $this->_id = $id;
return true; return true;
} }
} }

View File

@@ -1,11 +1,11 @@
<?php <?php
/** /**
* Class Minify_Cache_File * Class Minify_Cache_File
* @package Minify * @package Minify
*/ */
class Minify_Cache_File implements Minify_CacheInterface { class Minify_Cache_File implements Minify_CacheInterface {
public function __construct($path = '', $fileLocking = false) public function __construct($path = '', $fileLocking = false)
{ {
if (! $path) { if (! $path) {
@@ -19,9 +19,9 @@ class Minify_Cache_File implements Minify_CacheInterface {
* Write data to cache. * Write data to cache.
* *
* @param string $id cache id (e.g. a filename) * @param string $id cache id (e.g. a filename)
* *
* @param string $data * @param string $data
* *
* @return bool success * @return bool success
*/ */
public function store($id, $data) public function store($id, $data)
@@ -37,38 +37,41 @@ class Minify_Cache_File implements Minify_CacheInterface {
if ($data !== $this->fetch($id)) { if ($data !== $this->fetch($id)) {
@unlink($file); @unlink($file);
$this->_log("Minify_Cache_File: Post-write read failed for '$file'"); $this->_log("Minify_Cache_File: Post-write read failed for '$file'");
return false; return false;
} }
return true; return true;
} }
/** /**
* Get the size of a cache entry * Get the size of a cache entry
* *
* @param string $id cache id (e.g. a filename) * @param string $id cache id (e.g. a filename)
* *
* @return int size in bytes * @return int size in bytes
*/ */
public function getSize($id) public function getSize($id)
{ {
return filesize($this->_path . '/' . $id); return filesize($this->_path . '/' . $id);
} }
/** /**
* Does a valid cache entry exist? * Does a valid cache entry exist?
* *
* @param string $id cache id (e.g. a filename) * @param string $id cache id (e.g. a filename)
* *
* @param int $srcMtime mtime of the original source file(s) * @param int $srcMtime mtime of the original source file(s)
* *
* @return bool exists * @return bool exists
*/ */
public function isValid($id, $srcMtime) public function isValid($id, $srcMtime)
{ {
$file = $this->_path . '/' . $id; $file = $this->_path . '/' . $id;
return (is_file($file) && (filemtime($file) >= $srcMtime)); return (is_file($file) && (filemtime($file) >= $srcMtime));
} }
/** /**
* Send the cached content to output * Send the cached content to output
* *
@@ -83,15 +86,15 @@ class Minify_Cache_File implements Minify_CacheInterface {
flock($fp, LOCK_UN); flock($fp, LOCK_UN);
fclose($fp); fclose($fp);
} else { } else {
readfile($this->_path . '/' . $id); readfile($this->_path . '/' . $id);
} }
} }
/** /**
* Fetch the cached content * Fetch the cached content
* *
* @param string $id cache id (e.g. a filename) * @param string $id cache id (e.g. a filename)
* *
* @return string * @return string
*/ */
public function fetch($id) public function fetch($id)
@@ -105,12 +108,13 @@ class Minify_Cache_File implements Minify_CacheInterface {
$ret = stream_get_contents($fp); $ret = stream_get_contents($fp);
flock($fp, LOCK_UN); flock($fp, LOCK_UN);
fclose($fp); fclose($fp);
return $ret; return $ret;
} else { } else {
return file_get_contents($this->_path . '/' . $id); return file_get_contents($this->_path . '/' . $id);
} }
} }
/** /**
* Fetch the cache path used * Fetch the cache path used
* *
@@ -140,6 +144,7 @@ class Minify_Cache_File implements Minify_CacheInterface {
: self::_tmp(); : self::_tmp();
$tmp = rtrim($tmp, DIRECTORY_SEPARATOR); $tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
} }
return $tmp; return $tmp;
} }
@@ -191,7 +196,7 @@ class Minify_Cache_File implements Minify_CacheInterface {
{ {
Minify_Logger::log($msg); Minify_Logger::log($msg);
} }
private $_path = null; private $_path = null;
private $_locking = null; private $_locking = null;
} }

View File

@@ -6,7 +6,7 @@
/** /**
* Memcache-based cache class for Minify * Memcache-based cache class for Minify
* *
* <code> * <code>
* // fall back to disk caching if memcache can't connect * // fall back to disk caching if memcache can't connect
* $memcache = new Memcache; * $memcache = new Memcache;
@@ -18,13 +18,13 @@
* </code> * </code>
**/ **/
class Minify_Cache_Memcache implements Minify_CacheInterface { class Minify_Cache_Memcache implements Minify_CacheInterface {
/** /**
* Create a Minify_Cache_Memcache object, to be passed to * Create a Minify_Cache_Memcache object, to be passed to
* Minify::setCache(). * Minify::setCache().
* *
* @param Memcache $memcache already-connected instance * @param Memcache $memcache already-connected instance
* *
* @param int $expire seconds until expiration (default = 0 * @param int $expire seconds until expiration (default = 0
* meaning the item will not get an expiration date) * meaning the item will not get an expiration date)
*/ */
@@ -33,26 +33,26 @@ class Minify_Cache_Memcache implements Minify_CacheInterface {
$this->_mc = $memcache; $this->_mc = $memcache;
$this->_exp = $expire; $this->_exp = $expire;
} }
/** /**
* Write data to cache. * Write data to cache.
* *
* @param string $id cache id * @param string $id cache id
* *
* @param string $data * @param string $data
* *
* @return bool success * @return bool success
*/ */
public function store($id, $data) public function store($id, $data)
{ {
return $this->_mc->set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", 0, $this->_exp); return $this->_mc->set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", 0, $this->_exp);
} }
/** /**
* Get the size of a cache entry * Get the size of a cache entry
* *
* @param string $id cache id * @param string $id cache id
* *
* @return int size in bytes * @return int size in bytes
*/ */
public function getSize($id) public function getSize($id)
@@ -60,25 +60,26 @@ class Minify_Cache_Memcache implements Minify_CacheInterface {
if (! $this->_fetch($id)) { if (! $this->_fetch($id)) {
return false; return false;
} }
return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2))
? mb_strlen($this->_data, '8bit') ? mb_strlen($this->_data, '8bit')
: strlen($this->_data); : strlen($this->_data);
} }
/** /**
* Does a valid cache entry exist? * Does a valid cache entry exist?
* *
* @param string $id cache id * @param string $id cache id
* *
* @param int $srcMtime mtime of the original source file(s) * @param int $srcMtime mtime of the original source file(s)
* *
* @return bool exists * @return bool exists
*/ */
public function isValid($id, $srcMtime) public function isValid($id, $srcMtime)
{ {
return ($this->_fetch($id) && ($this->_lm >= $srcMtime)); return ($this->_fetch($id) && ($this->_lm >= $srcMtime));
} }
/** /**
* Send the cached content to output * Send the cached content to output
* *
@@ -90,12 +91,12 @@ class Minify_Cache_Memcache implements Minify_CacheInterface {
? $this->_data ? $this->_data
: ''; : '';
} }
/** /**
* Fetch the cached content * Fetch the cached content
* *
* @param string $id cache id * @param string $id cache id
* *
* @return string * @return string
*/ */
public function fetch($id) public function fetch($id)
@@ -104,20 +105,20 @@ class Minify_Cache_Memcache implements Minify_CacheInterface {
? $this->_data ? $this->_data
: ''; : '';
} }
private $_mc = null; private $_mc = null;
private $_exp = null; private $_exp = null;
// cache of most recently fetched id // cache of most recently fetched id
private $_lm = null; private $_lm = null;
private $_data = null; private $_data = null;
private $_id = null; private $_id = null;
/** /**
* Fetch data and timestamp from memcache, store in instance * Fetch data and timestamp from memcache, store in instance
* *
* @param string $id * @param string $id
* *
* @return bool success * @return bool success
*/ */
private function _fetch($id) private function _fetch($id)
@@ -128,10 +129,12 @@ class Minify_Cache_Memcache implements Minify_CacheInterface {
$ret = $this->_mc->get($id); $ret = $this->_mc->get($id);
if (false === $ret) { if (false === $ret) {
$this->_id = null; $this->_id = null;
return false; return false;
} }
list($this->_lm, $this->_data) = explode('|', $ret, 2); list($this->_lm, $this->_data) = explode('|', $ret, 2);
$this->_id = $id; $this->_id = $id;
return true; return true;
} }
} }

View File

@@ -6,11 +6,11 @@
/** /**
* WinCache-based cache class for Minify * WinCache-based cache class for Minify
* *
* <code> * <code>
* Minify::setCache(new Minify_Cache_WinCache()); * Minify::setCache(new Minify_Cache_WinCache());
* </code> * </code>
* *
* @package Minify * @package Minify
* @author Matthias Fax * @author Matthias Fax
**/ **/
@@ -32,7 +32,7 @@ class Minify_Cache_WinCache implements Minify_CacheInterface
} }
$this->_exp = $expire; $this->_exp = $expire;
} }
/** /**
* Write data to cache. * Write data to cache.
* *
@@ -46,7 +46,7 @@ class Minify_Cache_WinCache implements Minify_CacheInterface
{ {
return wincache_ucache_set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", $this->_exp); return wincache_ucache_set($id, "{$_SERVER['REQUEST_TIME']}|{$data}", $this->_exp);
} }
/** /**
* Get the size of a cache entry * Get the size of a cache entry
* *
@@ -59,9 +59,10 @@ class Minify_Cache_WinCache implements Minify_CacheInterface
if (!$this->_fetch($id)) { if (!$this->_fetch($id)) {
return false; return false;
} }
return (function_exists('mb_strlen') && ((int) ini_get('mbstring.func_overload') & 2)) ? mb_strlen($this->_data, '8bit') : strlen($this->_data); return (function_exists('mb_strlen') && ((int) ini_get('mbstring.func_overload') & 2)) ? mb_strlen($this->_data, '8bit') : strlen($this->_data);
} }
/** /**
* Does a valid cache entry exist? * Does a valid cache entry exist?
* *
@@ -75,7 +76,7 @@ class Minify_Cache_WinCache implements Minify_CacheInterface
{ {
return ($this->_fetch($id) && ($this->_lm >= $srcMtime)); return ($this->_fetch($id) && ($this->_lm >= $srcMtime));
} }
/** /**
* Send the cached content to output * Send the cached content to output
* *
@@ -85,7 +86,7 @@ class Minify_Cache_WinCache implements Minify_CacheInterface
{ {
echo $this->_fetch($id) ? $this->_data : ''; echo $this->_fetch($id) ? $this->_data : '';
} }
/** /**
* Fetch the cached content * Fetch the cached content
* *
@@ -97,14 +98,14 @@ class Minify_Cache_WinCache implements Minify_CacheInterface
{ {
return $this->_fetch($id) ? $this->_data : ''; return $this->_fetch($id) ? $this->_data : '';
} }
private $_exp = NULL; private $_exp = NULL;
// cache of most recently fetched id // cache of most recently fetched id
private $_lm = NULL; private $_lm = NULL;
private $_data = NULL; private $_data = NULL;
private $_id = NULL; private $_id = NULL;
/** /**
* Fetch data and timestamp from WinCache, store in instance * Fetch data and timestamp from WinCache, store in instance
* *
@@ -121,10 +122,12 @@ class Minify_Cache_WinCache implements Minify_CacheInterface
$ret = wincache_ucache_get($id, $suc); $ret = wincache_ucache_get($id, $suc);
if (!$suc) { if (!$suc) {
$this->_id = NULL; $this->_id = NULL;
return false; return false;
} }
list($this->_lm, $this->_data) = explode('|', $ret, 2); list($this->_lm, $this->_data) = explode('|', $ret, 2);
$this->_id = $id; $this->_id = $id;
return true; return true;
} }
} }

View File

@@ -54,6 +54,7 @@ class Minify_Cache_XCache implements Minify_CacheInterface {
if (! $this->_fetch($id)) { if (! $this->_fetch($id)) {
return false; return false;
} }
return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2))
? mb_strlen($this->_data, '8bit') ? mb_strlen($this->_data, '8bit')
: strlen($this->_data); : strlen($this->_data);
@@ -117,10 +118,12 @@ class Minify_Cache_XCache implements Minify_CacheInterface {
$ret = xcache_get($id); $ret = xcache_get($id);
if (false === $ret) { if (false === $ret) {
$this->_id = null; $this->_id = null;
return false; return false;
} }
list($this->_lm, $this->_data) = explode('|', $ret, 2); list($this->_lm, $this->_data) = explode('|', $ret, 2);
$this->_id = $id; $this->_id = $id;
return true; return true;
} }
} }

View File

@@ -4,12 +4,11 @@
* @package Minify * @package Minify
*/ */
/** /**
* ZendPlatform-based cache class for Minify * ZendPlatform-based cache class for Minify
* *
* Based on Minify_Cache_APC, uses output_cache_get/put (currently deprecated) * Based on Minify_Cache_APC, uses output_cache_get/put (currently deprecated)
* *
* <code> * <code>
* Minify::setCache(new Minify_Cache_ZendPlatform()); * Minify::setCache(new Minify_Cache_ZendPlatform());
* </code> * </code>
@@ -19,7 +18,6 @@
*/ */
class Minify_Cache_ZendPlatform implements Minify_CacheInterface { class Minify_Cache_ZendPlatform implements Minify_CacheInterface {
/** /**
* Create a Minify_Cache_ZendPlatform object, to be passed to * Create a Minify_Cache_ZendPlatform object, to be passed to
* Minify::setCache(). * Minify::setCache().
@@ -33,7 +31,6 @@ class Minify_Cache_ZendPlatform implements Minify_CacheInterface {
$this->_exp = $expire; $this->_exp = $expire;
} }
/** /**
* Write data to cache. * Write data to cache.
* *
@@ -48,7 +45,6 @@ class Minify_Cache_ZendPlatform implements Minify_CacheInterface {
return output_cache_put($id, "{$_SERVER['REQUEST_TIME']}|{$data}"); return output_cache_put($id, "{$_SERVER['REQUEST_TIME']}|{$data}");
} }
/** /**
* Get the size of a cache entry * Get the size of a cache entry
* *
@@ -75,6 +71,7 @@ class Minify_Cache_ZendPlatform implements Minify_CacheInterface {
public function isValid($id, $srcMtime) public function isValid($id, $srcMtime)
{ {
$ret = ($this->_fetch($id) && ($this->_lm >= $srcMtime)); $ret = ($this->_fetch($id) && ($this->_lm >= $srcMtime));
return $ret; return $ret;
} }
@@ -90,7 +87,6 @@ class Minify_Cache_ZendPlatform implements Minify_CacheInterface {
: ''; : '';
} }
/** /**
* Fetch the cached content * Fetch the cached content
* *
@@ -127,10 +123,12 @@ class Minify_Cache_ZendPlatform implements Minify_CacheInterface {
$ret = output_cache_get($id, $this->_exp); $ret = output_cache_get($id, $this->_exp);
if (false === $ret) { if (false === $ret) {
$this->_id = null; $this->_id = null;
return false; return false;
} }
list($this->_lm, $this->_data) = explode('|', $ret, 2); list($this->_lm, $this->_data) = explode('|', $ret, 2);
$this->_id = $id; $this->_id = $id;
return true; return true;
} }
} }

View File

@@ -4,7 +4,6 @@
* @package Minify * @package Minify
*/ */
/** /**
* Interface for Minify cache adapters * Interface for Minify cache adapters
* *

View File

@@ -73,6 +73,7 @@ class Minify_ClosureCompiler
public static function minify($js, $options = array()) public static function minify($js, $options = array())
{ {
$min = new static(); $min = new static();
return $min->process($js, $options); return $min->process($js, $options);
} }
@@ -123,6 +124,7 @@ class Minify_ClosureCompiler
$this->getCompilerCommandLine(), $this->getCompilerCommandLine(),
$this->getOptionsCommandLine($userOptions) $this->getOptionsCommandLine($userOptions)
); );
return join(' ', $args) . ' ' . escapeshellarg($tmpFile); return join(' ', $args) . ' ' . escapeshellarg($tmpFile);
} }
@@ -137,6 +139,7 @@ class Minify_ClosureCompiler
self::$javaExecutable, self::$javaExecutable,
'-jar', escapeshellarg(self::$jarFile) '-jar', escapeshellarg(self::$jarFile)
); );
return $server; return $server;
} }
@@ -215,6 +218,7 @@ class Minify_ClosureCompiler
throw new Minify_ClosureCompiler_Exception('Could not create temp file in "' . $dir . '".'); throw new Minify_ClosureCompiler_Exception('Could not create temp file in "' . $dir . '".');
} }
file_put_contents($tmpFile, $content); file_put_contents($tmpFile, $content);
return $tmpFile; return $tmpFile;
} }
@@ -232,6 +236,7 @@ class Minify_ClosureCompiler
if (!in_array($result_code, $expectedCodes)) { if (!in_array($result_code, $expectedCodes)) {
throw new Minify_ClosureCompiler_Exception("Unpexpected return code: $result_code"); throw new Minify_ClosureCompiler_Exception("Unpexpected return code: $result_code");
} }
return $output; return $output;
} }
} }

View File

@@ -1,41 +1,41 @@
<?php <?php
/** /**
* Class Minify_CommentPreserver * Class Minify_CommentPreserver
* @package Minify * @package Minify
*/ */
/** /**
* Process a string in pieces preserving C-style comments that begin with "/*!" * Process a string in pieces preserving C-style comments that begin with "/*!"
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */
class Minify_CommentPreserver { class Minify_CommentPreserver {
/** /**
* String to be prepended to each preserved comment * String to be prepended to each preserved comment
* *
* @var string * @var string
*/ */
public static $prepend = "\n"; public static $prepend = "\n";
/** /**
* String to be appended to each preserved comment * String to be appended to each preserved comment
* *
* @var string * @var string
*/ */
public static $append = "\n"; public static $append = "\n";
/** /**
* Process a string outside of C-style comments that begin with "/*!" * Process a string outside of C-style comments that begin with "/*!"
* *
* On each non-empty string outside these comments, the given processor * On each non-empty string outside these comments, the given processor
* function will be called. The comments will be surrounded by * function will be called. The comments will be surrounded by
* Minify_CommentPreserver::$preprend and Minify_CommentPreserver::$append. * Minify_CommentPreserver::$preprend and Minify_CommentPreserver::$append.
* *
* @param string $content * @param string $content
* @param callback $processor function * @param callback $processor function
* @param array $args array of extra arguments to pass to the processor * @param array $args array of extra arguments to pass to the processor
* function (default = array()) * function (default = array())
* @return string * @return string
*/ */
@@ -47,7 +47,7 @@ class Minify_CommentPreserver {
if ('' !== $beforeComment) { if ('' !== $beforeComment) {
$callArgs = $args; $callArgs = $args;
array_unshift($callArgs, $beforeComment); array_unshift($callArgs, $beforeComment);
$ret .= call_user_func_array($processor, $callArgs); $ret .= call_user_func_array($processor, $callArgs);
} }
if (false === $comment) { if (false === $comment) {
break; break;
@@ -55,17 +55,18 @@ class Minify_CommentPreserver {
$ret .= $comment; $ret .= $comment;
$content = $afterComment; $content = $afterComment;
} }
return $ret; return $ret;
} }
/** /**
* Extract comments that YUI Compressor preserves. * Extract comments that YUI Compressor preserves.
* *
* @param string $in input * @param string $in input
* *
* @return array 3 elements are returned. If a YUI comment is found, the * @return array 3 elements are returned. If a YUI comment is found, the
* 2nd element is the comment and the 1st and 3rd are the surrounding * 2nd element is the comment and the 1st and 3rd are the surrounding
* strings. If no comment is found, the entire string is returned as the * strings. If no comment is found, the entire string is returned as the
* 1st element and the other two are false. * 1st element and the other two are false.
*/ */
private static function _nextComment($in) private static function _nextComment($in)
@@ -84,6 +85,7 @@ class Minify_CommentPreserver {
$ret[] = (0 === $endChars) $ret[] = (0 === $endChars)
? '' ? ''
: substr($in, -$endChars); : substr($in, -$endChars);
return $ret; return $ret;
} }
} }

View File

@@ -1,14 +1,14 @@
<?php <?php
/** /**
* Class Minify_Controller_Base * Class Minify_Controller_Base
* @package Minify * @package Minify
*/ */
/** /**
* Base class for Minify controller * Base class for Minify controller
* *
* The controller class validates a request and uses it to create a configuration for Minify::serve(). * The controller class validates a request and uses it to create a configuration for Minify::serve().
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */
@@ -36,13 +36,13 @@ abstract class Minify_Controller_Base implements Minify_ControllerInterface {
/** /**
* Create controller sources and options for Minify::serve() * Create controller sources and options for Minify::serve()
* *
* @param array $options controller and Minify options * @param array $options controller and Minify options
* *
* @return Minify_ServeConfiguration * @return Minify_ServeConfiguration
*/ */
abstract public function createConfiguration(array $options); abstract public function createConfiguration(array $options);
/** /**
* Send message to the Minify logger * Send message to the Minify logger
* *

View File

@@ -1,12 +1,12 @@
<?php <?php
/** /**
* Class Minify_Controller_Files * Class Minify_Controller_Files
* @package Minify * @package Minify
*/ */
/** /**
* Controller class for minifying a set of files * Controller class for minifying a set of files
* *
* E.g. the following would serve the minified Javascript for a site * E.g. the following would serve the minified Javascript for a site
* <code> * <code>
* $options = [ * $options = [
@@ -40,17 +40,17 @@ class Minify_Controller_Files extends Minify_Controller_Base {
/** /**
* Set up file sources * Set up file sources
* *
* @param array $options controller and Minify options * @param array $options controller and Minify options
* @return Minify_ServeConfiguration * @return Minify_ServeConfiguration
* *
* Controller options: * Controller options:
* *
* 'files': (required) array of complete file paths, or a single path * 'files': (required) array of complete file paths, or a single path
*/ */
public function createConfiguration(array $options) { public function createConfiguration(array $options) {
// strip controller options // strip controller options
$files = $options['files']; $files = $options['files'];
// if $files is a single object, casting will break it // if $files is a single object, casting will break it
if (is_object($files)) { if (is_object($files)) {
@@ -59,7 +59,7 @@ class Minify_Controller_Files extends Minify_Controller_Base {
$files = (array)$files; $files = (array)$files;
} }
unset($options['files']); unset($options['files']);
$sources = array(); $sources = array();
foreach ($files as $file) { foreach ($files as $file) {
if ($file instanceof Minify_SourceInterface) { if ($file instanceof Minify_SourceInterface) {
@@ -72,9 +72,11 @@ class Minify_Controller_Files extends Minify_Controller_Base {
)); ));
} catch (Minify_Source_FactoryException $e) { } catch (Minify_Source_FactoryException $e) {
$this->log($e->getMessage()); $this->log($e->getMessage());
return new Minify_ServeConfiguration($options); return new Minify_ServeConfiguration($options);
} }
} }
return new Minify_ServeConfiguration($options, $sources); return new Minify_ServeConfiguration($options, $sources);
} }
} }

View File

@@ -1,33 +1,33 @@
<?php <?php
/** /**
* Class Minify_Controller_Groups * Class Minify_Controller_Groups
* @package Minify * @package Minify
*/ */
/** /**
* Controller class for serving predetermined groups of minimized sets, selected * Controller class for serving predetermined groups of minimized sets, selected
* by PATH_INFO * by PATH_INFO
* *
* <code> * <code>
* Minify::serve('Groups', array( * Minify::serve('Groups', array(
* 'groups' => array( * 'groups' => array(
* 'css' => array('//css/type.css', '//css/layout.css') * 'css' => array('//css/type.css', '//css/layout.css')
* ,'js' => array('//js/jquery.js', '//js/site.js') * ,'js' => array('//js/jquery.js', '//js/site.js')
* ) * )
* )); * ));
* </code> * </code>
* *
* If the above code were placed in /serve.php, it would enable the URLs * If the above code were placed in /serve.php, it would enable the URLs
* /serve.php/js and /serve.php/css * /serve.php/js and /serve.php/css
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */
class Minify_Controller_Groups extends Minify_Controller_Files { class Minify_Controller_Groups extends Minify_Controller_Files {
/** /**
* Set up groups of files as sources * Set up groups of files as sources
* *
* @param array $options controller and Minify options * @param array $options controller and Minify options
* *
* 'groups': (required) array mapping PATH_INFO strings to arrays * 'groups': (required) array mapping PATH_INFO strings to arrays
@@ -41,7 +41,7 @@ class Minify_Controller_Groups extends Minify_Controller_Files {
unset($options['groups']); unset($options['groups']);
$server = $this->env->server(); $server = $this->env->server();
// mod_fcgid places PATH_INFO in ORIG_PATH_INFO // mod_fcgid places PATH_INFO in ORIG_PATH_INFO
$pathInfo = isset($server['ORIG_PATH_INFO']) $pathInfo = isset($server['ORIG_PATH_INFO'])
? substr($server['ORIG_PATH_INFO'], 1) ? substr($server['ORIG_PATH_INFO'], 1)
@@ -52,6 +52,7 @@ class Minify_Controller_Groups extends Minify_Controller_Files {
if (false === $pathInfo || ! isset($groups[$pathInfo])) { if (false === $pathInfo || ! isset($groups[$pathInfo])) {
// no PATH_INFO or not a valid group // no PATH_INFO or not a valid group
$this->log("Missing PATH_INFO or no group set for \"$pathInfo\""); $this->log("Missing PATH_INFO or no group set for \"$pathInfo\"");
return new Minify_ServeConfiguration($options); return new Minify_ServeConfiguration($options);
} }

View File

@@ -6,7 +6,7 @@
/** /**
* Controller class for requests to /min/index.php * Controller class for requests to /min/index.php
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */
@@ -14,7 +14,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
/** /**
* Set up groups of files as sources * Set up groups of files as sources
* *
* @param array $options controller and Minify options * @param array $options controller and Minify options
* *
* @return array Minify options * @return array Minify options
@@ -59,11 +59,13 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
$keys = explode(',', $get['g']); $keys = explode(',', $get['g']);
if ($keys != array_unique($keys)) { if ($keys != array_unique($keys)) {
$this->log("Duplicate group key found."); $this->log("Duplicate group key found.");
return new Minify_ServeConfiguration($options); return new Minify_ServeConfiguration($options);
} }
foreach ($keys as $key) { foreach ($keys as $key) {
if (! isset($localOptions['groups'][$key])) { if (! isset($localOptions['groups'][$key])) {
$this->log("A group configuration for \"{$key}\" was not found"); $this->log("A group configuration for \"{$key}\" was not found");
return new Minify_ServeConfiguration($options); return new Minify_ServeConfiguration($options);
} }
$files = $localOptions['groups'][$key]; $files = $localOptions['groups'][$key];
@@ -91,6 +93,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
} else { } else {
$secondMissingResource = basename($file); $secondMissingResource = basename($file);
$this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource'"); $this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource'");
return new Minify_ServeConfiguration($options); return new Minify_ServeConfiguration($options);
} }
} }
@@ -101,7 +104,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
// try user files // try user files
// The following restrictions are to limit the URLs that minify will // The following restrictions are to limit the URLs that minify will
// respond to. // respond to.
if (// verify at least one file, files are single comma separated, if (// verify at least one file, files are single comma separated,
// and are all same extension // and are all same extension
! preg_match('/^[^,]+\\.(css|less|js)(?:,[^,]+\\.\\1)*$/', $get['f'], $m) ! preg_match('/^[^,]+\\.(css|less|js)(?:,[^,]+\\.\\1)*$/', $get['f'], $m)
// no "//" // no "//"
@@ -110,12 +113,14 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
|| strpos($get['f'], '\\') !== false || strpos($get['f'], '\\') !== false
) { ) {
$this->log("GET param 'f' was invalid"); $this->log("GET param 'f' was invalid");
return new Minify_ServeConfiguration($options); return new Minify_ServeConfiguration($options);
} }
$ext = ".{$m[1]}"; $ext = ".{$m[1]}";
$files = explode(',', $get['f']); $files = explode(',', $get['f']);
if ($files != array_unique($files)) { if ($files != array_unique($files)) {
$this->log("Duplicate files were specified"); $this->log("Duplicate files were specified");
return new Minify_ServeConfiguration($options); return new Minify_ServeConfiguration($options);
} }
if (isset($get['b'])) { if (isset($get['b'])) {
@@ -127,6 +132,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
$base = "/{$get['b']}/"; $base = "/{$get['b']}/";
} else { } else {
$this->log("GET param 'b' was invalid"); $this->log("GET param 'b' was invalid");
return new Minify_ServeConfiguration($options); return new Minify_ServeConfiguration($options);
} }
} else { } else {
@@ -160,6 +166,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
} else { } else {
$secondMissingResource = $uri; $secondMissingResource = $uri;
$this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource`'"); $this->log("More than one file was missing: '$firstMissingResource', '$secondMissingResource`'");
return new Minify_ServeConfiguration($options); return new Minify_ServeConfiguration($options);
} }
} }
@@ -172,6 +179,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
if (!$sources) { if (!$sources) {
$this->log("No sources to serve"); $this->log("No sources to serve");
return new Minify_ServeConfiguration($options); return new Minify_ServeConfiguration($options);
} }

View File

@@ -1,34 +1,34 @@
<?php <?php
/** /**
* Class Minify_Controller_Page * Class Minify_Controller_Page
* @package Minify * @package Minify
*/ */
/** /**
* Controller class for serving a single HTML page * Controller class for serving a single HTML page
* *
* @link http://code.google.com/p/minify/source/browse/trunk/web/examples/1/index.php#59 * @link http://code.google.com/p/minify/source/browse/trunk/web/examples/1/index.php#59
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */
class Minify_Controller_Page extends Minify_Controller_Base { class Minify_Controller_Page extends Minify_Controller_Base {
/** /**
* Set up source of HTML content * Set up source of HTML content
* *
* @param array $options controller and Minify options * @param array $options controller and Minify options
* @return array Minify options * @return array Minify options
* *
* Controller options: * Controller options:
* *
* 'content': (required) HTML markup * 'content': (required) HTML markup
* *
* 'id': (required) id of page (string for use in server-side caching) * 'id': (required) id of page (string for use in server-side caching)
* *
* 'lastModifiedTime': timestamp of when this content changed. This * 'lastModifiedTime': timestamp of when this content changed. This
* is recommended to allow both server and client-side caching. * is recommended to allow both server and client-side caching.
* *
* 'minifyAll': should all CSS and Javascript blocks be individually * 'minifyAll': should all CSS and Javascript blocks be individually
* minified? (default false) * minified? (default false)
*/ */
public function createConfiguration(array $options) { public function createConfiguration(array $options) {

View File

@@ -1,27 +1,28 @@
<?php <?php
/** /**
* Detect whether request should be debugged * Detect whether request should be debugged
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */
class Minify_DebugDetector { class Minify_DebugDetector {
public static function shouldDebugRequest(Minify_Env $env) public static function shouldDebugRequest(Minify_Env $env)
{ {
if ($env->get('debug')) { if ($env->get('debug')) {
return true; return true;
} }
$cookieValue = $env->cookie('minifyDebug'); $cookieValue = $env->cookie('minifyDebug');
if ($cookieValue) { if ($cookieValue) {
foreach (preg_split('/\\s+/', $cookieValue) as $debugUri) { foreach (preg_split('/\\s+/', $cookieValue) as $debugUri) {
$pattern = '@' . preg_quote($debugUri, '@') . '@i'; $pattern = '@' . preg_quote($debugUri, '@') . '@i';
$pattern = str_replace(array('\\*', '\\?'), array('.*', '.'), $pattern); $pattern = str_replace(array('\\*', '\\?'), array('.*', '.'), $pattern);
if (preg_match($pattern, $env->getRequestUri())) { if (preg_match($pattern, $env->getRequestUri())) {
return true; return true;
} }
} }
} }
return false;
} return false;
} }
}

View File

@@ -41,6 +41,7 @@ class Minify_Env {
if (null === $key) { if (null === $key) {
return $this->server; return $this->server;
} }
return isset($this->server[$key]) return isset($this->server[$key])
? $this->server[$key] ? $this->server[$key]
: null; : null;
@@ -51,6 +52,7 @@ class Minify_Env {
if (null === $key) { if (null === $key) {
return $this->cookie; return $this->cookie;
} }
return isset($this->cookie[$key]) return isset($this->cookie[$key])
? $this->cookie[$key] ? $this->cookie[$key]
: null; : null;
@@ -61,6 +63,7 @@ class Minify_Env {
if (null === $key) { if (null === $key) {
return $this->get; return $this->get;
} }
return isset($this->get[$key]) return isset($this->get[$key])
? $this->get[$key] ? $this->get[$key]
: null; : null;
@@ -87,6 +90,7 @@ class Minify_Env {
,0 ,0
,strlen($server['SCRIPT_FILENAME']) - strlen($server['SCRIPT_NAME']) ,strlen($server['SCRIPT_FILENAME']) - strlen($server['SCRIPT_NAME'])
); );
return rtrim($docRoot, '\\'); return rtrim($docRoot, '\\');
} }
} }

View File

@@ -1,255 +1,256 @@
<?php <?php
/** /**
* Class Minify_HTML * Class Minify_HTML
* @package Minify * @package Minify
*/ */
/** /**
* Compress HTML * Compress HTML
* *
* This is a heavy regex-based removal of whitespace, unnecessary comments and * This is a heavy regex-based removal of whitespace, unnecessary comments and
* tokens. IE conditional comments are preserved. There are also options to have * tokens. IE conditional comments are preserved. There are also options to have
* STYLE and SCRIPT blocks compressed by callback functions. * STYLE and SCRIPT blocks compressed by callback functions.
* *
* A test suite is available. * A test suite is available.
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */
class Minify_HTML { class Minify_HTML {
/** /**
* @var boolean * @var boolean
*/ */
protected $_jsCleanComments = true; protected $_jsCleanComments = true;
/** /**
* "Minify" an HTML page * "Minify" an HTML page
* *
* @param string $html * @param string $html
* *
* @param array $options * @param array $options
* *
* 'cssMinifier' : (optional) callback function to process content of STYLE * 'cssMinifier' : (optional) callback function to process content of STYLE
* elements. * elements.
* *
* 'jsMinifier' : (optional) callback function to process content of SCRIPT * 'jsMinifier' : (optional) callback function to process content of SCRIPT
* elements. Note: the type attribute is ignored. * elements. Note: the type attribute is ignored.
* *
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
* unset, minify will sniff for an XHTML doctype. * unset, minify will sniff for an XHTML doctype.
* *
* @return string * @return string
*/ */
public static function minify($html, $options = array()) { public static function minify($html, $options = array()) {
$min = new self($html, $options); $min = new self($html, $options);
return $min->process();
} return $min->process();
}
/** /**
* Create a minifier object * Create a minifier object
* *
* @param string $html * @param string $html
* *
* @param array $options * @param array $options
* *
* 'cssMinifier' : (optional) callback function to process content of STYLE * 'cssMinifier' : (optional) callback function to process content of STYLE
* elements. * elements.
* *
* 'jsMinifier' : (optional) callback function to process content of SCRIPT * 'jsMinifier' : (optional) callback function to process content of SCRIPT
* elements. Note: the type attribute is ignored. * elements. Note: the type attribute is ignored.
* *
* 'jsCleanComments' : (optional) whether to remove HTML comments beginning and end of script block * 'jsCleanComments' : (optional) whether to remove HTML comments beginning and end of script block
* *
* 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If * 'xhtml' : (optional boolean) should content be treated as XHTML1.0? If
* unset, minify will sniff for an XHTML doctype. * unset, minify will sniff for an XHTML doctype.
*/ */
public function __construct($html, $options = array()) public function __construct($html, $options = array())
{ {
$this->_html = str_replace("\r\n", "\n", trim($html)); $this->_html = str_replace("\r\n", "\n", trim($html));
if (isset($options['xhtml'])) { if (isset($options['xhtml'])) {
$this->_isXhtml = (bool)$options['xhtml']; $this->_isXhtml = (bool)$options['xhtml'];
} }
if (isset($options['cssMinifier'])) { if (isset($options['cssMinifier'])) {
$this->_cssMinifier = $options['cssMinifier']; $this->_cssMinifier = $options['cssMinifier'];
} }
if (isset($options['jsMinifier'])) { if (isset($options['jsMinifier'])) {
$this->_jsMinifier = $options['jsMinifier']; $this->_jsMinifier = $options['jsMinifier'];
} }
if (isset($options['jsCleanComments'])) { if (isset($options['jsCleanComments'])) {
$this->_jsCleanComments = (bool)$options['jsCleanComments']; $this->_jsCleanComments = (bool)$options['jsCleanComments'];
} }
} }
/**
/** * Minify the markeup given in the constructor
* Minify the markeup given in the constructor *
* * @return string
* @return string */
*/ public function process()
public function process() {
{ if ($this->_isXhtml === null) {
if ($this->_isXhtml === null) { $this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
$this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML')); }
}
$this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']);
$this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']); $this->_placeholders = array();
$this->_placeholders = array();
// replace SCRIPTs (and minify) with placeholders
// replace SCRIPTs (and minify) with placeholders $this->_html = preg_replace_callback(
$this->_html = preg_replace_callback( '/(\\s*)<script(\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i'
'/(\\s*)<script(\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/i' ,array($this, '_removeScriptCB')
,array($this, '_removeScriptCB') ,$this->_html);
,$this->_html);
// replace STYLEs (and minify) with placeholders
// replace STYLEs (and minify) with placeholders $this->_html = preg_replace_callback(
$this->_html = preg_replace_callback( '/\\s*<style(\\b[^>]*>)([\\s\\S]*?)<\\/style>\\s*/i'
'/\\s*<style(\\b[^>]*>)([\\s\\S]*?)<\\/style>\\s*/i' ,array($this, '_removeStyleCB')
,array($this, '_removeStyleCB') ,$this->_html);
,$this->_html);
// remove HTML comments (not containing IE conditional comments).
// remove HTML comments (not containing IE conditional comments). $this->_html = preg_replace_callback(
$this->_html = preg_replace_callback( '/<!--([\\s\\S]*?)-->/'
'/<!--([\\s\\S]*?)-->/' ,array($this, '_commentCB')
,array($this, '_commentCB') ,$this->_html);
,$this->_html);
// replace PREs with placeholders
// replace PREs with placeholders $this->_html = preg_replace_callback('/\\s*<pre(\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i'
$this->_html = preg_replace_callback('/\\s*<pre(\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i' ,array($this, '_removePreCB')
,array($this, '_removePreCB') ,$this->_html);
,$this->_html);
// replace TEXTAREAs with placeholders
// replace TEXTAREAs with placeholders $this->_html = preg_replace_callback(
$this->_html = preg_replace_callback( '/\\s*<textarea(\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i'
'/\\s*<textarea(\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i' ,array($this, '_removeTextareaCB')
,array($this, '_removeTextareaCB') ,$this->_html);
,$this->_html);
// trim each line.
// trim each line. // @todo take into account attribute values that span multiple lines.
// @todo take into account attribute values that span multiple lines. $this->_html = preg_replace('/^\\s+|\\s+$/m', '', $this->_html);
$this->_html = preg_replace('/^\\s+|\\s+$/m', '', $this->_html);
// remove ws around block/undisplayed elements
// remove ws around block/undisplayed elements $this->_html = preg_replace('/\\s+(<\\/?(?:area|article|aside|base(?:font)?|blockquote|body'
$this->_html = preg_replace('/\\s+(<\\/?(?:area|article|aside|base(?:font)?|blockquote|body' .'|canvas|caption|center|col(?:group)?|dd|dir|div|dl|dt|fieldset|figcaption|figure|footer|form'
.'|canvas|caption|center|col(?:group)?|dd|dir|div|dl|dt|fieldset|figcaption|figure|footer|form' .'|frame(?:set)?|h[1-6]|head|header|hgroup|hr|html|legend|li|link|main|map|menu|meta|nav'
.'|frame(?:set)?|h[1-6]|head|header|hgroup|hr|html|legend|li|link|main|map|menu|meta|nav' .'|ol|opt(?:group|ion)|output|p|param|section|t(?:able|body|head|d|h||r|foot|itle)'
.'|ol|opt(?:group|ion)|output|p|param|section|t(?:able|body|head|d|h||r|foot|itle)' .'|ul|video)\\b[^>]*>)/i', '$1', $this->_html);
.'|ul|video)\\b[^>]*>)/i', '$1', $this->_html);
// remove ws outside of all elements
// remove ws outside of all elements $this->_html = preg_replace(
$this->_html = preg_replace( '/>(\\s(?:\\s*))?([^<]+)(\\s(?:\s*))?</'
'/>(\\s(?:\\s*))?([^<]+)(\\s(?:\s*))?</' ,'>$1$2$3<'
,'>$1$2$3<' ,$this->_html);
,$this->_html);
// use newlines before 1st attribute in open tags (to limit line lengths)
// use newlines before 1st attribute in open tags (to limit line lengths) $this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
$this->_html = preg_replace('/(<[a-z\\-]+)\\s+([^>]+>)/i', "$1\n$2", $this->_html);
// fill placeholders
// fill placeholders $this->_html = str_replace(
$this->_html = str_replace( array_keys($this->_placeholders)
array_keys($this->_placeholders) ,array_values($this->_placeholders)
,array_values($this->_placeholders) ,$this->_html
,$this->_html );
); // issue 229: multi-pass to catch scripts that didn't get replaced in textareas
// issue 229: multi-pass to catch scripts that didn't get replaced in textareas $this->_html = str_replace(
$this->_html = str_replace( array_keys($this->_placeholders)
array_keys($this->_placeholders) ,array_values($this->_placeholders)
,array_values($this->_placeholders) ,$this->_html
,$this->_html );
);
return $this->_html; return $this->_html;
} }
protected function _commentCB($m) protected function _commentCB($m)
{ {
return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<![')) return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<!['))
? $m[0] ? $m[0]
: ''; : '';
} }
protected function _reservePlace($content) protected function _reservePlace($content)
{ {
$placeholder = '%' . $this->_replacementHash . count($this->_placeholders) . '%'; $placeholder = '%' . $this->_replacementHash . count($this->_placeholders) . '%';
$this->_placeholders[$placeholder] = $content; $this->_placeholders[$placeholder] = $content;
return $placeholder;
} return $placeholder;
}
protected $_isXhtml = null;
protected $_replacementHash = null; protected $_isXhtml = null;
protected $_placeholders = array(); protected $_replacementHash = null;
protected $_cssMinifier = null; protected $_placeholders = array();
protected $_jsMinifier = null; protected $_cssMinifier = null;
protected $_jsMinifier = null;
protected function _removePreCB($m)
{ protected function _removePreCB($m)
return $this->_reservePlace("<pre{$m[1]}"); {
} return $this->_reservePlace("<pre{$m[1]}");
}
protected function _removeTextareaCB($m)
{ protected function _removeTextareaCB($m)
return $this->_reservePlace("<textarea{$m[1]}"); {
} return $this->_reservePlace("<textarea{$m[1]}");
}
protected function _removeStyleCB($m)
{ protected function _removeStyleCB($m)
$openStyle = "<style{$m[1]}"; {
$css = $m[2]; $openStyle = "<style{$m[1]}";
// remove HTML comments $css = $m[2];
$css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css); // remove HTML comments
$css = preg_replace('/(?:^\\s*<!--|-->\\s*$)/', '', $css);
// remove CDATA section markers
$css = $this->_removeCdata($css); // remove CDATA section markers
$css = $this->_removeCdata($css);
// minify
$minifier = $this->_cssMinifier // minify
? $this->_cssMinifier $minifier = $this->_cssMinifier
: 'trim'; ? $this->_cssMinifier
$css = call_user_func($minifier, $css); : 'trim';
$css = call_user_func($minifier, $css);
return $this->_reservePlace($this->_needsCdata($css)
? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>" return $this->_reservePlace($this->_needsCdata($css)
: "{$openStyle}{$css}</style>" ? "{$openStyle}/*<![CDATA[*/{$css}/*]]>*/</style>"
); : "{$openStyle}{$css}</style>"
} );
}
protected function _removeScriptCB($m)
{ protected function _removeScriptCB($m)
$openScript = "<script{$m[2]}"; {
$js = $m[3]; $openScript = "<script{$m[2]}";
$js = $m[3];
// whitespace surrounding? preserve at least one space
$ws1 = ($m[1] === '') ? '' : ' '; // whitespace surrounding? preserve at least one space
$ws2 = ($m[4] === '') ? '' : ' '; $ws1 = ($m[1] === '') ? '' : ' ';
$ws2 = ($m[4] === '') ? '' : ' ';
// remove HTML comments (and ending "//" if present)
if ($this->_jsCleanComments) { // remove HTML comments (and ending "//" if present)
$js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js); if ($this->_jsCleanComments) {
} $js = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $js);
}
// remove CDATA section markers
$js = $this->_removeCdata($js); // remove CDATA section markers
$js = $this->_removeCdata($js);
// minify
$minifier = $this->_jsMinifier // minify
? $this->_jsMinifier $minifier = $this->_jsMinifier
: 'trim'; ? $this->_jsMinifier
$js = call_user_func($minifier, $js); : 'trim';
$js = call_user_func($minifier, $js);
return $this->_reservePlace($this->_needsCdata($js)
? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}" return $this->_reservePlace($this->_needsCdata($js)
: "{$ws1}{$openScript}{$js}</script>{$ws2}" ? "{$ws1}{$openScript}/*<![CDATA[*/{$js}/*]]>*/</script>{$ws2}"
); : "{$ws1}{$openScript}{$js}</script>{$ws2}"
} );
}
protected function _removeCdata($str)
{ protected function _removeCdata($str)
return (false !== strpos($str, '<![CDATA[')) {
? str_replace(array('<![CDATA[', ']]>'), '', $str) return (false !== strpos($str, '<![CDATA['))
: $str; ? str_replace(array('<![CDATA[', ']]>'), '', $str)
} : $str;
}
protected function _needsCdata($str)
{ protected function _needsCdata($str)
return ($this->_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str)); {
} return ($this->_isXhtml && preg_match('/(?:[<&]|\\-\\-|\\]\\]>)/', $str));
} }
}

View File

@@ -48,6 +48,7 @@ class Minify_HTML_Helper {
$h->setGroup($keyOrFiles, $opts['farExpires']); $h->setGroup($keyOrFiles, $opts['farExpires']);
} }
$uri = $h->getRawUri($opts['farExpires'], $opts['debug']); $uri = $h->getRawUri($opts['farExpires'], $opts['debug']);
return htmlspecialchars($uri, ENT_QUOTES, $opts['charset']); return htmlspecialchars($uri, ENT_QUOTES, $opts['charset']);
} }
@@ -75,6 +76,7 @@ class Minify_HTML_Helper {
} elseif ($farExpires && $this->_lastModified) { } elseif ($farExpires && $this->_lastModified) {
$path .= "&" . $this->_lastModified; $path .= "&" . $this->_lastModified;
} }
return $path; return $path;
} }
@@ -156,6 +158,7 @@ class Minify_HTML_Helper {
} }
} }
} }
return $max; return $max;
} }
@@ -163,7 +166,6 @@ class Minify_HTML_Helper {
protected $_filePaths = array(); protected $_filePaths = array();
protected $_lastModified = null; protected $_lastModified = null;
/** /**
* In a given array of strings, find the character they all have at * In a given array of strings, find the character they all have at
* a particular index * a particular index
@@ -186,6 +188,7 @@ class Minify_HTML_Helper {
return ''; return '';
} }
} }
return $c; return $c;
} }
@@ -225,6 +228,7 @@ class Minify_HTML_Helper {
? $uri ? $uri
: $bUri; : $bUri;
} }
return $uri; return $uri;
} }
} }

View File

@@ -1,216 +1,221 @@
<?php <?php
/** /**
* Class Minify_ImportProcessor * Class Minify_ImportProcessor
* @package Minify * @package Minify
*/ */
/** /**
* Linearize a CSS/JS file by including content specified by CSS import * Linearize a CSS/JS file by including content specified by CSS import
* declarations. In CSS files, relative URIs are fixed. * declarations. In CSS files, relative URIs are fixed.
* *
* @imports will be processed regardless of where they appear in the source * @imports will be processed regardless of where they appear in the source
* files; i.e. @imports commented out or in string content will still be * files; i.e. @imports commented out or in string content will still be
* processed! * processed!
* *
* This has a unit test but should be considered "experimental". * This has a unit test but should be considered "experimental".
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
* @author Simon Schick <simonsimcity@gmail.com> * @author Simon Schick <simonsimcity@gmail.com>
*/ */
class Minify_ImportProcessor { class Minify_ImportProcessor {
public static $filesIncluded = array(); public static $filesIncluded = array();
public static function process($file) public static function process($file)
{ {
self::$filesIncluded = array(); self::$filesIncluded = array();
self::$_isCss = (strtolower(substr($file, -4)) === '.css'); self::$_isCss = (strtolower(substr($file, -4)) === '.css');
$obj = new Minify_ImportProcessor(dirname($file)); $obj = new Minify_ImportProcessor(dirname($file));
return $obj->_getContent($file);
} return $obj->_getContent($file);
}
// allows callback funcs to know the current directory
private $_currentDir = null; // allows callback funcs to know the current directory
private $_currentDir = null;
// allows callback funcs to know the directory of the file that inherits this one
private $_previewsDir = null; // allows callback funcs to know the directory of the file that inherits this one
private $_previewsDir = null;
// allows _importCB to write the fetched content back to the obj
private $_importedContent = ''; // allows _importCB to write the fetched content back to the obj
private $_importedContent = '';
private static $_isCss = null;
private static $_isCss = null;
/**
* @param String $currentDir /**
* @param String $previewsDir Is only used internally * @param String $currentDir
*/ * @param String $previewsDir Is only used internally
private function __construct($currentDir, $previewsDir = "") */
{ private function __construct($currentDir, $previewsDir = "")
$this->_currentDir = $currentDir; {
$this->_previewsDir = $previewsDir; $this->_currentDir = $currentDir;
} $this->_previewsDir = $previewsDir;
}
private function _getContent($file, $is_imported = false)
{ private function _getContent($file, $is_imported = false)
$file = realpath($file); {
if (! $file $file = realpath($file);
|| in_array($file, self::$filesIncluded) if (! $file
|| false === ($content = @file_get_contents($file)) || in_array($file, self::$filesIncluded)
) { || false === ($content = @file_get_contents($file))
// file missing, already included, or failed read ) {
return ''; // file missing, already included, or failed read
} return '';
self::$filesIncluded[] = realpath($file); }
$this->_currentDir = dirname($file); self::$filesIncluded[] = realpath($file);
$this->_currentDir = dirname($file);
// remove UTF-8 BOM if present
if (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3)) { // remove UTF-8 BOM if present
$content = substr($content, 3); if (pack("CCC",0xef,0xbb,0xbf) === substr($content, 0, 3)) {
} $content = substr($content, 3);
// ensure uniform EOLs }
$content = str_replace("\r\n", "\n", $content); // ensure uniform EOLs
$content = str_replace("\r\n", "\n", $content);
// process @imports
$content = preg_replace_callback( // process @imports
'/ $content = preg_replace_callback(
@import\\s+ '/
(?:url\\(\\s*)? # maybe url( @import\\s+
[\'"]? # maybe quote (?:url\\(\\s*)? # maybe url(
(.*?) # 1 = URI [\'"]? # maybe quote
[\'"]? # maybe end quote (.*?) # 1 = URI
(?:\\s*\\))? # maybe ) [\'"]? # maybe end quote
([a-zA-Z,\\s]*)? # 2 = media list (?:\\s*\\))? # maybe )
; # end token ([a-zA-Z,\\s]*)? # 2 = media list
/x' ; # end token
,array($this, '_importCB') /x'
,$content ,array($this, '_importCB')
); ,$content
);
// You only need to rework the import-path if the script is imported
if (self::$_isCss && $is_imported) { // You only need to rework the import-path if the script is imported
// rewrite remaining relative URIs if (self::$_isCss && $is_imported) {
$content = preg_replace_callback( // rewrite remaining relative URIs
'/url\\(\\s*([^\\)\\s]+)\\s*\\)/' $content = preg_replace_callback(
,array($this, '_urlCB') '/url\\(\\s*([^\\)\\s]+)\\s*\\)/'
,$content ,array($this, '_urlCB')
); ,$content
} );
}
return $this->_importedContent . $content;
} return $this->_importedContent . $content;
}
private function _importCB($m)
{ private function _importCB($m)
$url = $m[1]; {
$mediaList = preg_replace('/\\s+/', '', $m[2]); $url = $m[1];
$mediaList = preg_replace('/\\s+/', '', $m[2]);
if (strpos($url, '://') > 0) {
// protocol, leave in place for CSS, comment for JS if (strpos($url, '://') > 0) {
return self::$_isCss // protocol, leave in place for CSS, comment for JS
? $m[0] return self::$_isCss
: "/* Minify_ImportProcessor will not include remote content */"; ? $m[0]
} : "/* Minify_ImportProcessor will not include remote content */";
if ('/' === $url[0]) { }
// protocol-relative or root path if ('/' === $url[0]) {
$url = ltrim($url, '/'); // protocol-relative or root path
$file = realpath($_SERVER['DOCUMENT_ROOT']) . DIRECTORY_SEPARATOR $url = ltrim($url, '/');
. strtr($url, '/', DIRECTORY_SEPARATOR); $file = realpath($_SERVER['DOCUMENT_ROOT']) . DIRECTORY_SEPARATOR
} else { . strtr($url, '/', DIRECTORY_SEPARATOR);
// relative to current path } else {
$file = $this->_currentDir . DIRECTORY_SEPARATOR // relative to current path
. strtr($url, '/', DIRECTORY_SEPARATOR); $file = $this->_currentDir . DIRECTORY_SEPARATOR
} . strtr($url, '/', DIRECTORY_SEPARATOR);
$obj = new Minify_ImportProcessor(dirname($file), $this->_currentDir); }
$content = $obj->_getContent($file, true); $obj = new Minify_ImportProcessor(dirname($file), $this->_currentDir);
if ('' === $content) { $content = $obj->_getContent($file, true);
// failed. leave in place for CSS, comment for JS if ('' === $content) {
return self::$_isCss // failed. leave in place for CSS, comment for JS
? $m[0] return self::$_isCss
: "/* Minify_ImportProcessor could not fetch '{$file}' */"; ? $m[0]
} : "/* Minify_ImportProcessor could not fetch '{$file}' */";
return (!self::$_isCss || preg_match('@(?:^$|\\ball\\b)@', $mediaList)) }
? $content
: "@media {$mediaList} {\n{$content}\n}\n"; return (!self::$_isCss || preg_match('@(?:^$|\\ball\\b)@', $mediaList))
} ? $content
: "@media {$mediaList} {\n{$content}\n}\n";
private function _urlCB($m) }
{
// $m[1] is either quoted or not private function _urlCB($m)
$quote = ($m[1][0] === "'" || $m[1][0] === '"') {
? $m[1][0] // $m[1] is either quoted or not
: ''; $quote = ($m[1][0] === "'" || $m[1][0] === '"')
$url = ($quote === '') ? $m[1][0]
? $m[1] : '';
: substr($m[1], 1, strlen($m[1]) - 2); $url = ($quote === '')
if ('/' !== $url[0]) { ? $m[1]
if (strpos($url, '//') > 0) { : substr($m[1], 1, strlen($m[1]) - 2);
// probably starts with protocol, do not alter if ('/' !== $url[0]) {
} else { if (strpos($url, '//') > 0) {
// prepend path with current dir separator (OS-independent) // probably starts with protocol, do not alter
$path = $this->_currentDir } else {
. DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR); // prepend path with current dir separator (OS-independent)
// update the relative path by the directory of the file that imported this one $path = $this->_currentDir
$url = self::getPathDiff(realpath($this->_previewsDir), $path); . DIRECTORY_SEPARATOR . strtr($url, '/', DIRECTORY_SEPARATOR);
} // update the relative path by the directory of the file that imported this one
} $url = self::getPathDiff(realpath($this->_previewsDir), $path);
return "url({$quote}{$url}{$quote})"; }
} }
/** return "url({$quote}{$url}{$quote})";
* @param string $from }
* @param string $to
* @param string $ps /**
* @return string * @param string $from
*/ * @param string $to
private function getPathDiff($from, $to, $ps = DIRECTORY_SEPARATOR) * @param string $ps
{ * @return string
$realFrom = $this->truepath($from); */
$realTo = $this->truepath($to); private function getPathDiff($from, $to, $ps = DIRECTORY_SEPARATOR)
{
$arFrom = explode($ps, rtrim($realFrom, $ps)); $realFrom = $this->truepath($from);
$arTo = explode($ps, rtrim($realTo, $ps)); $realTo = $this->truepath($to);
while (count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0]))
{ $arFrom = explode($ps, rtrim($realFrom, $ps));
array_shift($arFrom); $arTo = explode($ps, rtrim($realTo, $ps));
array_shift($arTo); while (count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0]))
} {
return str_pad("", count($arFrom) * 3, '..' . $ps) . implode($ps, $arTo); array_shift($arFrom);
} array_shift($arTo);
}
/**
* This function is to replace PHP's extremely buggy realpath(). return str_pad("", count($arFrom) * 3, '..' . $ps) . implode($ps, $arTo);
* @param string $path The original path, can be relative etc. }
* @return string The resolved path, it might not exist.
* @see http://stackoverflow.com/questions/4049856/replace-phps-realpath /**
*/ * This function is to replace PHP's extremely buggy realpath().
function truepath($path) * @param string $path The original path, can be relative etc.
{ * @return string The resolved path, it might not exist.
// whether $path is unix or not * @see http://stackoverflow.com/questions/4049856/replace-phps-realpath
$unipath = strlen($path) == 0 || $path{0} != '/'; */
// attempts to detect if path is relative in which case, add cwd public function truepath($path)
if (strpos($path, ':') === false && $unipath) {
$path = $this->_currentDir . DIRECTORY_SEPARATOR . $path; // whether $path is unix or not
$unipath = strlen($path) == 0 || $path{0} != '/';
// resolve path parts (single dot, double dot and double delimiters) // attempts to detect if path is relative in which case, add cwd
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path); if (strpos($path, ':') === false && $unipath)
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen'); $path = $this->_currentDir . DIRECTORY_SEPARATOR . $path;
$absolutes = array();
foreach ($parts as $part) { // resolve path parts (single dot, double dot and double delimiters)
if ('.' == $part) $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
continue; $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
if ('..' == $part) { $absolutes = array();
array_pop($absolutes); foreach ($parts as $part) {
} else { if ('.' == $part)
$absolutes[] = $part; continue;
} if ('..' == $part) {
} array_pop($absolutes);
$path = implode(DIRECTORY_SEPARATOR, $absolutes); } else {
// resolve any symlinks $absolutes[] = $part;
if (file_exists($path) && linkinfo($path) > 0) }
$path = readlink($path); }
// put initial separator that could have been lost $path = implode(DIRECTORY_SEPARATOR, $absolutes);
$path = !$unipath ? '/' . $path : $path; // resolve any symlinks
return $path; if (file_exists($path) && linkinfo($path) > 0)
} $path = readlink($path);
} // put initial separator that could have been lost
$path = !$unipath ? '/' . $path : $path;
return $path;
}
}

View File

@@ -81,6 +81,7 @@ class Minify_JS_ClosureCompiler {
public static function minify($js, array $options = array()) public static function minify($js, array $options = array())
{ {
$obj = new self($options); $obj = new self($options);
return $obj->min($js); return $obj->min($js);
} }

View File

@@ -35,6 +35,7 @@ class Minify_LessCssSource extends Minify_Source {
$lastModified = max($lastModified, $mtime); $lastModified = max($lastModified, $mtime);
} }
return $lastModified; return $lastModified;
} }
@@ -90,6 +91,7 @@ class Minify_LessCssSource extends Minify_Source {
*/ */
private function getCacheId($prefix = 'minify') { private function getCacheId($prefix = 'minify') {
$md5 = md5($this->filepath); $md5 = md5($this->filepath);
return "{$prefix}_less_{$md5}"; return "{$prefix}_less_{$md5}";
} }
@@ -102,6 +104,7 @@ class Minify_LessCssSource extends Minify_Source {
$less = new lessc(); $less = new lessc();
// do not spend CPU time letting less doing minify // do not spend CPU time letting less doing minify
$less->setPreserveComments(true); $less->setPreserveComments(true);
return $less; return $less;
} }
} }

View File

@@ -1,6 +1,6 @@
<?php <?php
/** /**
* Class Minify_Lines * Class Minify_Lines
* @package Minify * @package Minify
*/ */
@@ -17,13 +17,13 @@ class Minify_Lines {
* Add line numbers in C-style comments * Add line numbers in C-style comments
* *
* This uses a very basic parser easily fooled by comment tokens inside * This uses a very basic parser easily fooled by comment tokens inside
* strings or regexes, but, otherwise, generally clean code will not be * strings or regexes, but, otherwise, generally clean code will not be
* mangled. URI rewriting can also be performed. * mangled. URI rewriting can also be performed.
* *
* @param string $content * @param string $content
* *
* @param array $options available options: * @param array $options available options:
* *
* 'id': (optional) string to identify file. E.g. file name/path * 'id': (optional) string to identify file. E.g. file name/path
* *
* 'currentDir': (default null) if given, this is assumed to be the * 'currentDir': (default null) if given, this is assumed to be the
@@ -31,10 +31,10 @@ class Minify_Lines {
* all relative URIs in import/url declarations to correctly point to * all relative URIs in import/url declarations to correctly point to
* the desired files, and prepend a comment with debugging information about * the desired files, and prepend a comment with debugging information about
* this process. * this process.
* *
* @return string * @return string
*/ */
public static function minify($content, $options = array()) public static function minify($content, $options = array())
{ {
$id = (isset($options['id']) && $options['id']) $id = (isset($options['id']) && $options['id'])
? $options['id'] ? $options['id']
@@ -66,7 +66,7 @@ class Minify_Lines {
$inComment = self::_eolInComment($line, $inComment); $inComment = self::_eolInComment($line, $inComment);
} }
$content = implode("\n", $newLines) . "\n"; $content = implode("\n", $newLines) . "\n";
// check for desired URI rewriting // check for desired URI rewriting
if (isset($options['currentDir'])) { if (isset($options['currentDir'])) {
Minify_CSS_UriRewriter::$debugText = ''; Minify_CSS_UriRewriter::$debugText = '';
@@ -76,19 +76,19 @@ class Minify_Lines {
,isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT'] ,isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']
,isset($options['symlinks']) ? $options['symlinks'] : array() ,isset($options['symlinks']) ? $options['symlinks'] : array()
); );
$content = "/* Minify_CSS_UriRewriter::\$debugText\n\n" $content = "/* Minify_CSS_UriRewriter::\$debugText\n\n"
. Minify_CSS_UriRewriter::$debugText . "*/\n" . Minify_CSS_UriRewriter::$debugText . "*/\n"
. $content; . $content;
} }
return $content; return $content;
} }
/** /**
* Is the parser within a C-style comment at the end of this line? * Is the parser within a C-style comment at the end of this line?
* *
* @param string $line current line of code * @param string $line current line of code
* *
* @param bool $inComment was the parser in a comment at the * @param bool $inComment was the parser in a comment at the
* beginning of the line? * beginning of the line?
* *
@@ -117,21 +117,22 @@ class Minify_Lines {
$line = substr($line, $pos + 2); $line = substr($line, $pos + 2);
} }
} }
return $inComment; return $inComment;
} }
/** /**
* Prepend a comment (or note) to the given line * Prepend a comment (or note) to the given line
* *
* @param string $line current line of code * @param string $line current line of code
* *
* @param string $note content of note/comment * @param string $note content of note/comment
* *
* @param bool $inComment was the parser in a comment at the * @param bool $inComment was the parser in a comment at the
* beginning of the line? * beginning of the line?
* *
* @param int $padTo minimum width of comment * @param int $padTo minimum width of comment
* *
* @return string * @return string
*/ */
private static function _addNote($line, $note, $inComment, $padTo) private static function _addNote($line, $note, $inComment, $padTo)

View File

@@ -18,6 +18,7 @@ class Minify_Loader {
$file .= strtr($class, "\\_", DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR) . '.php'; $file .= strtr($class, "\\_", DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR) . '.php';
if (is_readable($file)) { if (is_readable($file)) {
require $file; require $file;
return; return;
} }
@@ -36,6 +37,7 @@ class Minify_Loader {
{ {
$inst = new self(); $inst = new self();
spl_autoload_register(array($inst, 'loadClass')); spl_autoload_register(array($inst, 'loadClass'));
return $inst; return $inst;
} }
} }

View File

@@ -1,12 +1,12 @@
<?php <?php
/** /**
* Class Minify_Logger * Class Minify_Logger
* @package Minify * @package Minify
*/ */
/** /**
* Message logging class * Message logging class
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
* *
@@ -15,7 +15,7 @@
class Minify_Logger { class Minify_Logger {
/** /**
* Set logger object. * Set logger object.
* *
* The object should have a method "log" that accepts a value as 1st argument and * The object should have a method "log" that accepts a value as 1st argument and
* an optional string label as the 2nd. * an optional string label as the 2nd.
@@ -28,7 +28,7 @@ class Minify_Logger {
? $obj ? $obj
: null; : null;
} }
/** /**
* Pass a message to the logger (if set) * Pass a message to the logger (if set)
* *
@@ -39,7 +39,7 @@ class Minify_Logger {
if (! self::$_logger) return; if (! self::$_logger) return;
self::$_logger->log($msg, $label); self::$_logger->log($msg, $label);
} }
/** /**
* @var mixed logger object (like FirePHP) or null (i.e. no logger available) * @var mixed logger object (like FirePHP) or null (i.e. no logger available)
*/ */

View File

@@ -4,19 +4,19 @@
* *
* To use this class you must first download the PHP port of Packer * To use this class you must first download the PHP port of Packer
* and place the file "class.JavaScriptPacker.php" in /lib (or your * and place the file "class.JavaScriptPacker.php" in /lib (or your
* include_path). * include_path).
* @link http://joliclic.free.fr/php/javascript-packer/en/ * @link http://joliclic.free.fr/php/javascript-packer/en/
* *
* Be aware that, as long as HTTP encoding is used, scripts minified with JSMin * Be aware that, as long as HTTP encoding is used, scripts minified with JSMin
* will provide better client-side performance, as they need not be unpacked in * will provide better client-side performance, as they need not be unpacked in
* client-side code. * client-side code.
* *
* @package Minify * @package Minify
*/ */
/** /**
* Minify Javascript using Dean Edward's Packer * Minify Javascript using Dean Edward's Packer
* *
* @package Minify * @package Minify
*/ */
class Minify_Packer { class Minify_Packer {
@@ -24,6 +24,7 @@ class Minify_Packer {
{ {
// @todo: set encoding options based on $options :) // @todo: set encoding options based on $options :)
$packer = new JavascriptPacker($code, 'Normal', true, false); $packer = new JavascriptPacker($code, 'Normal', true, false);
return trim($packer->pack()); return trim($packer->pack());
} }
} }

View File

@@ -31,7 +31,7 @@ class Minify_ServeConfiguration {
* @param Minify_SourceInterface[] $sources * @param Minify_SourceInterface[] $sources
* @param string $selectionId * @param string $selectionId
*/ */
function __construct(array $options, array $sources = array(), $selectionId = '') public function __construct(array $options, array $sources = array(), $selectionId = '')
{ {
$this->options = $options; $this->options = $options;
$this->sources = $sources; $this->sources = $sources;

View File

@@ -1,15 +1,15 @@
<?php <?php
/** /**
* Class Minify_Source * Class Minify_Source
* @package Minify * @package Minify
*/ */
/** /**
* A content source to be minified by Minify. * A content source to be minified by Minify.
* *
* This allows per-source minification options and the mixing of files with * This allows per-source minification options and the mixing of files with
* content from other sources. * content from other sources.
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */

View File

@@ -24,6 +24,7 @@ class Minify_SourceSet {
$source->getId(), $source->getMinifier(), $source->getMinifierOptions() $source->getId(), $source->getMinifier(), $source->getMinifierOptions()
); );
} }
return md5(serialize($info)); return md5(serialize($info));
} }
} }

View File

@@ -1,17 +1,17 @@
<?php <?php
/** /**
* Class Minify_YUICompressor * Class Minify_YUICompressor
* @package Minify * @package Minify
*/ */
/** /**
* Compress Javascript/CSS using the YUI Compressor * Compress Javascript/CSS using the YUI Compressor
* *
* You must set $jarFile and $tempDir before calling the minify functions. * You must set $jarFile and $tempDir before calling the minify functions.
* Also, depending on your shell's environment, you may need to specify * Also, depending on your shell's environment, you may need to specify
* the full path to java in $javaExecutable or use putenv() to setup the * the full path to java in $javaExecutable or use putenv() to setup the
* Java environment. * Java environment.
* *
* <code> * <code>
* Minify_YUICompressor::$jarFile = '/path/to/yuicompressor-2.4.6.jar'; * Minify_YUICompressor::$jarFile = '/path/to/yuicompressor-2.4.6.jar';
* Minify_YUICompressor::$tempDir = '/tmp'; * Minify_YUICompressor::$tempDir = '/tmp';
@@ -25,7 +25,7 @@
* array('stack-size' => '2048k') * array('stack-size' => '2048k')
* *
* @todo unit tests, $options docs * @todo unit tests, $options docs
* *
* @package Minify * @package Minify
* @author Stephen Clay <steve@mrclay.org> * @author Stephen Clay <steve@mrclay.org>
*/ */
@@ -38,7 +38,7 @@ class Minify_YUICompressor {
* @var string * @var string
*/ */
public static $jarFile = null; public static $jarFile = null;
/** /**
* Writable temp directory. This must be set before calling minifyJs() * Writable temp directory. This must be set before calling minifyJs()
* or minifyCss(). * or minifyCss().
@@ -46,46 +46,46 @@ class Minify_YUICompressor {
* @var string * @var string
*/ */
public static $tempDir = null; public static $tempDir = null;
/** /**
* Filepath of "java" executable (may be needed if not in shell's PATH) * Filepath of "java" executable (may be needed if not in shell's PATH)
* *
* @var string * @var string
*/ */
public static $javaExecutable = 'java'; public static $javaExecutable = 'java';
/** /**
* Minify a Javascript string * Minify a Javascript string
* *
* @param string $js * @param string $js
* *
* @param array $options (verbose is ignored) * @param array $options (verbose is ignored)
* *
* @see http://www.julienlecomte.net/yuicompressor/README * @see http://www.julienlecomte.net/yuicompressor/README
* *
* @return string * @return string
*/ */
public static function minifyJs($js, $options = array()) public static function minifyJs($js, $options = array())
{ {
return self::_minify('js', $js, $options); return self::_minify('js', $js, $options);
} }
/** /**
* Minify a CSS string * Minify a CSS string
* *
* @param string $css * @param string $css
* *
* @param array $options (verbose is ignored) * @param array $options (verbose is ignored)
* *
* @see http://www.julienlecomte.net/yuicompressor/README * @see http://www.julienlecomte.net/yuicompressor/README
* *
* @return string * @return string
*/ */
public static function minifyCss($css, $options = array()) public static function minifyCss($css, $options = array())
{ {
return self::_minify('css', $css, $options); return self::_minify('css', $css, $options);
} }
private static function _minify($type, $content, $options) private static function _minify($type, $content, $options)
{ {
self::_prepare(); self::_prepare();
@@ -98,9 +98,10 @@ class Minify_YUICompressor {
if ($result_code != 0) { if ($result_code != 0) {
throw new Exception('Minify_YUICompressor : YUI compressor execution failed.'); throw new Exception('Minify_YUICompressor : YUI compressor execution failed.');
} }
return implode("\n", $output); return implode("\n", $output);
} }
private static function _getCmd($userOptions, $type, $tmpFile) private static function _getCmd($userOptions, $type, $tmpFile)
{ {
$o = array_merge( $o = array_merge(
@@ -122,21 +123,22 @@ class Minify_YUICompressor {
. ' -jar ' . escapeshellarg(self::$jarFile) . ' -jar ' . escapeshellarg(self::$jarFile)
. " --type {$type}" . " --type {$type}"
. (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset']) . (preg_match('/^[\\da-zA-Z0-9\\-]+$/', $o['charset'])
? " --charset {$o['charset']}" ? " --charset {$o['charset']}"
: '') : '')
. (is_numeric($o['line-break']) && $o['line-break'] >= 0 . (is_numeric($o['line-break']) && $o['line-break'] >= 0
? ' --line-break ' . (int)$o['line-break'] ? ' --line-break ' . (int)$o['line-break']
: ''); : '');
if ($type === 'js') { if ($type === 'js') {
foreach (array('nomunge', 'preserve-semi', 'disable-optimizations') as $opt) { foreach (array('nomunge', 'preserve-semi', 'disable-optimizations') as $opt) {
$cmd .= $o[$opt] $cmd .= $o[$opt]
? " --{$opt}" ? " --{$opt}"
: ''; : '';
} }
} }
return $cmd . ' ' . escapeshellarg($tmpFile); return $cmd . ' ' . escapeshellarg($tmpFile);
} }
private static function _prepare() private static function _prepare()
{ {
if (! is_file(self::$jarFile)) { if (! is_file(self::$jarFile)) {

View File

@@ -1,4 +1,4 @@
<?php <?php
namespace MrClay; namespace MrClay;
@@ -19,15 +19,15 @@ use InvalidArgumentException;
* @license http://www.opensource.org/licenses/mit-license.php MIT License * @license http://www.opensource.org/licenses/mit-license.php MIT License
*/ */
class Cli { class Cli {
/** /**
* @var array validation errors * @var array validation errors
*/ */
public $errors = array(); public $errors = array();
/** /**
* @var array option values available after validation. * @var array option values available after validation.
* *
* E.g. array( * E.g. array(
* 'a' => false // option was missing * 'a' => false // option was missing
* ,'b' => true // option was present * ,'b' => true // option was present
@@ -67,7 +67,7 @@ class Cli {
* @var resource * @var resource
*/ */
protected $_stdout = null; protected $_stdout = null;
/** /**
* @param bool $exitIfNoStdin (default true) Exit() if STDIN is not defined * @param bool $exitIfNoStdin (default true) Exit() if STDIN is not defined
*/ */
@@ -116,6 +116,7 @@ class Cli {
$arg = new Arg($required); $arg = new Arg($required);
} }
$this->_args[$letter] = $arg; $this->_args[$letter] = $arg;
return $arg; return $arg;
} }
@@ -130,7 +131,7 @@ class Cli {
/* /*
* Read and validate options * Read and validate options
* *
* @return bool true if all options are valid * @return bool true if all options are valid
*/ */
public function validate() public function validate()
@@ -139,17 +140,17 @@ class Cli {
$this->errors = array(); $this->errors = array();
$this->values = array(); $this->values = array();
$this->_stdin = null; $this->_stdin = null;
if ($this->isHelpRequest) { if ($this->isHelpRequest) {
return false; return false;
} }
$lettersUsed = ''; $lettersUsed = '';
foreach ($this->_args as $letter => $arg) { foreach ($this->_args as $letter => $arg) {
/* @var Arg $arg */ /* @var Arg $arg */
$options .= $letter; $options .= $letter;
$lettersUsed .= $letter; $lettersUsed .= $letter;
if ($arg->mayHaveValue || $arg->mustHaveValue) { if ($arg->mayHaveValue || $arg->mustHaveValue) {
$options .= ($arg->mustHaveValue ? ':' : '::'); $options .= ($arg->mustHaveValue ? ':' : '::');
} }
@@ -201,14 +202,15 @@ class Cli {
array_splice($argvCopy, $k, 2); array_splice($argvCopy, $k, 2);
} }
} }
// check that value isn't really another option // check that value isn't really another option
if (strlen($lettersUsed) > 1) { if (strlen($lettersUsed) > 1) {
$pattern = "/^-[" . str_replace($letter, '', $lettersUsed) . "]/i"; $pattern = "/^-[" . str_replace($letter, '', $lettersUsed) . "]/i";
if (preg_match($pattern, $v)) { if (preg_match($pattern, $v)) {
$this->addError($letter, "Value was read as another option: %s", $v); $this->addError($letter, "Value was read as another option: %s", $v);
return false; return false;
} }
} }
if ($arg->assertFile || $arg->assertDir) { if ($arg->assertFile || $arg->assertDir) {
if ($v[0] !== '/' && $v[0] !== '~') { if ($v[0] !== '/' && $v[0] !== '~') {
@@ -249,6 +251,7 @@ class Cli {
} }
$this->moreArgs = $argvCopy; $this->moreArgs = $argvCopy;
reset($this->moreArgs); reset($this->moreArgs);
return empty($this->errors); return empty($this->errors);
} }
@@ -270,12 +273,13 @@ class Cli {
$r[$k] = $v; $r[$k] = $v;
} }
} }
return $r; return $r;
} }
/** /**
* Get a short list of errors with options * Get a short list of errors with options
* *
* @return string * @return string
*/ */
public function getErrorReport() public function getErrorReport()
@@ -288,6 +292,7 @@ class Cli {
$r .= " $letter : " . implode(', ', $arr) . "\n"; $r .= " $letter : " . implode(', ', $arr) . "\n";
} }
$r .= "\n"; $r .= "\n";
return $r; return $r;
} }
@@ -318,9 +323,10 @@ class Cli {
$desc = wordwrap($desc, 70); $desc = wordwrap($desc, 70);
$r .= $flag . str_replace("\n", "\n ", $desc) . "\n\n"; $r .= $flag . str_replace("\n", "\n ", $desc) . "\n\n";
} }
return $r; return $r;
} }
/** /**
* Get resource of open input stream. May be STDIN or a file pointer * Get resource of open input stream. May be STDIN or a file pointer
* to the file specified by an option with 'STDIN'. * to the file specified by an option with 'STDIN'.
@@ -333,17 +339,18 @@ class Cli {
return STDIN; return STDIN;
} else { } else {
$this->_stdin = fopen($this->_stdin, 'rb'); $this->_stdin = fopen($this->_stdin, 'rb');
return $this->_stdin; return $this->_stdin;
} }
} }
public function closeInput() public function closeInput()
{ {
if (null !== $this->_stdin) { if (null !== $this->_stdin) {
fclose($this->_stdin); fclose($this->_stdin);
} }
} }
/** /**
* Get resource of open output stream. May be STDOUT or a file pointer * Get resource of open output stream. May be STDOUT or a file pointer
* to the file specified by an option with 'STDOUT'. The file will be * to the file specified by an option with 'STDOUT'. The file will be
@@ -357,10 +364,11 @@ class Cli {
return STDOUT; return STDOUT;
} else { } else {
$this->_stdout = fopen($this->_stdout, 'wb'); $this->_stdout = fopen($this->_stdout, 'wb');
return $this->_stdout; return $this->_stdout;
} }
} }
public function closeOutput() public function closeOutput()
{ {
if (null !== $this->_stdout) { if (null !== $this->_stdout) {

View File

@@ -97,6 +97,7 @@ class Arg {
public function useAsOutfile() public function useAsOutfile()
{ {
$this->spec['useAsOutfile'] = true; $this->spec['useAsOutfile'] = true;
return $this->assertFile()->assertWritable(); return $this->assertFile()->assertWritable();
} }
@@ -109,6 +110,7 @@ class Arg {
public function useAsInfile() public function useAsInfile()
{ {
$this->spec['useAsInfile'] = true; $this->spec['useAsInfile'] = true;
return $this->assertFile()->assertReadable(); return $this->assertFile()->assertReadable();
} }
@@ -127,6 +129,7 @@ class Arg {
public function setDescription($desc) public function setDescription($desc)
{ {
$this->description = $desc; $this->description = $desc;
return $this; return $this;
} }
@@ -164,6 +167,7 @@ class Arg {
} else { } else {
throw new BadMethodCallException('Method does not exist'); throw new BadMethodCallException('Method does not exist');
} }
return $this; return $this;
} }
@@ -178,6 +182,7 @@ class Arg {
if (array_key_exists($name, $this->spec)) { if (array_key_exists($name, $this->spec)) {
return $this->spec[$name]; return $this->spec[$name];
} }
return null; return null;
} }
} }