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

(WIP) Move to dynamic objects, adds env & source factory

This commit is contained in:
Steve Clay
2014-09-22 15:04:05 -04:00
parent 4910238a9f
commit c75f97f3bc
8 changed files with 394 additions and 181 deletions

View File

@@ -31,17 +31,30 @@ if (isset($_GET['test'])) {
require "$min_libPath/Minify/Loader.php"; require "$min_libPath/Minify/Loader.php";
Minify_Loader::register(); Minify_Loader::register();
Minify::$uploaderHoursBehind = $min_uploaderHoursBehind; $server = $_SERVER;
Minify::setCache(
isset($min_cachePath) ? $min_cachePath : ''
,$min_cacheFileLocking
);
if ($min_documentRoot) { if ($min_documentRoot) {
$_SERVER['DOCUMENT_ROOT'] = $min_documentRoot; $server['DOCUMENT_ROOT'] = $min_documentRoot;
Minify::$isDocRootSet = true;
} }
$env = new Minify_Env(array(
'server' => $server,
// move these...
'allowDebug' => $min_allowDebugFlag,
'uploaderHoursBehind' => $min_uploaderHoursBehind,
));
if (!isset($min_cachePath)) {
$cache = new Minify_Cache_File('', $min_cacheFileLocking);
} elseif (is_object($min_cachePath)) {
// let type hinting catch type error
$cache = $min_cachePath;
} else {
$cache = new Minify_Cache_File($min_cachePath, $min_cacheFileLocking);
}
$server = new Minify($env, $cache);
$min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks; $min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks;
// auto-add targets to allowDirs // auto-add targets to allowDirs
foreach ($min_symlinks as $uri => $target) { foreach ($min_symlinks as $uri => $target) {
@@ -73,13 +86,30 @@ if (isset($_GET['g'])) {
// serve or redirect // serve or redirect
if (isset($_GET['f']) || isset($_GET['g'])) { if (isset($_GET['f']) || isset($_GET['g'])) {
if (! isset($min_serveController)) { if (! isset($min_serveController)) {
$sourceFactoryOptions = array(
'noMinPattern' => '@[-\\.]min\\.(?:js|css)$@i', // matched against basename
'uploaderHoursBehind' => 0,
'fileChecker' => array($this, 'checkIsFile'),
'resolveDocRoot' => true,
'checkAllowDirs' => true,
'allowDirs' => array($env->getDocRoot()),
);
if (isset($min_serveOptions['minApp']['noMinPattern'])) {
$sourceFactoryOptions['noMinPattern'] = $min_serveOptions['minApp']['noMinPattern'];
}
$sourceFactory = new Minify_Source_Factory($env, $sourceFactoryOptions);
$min_serveController = new Minify_Controller_MinApp(); $min_serveController = new Minify_Controller_MinApp();
} }
Minify::serve($min_serveController, $min_serveOptions); $server->serve($min_serveController, $min_serveOptions);
} elseif ($min_enableBuilder) { } elseif ($min_enableBuilder) {
header('Location: builder/'); header('Location: builder/');
exit; exit;
} else { } else {
header('Location: /'); header('Location: /');
exit; exit;

View File

@@ -31,20 +31,12 @@ class Minify {
// Apache default and what Yahoo! uses.. // Apache default and what Yahoo! uses..
const TYPE_JS = 'application/x-javascript'; const TYPE_JS = 'application/x-javascript';
const URL_DEBUG = 'http://code.google.com/p/minify/wiki/Debugging'; const URL_DEBUG = 'http://code.google.com/p/minify/wiki/Debugging';
/** public function __construct(Minify_Env $env, Minify_CacheInterface $cache) {
* How many hours behind are the file modification times of uploaded files? $this->env = $env;
* $this->cache = $cache;
* If you upload files from Windows to a non-Windows server, Windows may report }
* incorrect mtimes for the files. Immediately after modifying and uploading a
* file, use the touch command to update the mtime on the server. If the mtime
* jumps ahead by a number of hours, set this variable to that number. If the mtime
* moves back, this should not be needed.
*
* @var int $uploaderHoursBehind
*/
public static $uploaderHoursBehind = 0;
/** /**
* If this string is not empty AND the serve() option 'bubbleCssImports' is * If this string is not empty AND the serve() option 'bubbleCssImports' is
* NOT set, then serve() will check CSS files for @import declarations that * NOT set, then serve() will check CSS files for @import declarations that
@@ -53,50 +45,16 @@ class Minify {
* *
* @var string $importWarning * @var string $importWarning
*/ */
public static $importWarning = "/* See http://code.google.com/p/minify/wiki/CommonProblems#@imports_can_appear_in_invalid_locations_in_combined_CSS_files */\n"; public $importWarning = "/* See http://code.google.com/p/minify/wiki/CommonProblems#@imports_can_appear_in_invalid_locations_in_combined_CSS_files */\n";
/** /**
* Has the DOCUMENT_ROOT been set in user code? * Replace the cache object
* *
* @var bool * @param Minify_CacheInterface $cache object
*/ */
public static $isDocRootSet = false; public function setCache(Minify_CacheInterface $cache)
/**
* Specify a cache object (with identical interface as Minify_Cache_File) or
* a path to use with Minify_Cache_File.
*
* If not called, Minify will not use a cache and, for each 200 response, will
* need to recombine files, minify and encode the output.
*
* @param mixed $cache object with identical interface as Minify_Cache_File or
* a directory path, or null to disable caching. (default = '')
*
* @param bool $fileLocking (default = true) This only applies if the first
* parameter is a string.
*
* @return null
*/
public static function setCache($cache = '', $fileLocking = true)
{ {
if (is_string($cache)) { $this->cache = $cache;
self::$_cache = new Minify_Cache_File($cache, $fileLocking);
} else {
self::$_cache = $cache;
}
}
/**
* Get Minify cache, if no Cache is defined, create Minify_Cache_Null
*
* @return Minify_CacheInterface
*/
public static function getCache()
{
if (!self::$_cache) {
self::$_cache = new Minify_Cache_Null();
}
return self::$_cache;
} }
/** /**
@@ -174,12 +132,8 @@ class Minify {
* *
* @throws Exception * @throws Exception
*/ */
public static function serve($controller, $options = array()) public function serve($controller, $options = array())
{ {
if (! self::$isDocRootSet && 0 === stripos(PHP_OS, 'win')) {
self::setDocRoot();
}
if (is_string($controller)) { if (is_string($controller)) {
// make $controller into object // make $controller into object
$class = 'Minify_Controller_' . $controller; $class = 'Minify_Controller_' . $controller;
@@ -191,15 +145,15 @@ class Minify {
// controller defaults // controller defaults
$options = $controller->setupSources($options); $options = $controller->setupSources($options);
$options = $controller->analyzeSources($options); $options = $controller->analyzeSources($options);
self::$_options = $controller->mixInDefaultOptions($options); $this->options = $controller->mixInDefaultOptions($options);
// check request validity // check request validity
if (! $controller->sources) { if (! $controller->sources) {
// invalid request! // invalid request!
if (! self::$_options['quiet']) { if (! $this->options['quiet']) {
self::_errorExit(self::$_options['badRequestHeader'], self::URL_DEBUG); $this->errorExit($this->options['badRequestHeader'], self::URL_DEBUG);
} else { } else {
list(,$statusCode) = explode(' ', self::$_options['badRequestHeader']); list(,$statusCode) = explode(' ', $this->options['badRequestHeader']);
return array( return array(
'success' => false 'success' => false
,'statusCode' => (int)$statusCode ,'statusCode' => (int)$statusCode
@@ -209,46 +163,46 @@ class Minify {
} }
} }
self::$_controller = $controller; $this->controller = $controller;
if (self::$_options['debug']) { if ($this->options['debug']) {
self::_setupDebug($controller->sources); $this->setupDebug($controller->sources);
self::$_options['maxAge'] = 0; $this->options['maxAge'] = 0;
} }
// determine encoding // determine encoding
if (self::$_options['encodeOutput']) { if ($this->options['encodeOutput']) {
$sendVary = true; $sendVary = true;
if (self::$_options['encodeMethod'] !== null) { if ($this->options['encodeMethod'] !== null) {
// controller specifically requested this // controller specifically requested this
$contentEncoding = self::$_options['encodeMethod']; $contentEncoding = $this->options['encodeMethod'];
} else { } else {
// sniff request header // sniff request header
// depending on what the client accepts, $contentEncoding may be // depending on what the client accepts, $contentEncoding may be
// 'x-gzip' while our internal encodeMethod is 'gzip'. Calling // 'x-gzip' while our internal encodeMethod is 'gzip'. Calling
// getAcceptedEncoding(false, false) leaves out compress and deflate as options. // getAcceptedEncoding(false, false) leaves out compress and deflate as options.
list(self::$_options['encodeMethod'], $contentEncoding) = HTTP_Encoder::getAcceptedEncoding(false, false); list($this->options['encodeMethod'], $contentEncoding) = HTTP_Encoder::getAcceptedEncoding(false, false);
$sendVary = ! HTTP_Encoder::isBuggyIe(); $sendVary = ! HTTP_Encoder::isBuggyIe();
} }
} else { } else {
self::$_options['encodeMethod'] = ''; // identity (no encoding) $this->options['encodeMethod'] = ''; // identity (no encoding)
} }
// check client cache // check client cache
$cgOptions = array( $cgOptions = array(
'lastModifiedTime' => self::$_options['lastModifiedTime'] 'lastModifiedTime' => $this->options['lastModifiedTime']
,'isPublic' => self::$_options['isPublic'] ,'isPublic' => $this->options['isPublic']
,'encoding' => self::$_options['encodeMethod'] ,'encoding' => $this->options['encodeMethod']
); );
if (self::$_options['maxAge'] > 0) { if ($this->options['maxAge'] > 0) {
$cgOptions['maxAge'] = self::$_options['maxAge']; $cgOptions['maxAge'] = $this->options['maxAge'];
} elseif (self::$_options['debug']) { } elseif ($this->options['debug']) {
$cgOptions['invalidate'] = true; $cgOptions['invalidate'] = true;
} }
$cg = new HTTP_ConditionalGet($cgOptions); $cg = new HTTP_ConditionalGet($cgOptions);
if ($cg->cacheIsValid) { if ($cg->cacheIsValid) {
// client's cache is valid // client's cache is valid
if (! self::$_options['quiet']) { if (! $this->options['quiet']) {
$cg->sendHeaders(); $cg->sendHeaders();
return; return;
} else { } else {
@@ -265,59 +219,59 @@ class Minify {
unset($cg); unset($cg);
} }
if (self::$_options['contentType'] === self::TYPE_CSS if ($this->options['contentType'] === self::TYPE_CSS
&& self::$_options['rewriteCssUris']) { && $this->options['rewriteCssUris']) {
foreach($controller->sources as $key => $source) { foreach($controller->sources as $key => $source) {
$source->setupUriRewrites(); $source->setupUriRewrites();
} }
} }
// check server cache // check server cache
if (null !== self::$_cache && ! self::$_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 = self::_getCacheId(); $cacheId = $this->_getCacheId();
$fullCacheId = (self::$_options['encodeMethod']) $fullCacheId = ($this->options['encodeMethod'])
? $cacheId . '.gz' ? $cacheId . '.gz'
: $cacheId; : $cacheId;
// check cache for valid entry // check cache for valid entry
$cacheIsReady = self::$_cache->isValid($fullCacheId, self::$_options['lastModifiedTime']); $cacheIsReady = $this->cache->isValid($fullCacheId, $this->options['lastModifiedTime']);
if ($cacheIsReady) { if ($cacheIsReady) {
$cacheContentLength = self::$_cache->getSize($fullCacheId); $cacheContentLength = $this->cache->getSize($fullCacheId);
} else { } else {
// generate & cache content // generate & cache content
try { try {
$content = self::_combineMinify(); $content = $this->combineMinify();
} catch (Exception $e) { } catch (Exception $e) {
self::$_controller->log($e->getMessage()); $this->controller->log($e->getMessage());
if (! self::$_options['quiet']) { if (! $this->options['quiet']) {
self::_errorExit(self::$_options['errorHeader'], self::URL_DEBUG); $this->errorExit($this->options['errorHeader'], self::URL_DEBUG);
} }
throw $e; throw $e;
} }
self::$_cache->store($cacheId, $content); $this->cache->store($cacheId, $content);
if (function_exists('gzencode') && self::$_options['encodeMethod']) { if (function_exists('gzencode') && $this->options['encodeMethod']) {
self::$_cache->store($cacheId . '.gz', gzencode($content, self::$_options['encodeLevel'])); $this->cache->store($cacheId . '.gz', gzencode($content, $this->options['encodeLevel']));
} }
} }
} else { } else {
// no cache // no cache
$cacheIsReady = false; $cacheIsReady = false;
try { try {
$content = self::_combineMinify(); $content = $this->combineMinify();
} catch (Exception $e) { } catch (Exception $e) {
self::$_controller->log($e->getMessage()); $this->controller->log($e->getMessage());
if (! self::$_options['quiet']) { if (! $this->options['quiet']) {
self::_errorExit(self::$_options['errorHeader'], self::URL_DEBUG); $this->errorExit($this->options['errorHeader'], self::URL_DEBUG);
} }
throw $e; throw $e;
} }
} }
if (! $cacheIsReady && self::$_options['encodeMethod']) { if (! $cacheIsReady && $this->options['encodeMethod']) {
// still need to encode // still need to encode
$content = gzencode($content, self::$_options['encodeLevel']); $content = gzencode($content, $this->options['encodeLevel']);
} }
// add headers // add headers
@@ -327,23 +281,23 @@ class Minify {
? mb_strlen($content, '8bit') ? mb_strlen($content, '8bit')
: strlen($content) : strlen($content)
); );
$headers['Content-Type'] = self::$_options['contentTypeCharset'] $headers['Content-Type'] = $this->options['contentTypeCharset']
? self::$_options['contentType'] . '; charset=' . self::$_options['contentTypeCharset'] ? $this->options['contentType'] . '; charset=' . $this->options['contentTypeCharset']
: self::$_options['contentType']; : $this->options['contentType'];
if (self::$_options['encodeMethod'] !== '') { if ($this->options['encodeMethod'] !== '') {
$headers['Content-Encoding'] = $contentEncoding; $headers['Content-Encoding'] = $contentEncoding;
} }
if (self::$_options['encodeOutput'] && $sendVary) { if ($this->options['encodeOutput'] && $sendVary) {
$headers['Vary'] = 'Accept-Encoding'; $headers['Vary'] = 'Accept-Encoding';
} }
if (! self::$_options['quiet']) { if (! $this->options['quiet']) {
// output headers & content // output headers & content
foreach ($headers as $name => $val) { foreach ($headers as $name => $val) {
header($name . ': ' . $val); header($name . ': ' . $val);
} }
if ($cacheIsReady) { if ($cacheIsReady) {
self::$_cache->display($fullCacheId); $this->cache->display($fullCacheId);
} else { } else {
echo $content; echo $content;
} }
@@ -352,7 +306,7 @@ class Minify {
'success' => true 'success' => true
,'statusCode' => 200 ,'statusCode' => 200
,'content' => $cacheIsReady ,'content' => $cacheIsReady
? self::$_cache->fetch($fullCacheId) ? $this->cache->fetch($fullCacheId)
: $content : $content
,'headers' => $headers ,'headers' => $headers
); );
@@ -371,68 +325,53 @@ class Minify {
* *
* @return string * @return string
*/ */
public static function combine($sources, $options = array()) public function combine($sources, $options = array())
{ {
$cache = self::$_cache; $cache = $this->cache;
self::$_cache = null; $this->cache = null;
$options = array_merge(array( $options = array_merge(array(
'files' => (array)$sources 'files' => (array)$sources
,'quiet' => true ,'quiet' => true
,'encodeMethod' => '' ,'encodeMethod' => ''
,'lastModifiedTime' => 0 ,'lastModifiedTime' => 0
), $options); ), $options);
$out = self::serve('Files', $options); $out = $this->serve('Files', $options);
self::$_cache = $cache; $this->cache = $cache;
return $out['content']; return $out['content'];
} }
/** /**
* Set $_SERVER['DOCUMENT_ROOT']. On IIS, the value is created from SCRIPT_FILENAME and SCRIPT_NAME. * @var Minify_Env
*
* @param string $docRoot value to use for DOCUMENT_ROOT
*/ */
public static function setDocRoot($docRoot = '') protected $env;
{
self::$isDocRootSet = true;
if ($docRoot) {
$_SERVER['DOCUMENT_ROOT'] = $docRoot;
} elseif (isset($_SERVER['SERVER_SOFTWARE'])
&& 0 === strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/')) {
$_SERVER['DOCUMENT_ROOT'] = substr(
$_SERVER['SCRIPT_FILENAME']
,0
,strlen($_SERVER['SCRIPT_FILENAME']) - strlen($_SERVER['SCRIPT_NAME']));
$_SERVER['DOCUMENT_ROOT'] = rtrim($_SERVER['DOCUMENT_ROOT'], '\\');
}
}
/** /**
* Any Minify_Cache_* object or null (i.e. no server cache is used) * Any Minify_Cache_* object or null (i.e. no server cache is used)
* *
* @var Minify_CacheInterface * @var Minify_CacheInterface
*/ */
private static $_cache = null; private $cache = null;
/** /**
* Active controller for current request * Active controller for current request
* *
* @var Minify_Controller_Base * @var Minify_Controller_Base
*/ */
protected static $_controller = null; protected $controller = null;
/** /**
* Options for current request * Options for current request
* *
* @var array * @var array
*/ */
protected static $_options = null; protected $options = null;
/** /**
* @param string $header * @param string $header
* *
* @param string $url * @param string $url
*/ */
protected static function _errorExit($header, $url) protected function errorExit($header, $url)
{ {
$url = htmlspecialchars($url, ENT_QUOTES); $url = htmlspecialchars($url, ENT_QUOTES);
list(,$h1) = explode(' ', $header, 2); list(,$h1) = explode(' ', $header, 2);
@@ -449,9 +388,9 @@ class Minify {
/** /**
* Set up sources to use Minify_Lines * Set up sources to use Minify_Lines
* *
* @param Minify_Source[] $sources Minify_Source instances * @param Minify_SourceInterface[] $sources
*/ */
protected static function _setupDebug($sources) protected function setupDebug($sources)
{ {
foreach ($sources as $source) { foreach ($sources as $source) {
$source->setMinifier(array('Minify_Lines', 'minify')); $source->setMinifier(array('Minify_Lines', 'minify'));
@@ -469,9 +408,9 @@ class Minify {
* *
* @throws Exception * @throws Exception
*/ */
protected static function _combineMinify() protected function combineMinify()
{ {
$type = self::$_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
@@ -481,19 +420,19 @@ class Minify {
// allow the user to pass a particular array of options to each // allow the user to pass a particular array of options to each
// minifier (designated by type). source objects may still override // minifier (designated by type). source objects may still override
// these // these
$defaultOptions = isset(self::$_options['minifierOptions'][$type]) $defaultOptions = isset($this->options['minifierOptions'][$type])
? self::$_options['minifierOptions'][$type] ? $this->options['minifierOptions'][$type]
: array(); : array();
// if minifier not set, default is no minification. source objects // if minifier not set, default is no minification. source objects
// may still override this // may still override this
$defaultMinifier = isset(self::$_options['minifiers'][$type]) $defaultMinifier = isset($this->options['minifiers'][$type])
? self::$_options['minifiers'][$type] ? $this->options['minifiers'][$type]
: false; : false;
// process groups of sources with identical minifiers/options // process groups of sources with identical minifiers/options
$content = array(); $content = array();
$i = 0; $i = 0;
$l = count(self::$_controller->sources); $l = count($this->controller->sources);
$groupToProcessTogether = array(); $groupToProcessTogether = array();
$lastMinifier = null; $lastMinifier = null;
$lastOptions = null; $lastOptions = null;
@@ -501,7 +440,7 @@ class Minify {
// get next source // get next source
$source = null; $source = null;
if ($i < $l) { if ($i < $l) {
$source = self::$_controller->sources[$i]; $source = $this->controller->sources[$i];
/* @var Minify_Source $source */ /* @var Minify_Source $source */
$sourceContent = $source->getContent(); $sourceContent = $source->getContent();
@@ -546,15 +485,15 @@ class Minify {
$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 = self::_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 (self::$_options['postprocessorRequire']) { if ($this->options['postprocessorRequire']) {
require_once self::$_options['postprocessorRequire']; require_once $this->options['postprocessorRequire'];
} }
if (self::$_options['postprocessor']) { if ($this->options['postprocessor']) {
$content = call_user_func(self::$_options['postprocessor'], $content, $type); $content = call_user_func($this->options['postprocessor'], $content, $type);
} }
return $content; return $content;
} }
@@ -568,17 +507,17 @@ class Minify {
* *
* @return string * @return string
*/ */
protected static function _getCacheId($prefix = 'minify') protected function _getCacheId($prefix = 'minify')
{ {
$name = preg_replace('/[^a-zA-Z0-9\\.=_,]/', '', self::$_controller->selectionId); $name = preg_replace('/[^a-zA-Z0-9\\.=_,]/', '', $this->controller->selectionId);
$name = preg_replace('/\\.+/', '.', $name); $name = preg_replace('/\\.+/', '.', $name);
$name = substr($name, 0, 100 - 34 - strlen($prefix)); $name = substr($name, 0, 100 - 34 - strlen($prefix));
$md5 = md5(serialize(array( $md5 = md5(serialize(array(
Minify_SourceSet::getDigest(self::$_controller->sources) Minify_SourceSet::getDigest($this->controller->sources)
,self::$_options['minifiers'] ,$this->options['minifiers']
,self::$_options['minifierOptions'] ,$this->options['minifierOptions']
,self::$_options['postprocessor'] ,$this->options['postprocessor']
,self::$_options['bubbleCssImports'] ,$this->options['bubbleCssImports']
,self::VERSION ,self::VERSION
))); )));
return "{$prefix}_{$name}_{$md5}"; return "{$prefix}_{$name}_{$md5}";
@@ -591,13 +530,13 @@ class Minify {
* *
* @return string * @return string
*/ */
protected static function _handleCssImports($css) protected function handleCssImports($css)
{ {
if (self::$_options['bubbleCssImports']) { if ($this->options['bubbleCssImports']) {
// 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);
} else if ('' !== self::$importWarning) { } else if ('' !== $this->importWarning) {
// remove comments so we don't mistake { in a comment as a block // remove comments so we don't mistake { in a comment as a block
$noCommentCss = preg_replace('@/\\*[\\s\\S]*?\\*/@', '', $css); $noCommentCss = preg_replace('@/\\*[\\s\\S]*?\\*/@', '', $css);
$lastImportPos = strrpos($noCommentCss, '@import'); $lastImportPos = strrpos($noCommentCss, '@import');
@@ -607,7 +546,7 @@ class Minify {
&& $firstBlockPos < $lastImportPos && $firstBlockPos < $lastImportPos
) { ) {
// { appears before @import : prepend warning // { appears before @import : prepend warning
$css = self::$importWarning . $css; $css = $this->importWarning . $css;
} }
} }
return $css; return $css;

View File

@@ -3,6 +3,9 @@
/** /**
* Class Minify_Cache_Null * Class Minify_Cache_Null
* *
* If this is used, Minify will not use a cache and, for each 200 response, will
* need to recombine files, minify and encode the output.
*
* @package Minify * @package Minify
*/ */
class Minify_Cache_Null implements Minify_CacheInterface { class Minify_Cache_Null implements Minify_CacheInterface {

View File

@@ -38,7 +38,8 @@ abstract class Minify_Controller_Base {
* *
* @return array options for Minify * @return array options for Minify
*/ */
public function getDefaultMinifyOptions() { public function getDefaultMinifyOptions()
{
return array( return array(
'isPublic' => true 'isPublic' => true
,'encodeOutput' => function_exists('gzdeflate') ,'encodeOutput' => function_exists('gzdeflate')
@@ -71,7 +72,8 @@ abstract class Minify_Controller_Base {
* *
* @return array minifier callbacks for common types * @return array minifier callbacks for common types
*/ */
public function getDefaultMinifers() { public function getDefaultMinifers()
{
$ret[Minify::TYPE_JS] = array('JSMin', 'minify'); $ret[Minify::TYPE_JS] = array('JSMin', 'minify');
$ret[Minify::TYPE_CSS] = array('Minify_CSS', 'minify'); $ret[Minify::TYPE_CSS] = array('Minify_CSS', 'minify');
$ret[Minify::TYPE_HTML] = array('Minify_HTML', 'minify'); $ret[Minify::TYPE_HTML] = array('Minify_HTML', 'minify');
@@ -215,7 +217,8 @@ abstract class Minify_Controller_Base {
* *
* @return null * @return null
*/ */
public function log($msg) { public function log($msg)
{
Minify_Logger::log($msg); Minify_Logger::log($msg);
} }
} }

95
min/lib/Minify/Env.php Normal file
View File

@@ -0,0 +1,95 @@
<?php
class Minify_Env {
/**
* How many hours behind are the file modification times of uploaded files?
*
* If you upload files from Windows to a non-Windows server, Windows may report
* incorrect mtimes for the files. Immediately after modifying and uploading a
* file, use the touch command to update the mtime on the server. If the mtime
* jumps ahead by a number of hours, set this variable to that number. If the mtime
* moves back, this should not be needed.
*
* @var int $uploaderHoursBehind
*/
protected $uploaderHoursBehind = 0;
/**
* @return null
*/
public function getDocRoot()
{
return $this->server['DOCUMENT_ROOT'];
}
/**
* @return null
*/
public function getRequestUri()
{
return $this->server['REQUEST_URI'];
}
public function __construct($options = array())
{
$options = array_merge(array(
'server' => $_SERVER,
'get' => $_GET,
'cookie' => $_COOKIE,
), $options);
$this->server = $options['server'];
if (empty($this->server['DOCUMENT_ROOT'])) {
$this->server['DOCUMENT_ROOT'] = $this->computeDocRoot($options['server']);
}
$this->get = $options['get'];
$this->cookie = $options['cookie'];
}
public function server($key)
{
return isset($this->server[$key])
? $this->server[$key]
: null;
}
public function cookie($key)
{
return isset($this->cookie[$key])
? $this->cookie[$key]
: null;
}
public function get($key)
{
return isset($this->get[$key])
? $this->get[$key]
: null;
}
protected $server = null;
protected $get = null;
protected $cookie = null;
/**
* Compute $_SERVER['DOCUMENT_ROOT'] for IIS using SCRIPT_FILENAME and SCRIPT_NAME.
*
* @param array $server
* @return string
*/
protected function computeDocRoot(array $server)
{
if (isset($server['SERVER_SOFTWARE'])
&& 0 === strpos($server['SERVER_SOFTWARE'], 'Microsoft-IIS/')) {
$docRoot = substr(
$server['SCRIPT_FILENAME']
,0
,strlen($server['SCRIPT_FILENAME']) - strlen($server['SCRIPT_NAME']));
$docRoot = rtrim($docRoot, '\\');
} else {
throw new InvalidArgumentException('DOCUMENT_ROOT is not provided and could not be computed');
}
return $docRoot;
}
}

View File

@@ -18,42 +18,48 @@ class Minify_Source implements Minify_SourceInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getLastModified() { public function getLastModified()
{
return $this->lastModified; return $this->lastModified;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getMinifier() { public function getMinifier()
{
return $this->minifier; return $this->minifier;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setMinifier($minifier) { public function setMinifier($minifier)
{
$this->minifier = $minifier; $this->minifier = $minifier;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getMinifierOptions() { public function getMinifierOptions()
{
return $this->minifyOptions; return $this->minifyOptions;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setMinifierOptions(array $options) { public function setMinifierOptions(array $options)
{
$this->minifyOptions = $options; $this->minifyOptions = $options;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getContentType() { public function getContentType()
{
return $this->contentType; return $this->contentType;
} }
@@ -89,9 +95,12 @@ class Minify_Source implements Minify_SourceInterface {
} }
$this->filepath = $spec['filepath']; $this->filepath = $spec['filepath'];
$this->_id = $spec['filepath']; $this->_id = $spec['filepath'];
$this->lastModified = filemtime($spec['filepath'])
$this->lastModified = filemtime($spec['filepath']);
if (!empty($spec['uploaderHoursBehind'])) {
// offset for Windows uploaders with out of sync clocks // offset for Windows uploaders with out of sync clocks
+ round(Minify::$uploaderHoursBehind * 3600); $this->lastModified += round($spec['uploaderHoursBehind'] * 3600);
}
} elseif (isset($spec['id'])) { } elseif (isset($spec['id'])) {
$this->_id = 'id::' . $spec['id']; $this->_id = 'id::' . $spec['id'];
if (isset($spec['content'])) { if (isset($spec['content'])) {
@@ -191,4 +200,3 @@ class Minify_Source implements Minify_SourceInterface {
*/ */
protected $_id = null; protected $_id = null;
} }

View File

@@ -0,0 +1,132 @@
<?php
class Minify_Source_Factory {
protected $options;
protected $handlers = array();
protected $env;
public function __construct(Minify_Env $env, array $options = array())
{
$this->env = $env;
$this->options = array_merge(array(
'noMinPattern' => '@[-\\.]min\\.(?:js|css)$@i', // matched against basename
'uploaderHoursBehind' => 0,
'fileChecker' => array($this, 'checkIsFile'),
'resolveDocRoot' => true,
'checkAllowDirs' => true,
'allowDirs' => array($env->getDocRoot()),
), $options);
if ($this->options['fileChecker'] && !is_callable($this->options['fileChecker'])) {
throw new InvalidArgumentException("fileChecker option is not callable");
}
}
/**
* @param string $basenamePattern A pattern tested against basename. E.g. "~\.css$~"
* @param callable $handler Function that recieves a $spec array and returns a Minify_SourceInterface
*/
public function setHandler($basenamePattern, $handler)
{
$this->handlers[$basenamePattern] = $handler;
}
/**
* @param string $file
* @return string
*
* @throws Minify_Source_FactoryException
*/
public function checkIsFile($file)
{
$realpath = realpath($file);
if (!$realpath) {
throw new Minify_Source_FactoryException("File failed realpath(): $file");
}
$basename = basename($file);
if (0 === strpos($basename, '.')) {
throw new Minify_Source_FactoryException("Filename starts with period (may be hidden): $basename");
}
if (!is_file($realpath) || !is_readable($realpath)) {
throw new Minify_Source_FactoryException("Not a file or isn't readable: $file");
}
return $realpath;
}
/**
* @param mixed $spec
*
* @return Minify_SourceInterface
*
* @throws Minify_Source_FactoryException
*/
public function makeSource($spec)
{
$source = null;
if ($spec instanceof Minify_SourceInterface) {
$source = $spec;
}
if (empty($spec['filepath'])) {
// not much we can check
return new Minify_Source($spec);
}
if ($this->options['resolveDocRoot']) {
if (0 === strpos($spec['filepath'], '//')) {
$spec['filepath'] = $this->env->getDocRoot() . substr($spec['filepath'], 1);
}
}
if (!empty($this->options['fileChecker'])) {
$spec['filepath'] = call_user_func($this->options['fileChecker'], $spec['filepath']);
}
if ($this->options['checkAllowDirs']) {
foreach ((array)$this->options['allowDirs'] as $allowDir) {
if (strpos($spec['filepath'], $allowDir) !== 0) {
throw new Minify_Source_FactoryException("File '{$spec['filepath']}' is outside \$allowDirs."
. " If the path is resolved via an alias/symlink, look into the \$min_symlinks option.");
}
}
}
$basename = basename($spec['filepath']);
if ($this->options['noMinPattern'] && preg_match($this->options['noMinPattern'], $basename)) {
if (preg_match('~\.css$~i', $basename)) {
$spec['minifyOptions']['compress'] = false;
// we still want URI rewriting to work for CSS
} else {
$spec['minifier'] = '';
}
}
$hoursBehind = $this->options['uploaderHoursBehind'];
if ($hoursBehind != 0) {
$spec['uploaderHoursBehind'] = $hoursBehind;
}
foreach ($this->handlers as $basenamePattern => $handler) {
if (preg_match($basenamePattern, $basename)) {
$source = $handler($spec);
break;
}
}
if (!$source) {
if (in_array(pathinfo($spec['filepath'], PATHINFO_EXTENSION), array('css', 'js'))) {
$source = new Minify_Source($spec);
} else {
throw new Minify_Source_FactoryException("Handler not found for file: {$spec['filepath']}");
}
}
return $source;
}
}

View File

@@ -0,0 +1,3 @@
<?php
class Minify_Source_FactoryException extends Exception {}