mirror of
https://github.com/mrclay/minify.git
synced 2025-08-23 22:22:59 +02:00
Merge pull request #125 from mrclay/WIP_dynamic_objects
Add dynamic objects and LESS support!
This commit is contained in:
@@ -16,11 +16,21 @@
|
|||||||
"issues": "http://code.google.com/p/minify/issues/list",
|
"issues": "http://code.google.com/p/minify/issues/list",
|
||||||
"wiki": "http://code.google.com/p/minify/w/list"
|
"wiki": "http://code.google.com/p/minify/w/list"
|
||||||
},
|
},
|
||||||
|
"autoload": {
|
||||||
|
"classmap": ["min/lib/"]
|
||||||
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.2.1",
|
"php": ">=5.2.1",
|
||||||
"ext-pcre": "*"
|
"ext-pcre": "*"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"require-dev": {
|
||||||
"classmap": ["min/lib/"]
|
"leafo/lessphp": "~0.4.0",
|
||||||
|
"meenie/javascript-packer": "~1.1",
|
||||||
|
"tubalmartin/cssmin": "~2.4.8"
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"leafo/lessphp": "LESS support",
|
||||||
|
"meenie/javascript-packer": "Keep track of the Packer PHP port using Composer",
|
||||||
|
"tubalmartin/cssmin": "Support minify with CSSMin (YUI PHP port)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -220,22 +220,34 @@ by Minify. E.g. <code>@import "<span class=minRoot>/min/?</span>g=css2";</code><
|
|||||||
<?php
|
<?php
|
||||||
$content = ob_get_clean();
|
$content = ob_get_clean();
|
||||||
|
|
||||||
// setup Minify
|
if (!isset($min_cachePath)) {
|
||||||
Minify::setCache(
|
$min_cachePath = '';
|
||||||
isset($min_cachePath) ? $min_cachePath : ''
|
}
|
||||||
,$min_cacheFileLocking
|
if (is_string($min_cachePath)) {
|
||||||
);
|
$cache = new Minify_Cache_File($min_cachePath, $min_cacheFileLocking);
|
||||||
Minify::$uploaderHoursBehind = $min_uploaderHoursBehind;
|
} else {
|
||||||
|
$cache = $min_cachePath;
|
||||||
|
}
|
||||||
|
|
||||||
Minify::serve('Page', array(
|
$env = new Minify_Env();
|
||||||
'content' => $content
|
|
||||||
,'id' => __FILE__
|
$sourceFactory = new Minify_Source_Factory($env, array(
|
||||||
,'lastModifiedTime' => max(
|
'uploaderHoursBehind' => $min_uploaderHoursBehind,
|
||||||
// regenerate cache if any of these change
|
));
|
||||||
filemtime(__FILE__)
|
|
||||||
,filemtime(dirname(__FILE__) . '/../config.php')
|
$controller = new Minify_Controller_Page($env, $sourceFactory);
|
||||||
,filemtime(dirname(__FILE__) . '/../lib/Minify.php')
|
|
||||||
)
|
$server = new Minify($cache);
|
||||||
,'minifyAll' => true
|
|
||||||
,'encodeOutput' => $encodeOutput
|
$server->serve($controller, array(
|
||||||
|
'content' => $content,
|
||||||
|
'id' => __FILE__,
|
||||||
|
'lastModifiedTime' => max(
|
||||||
|
// regenerate cache if any of these change
|
||||||
|
filemtime(__FILE__),
|
||||||
|
filemtime(dirname(__FILE__) . '/../config.php'),
|
||||||
|
filemtime(dirname(__FILE__) . '/../lib/Minify.php')
|
||||||
|
),
|
||||||
|
'minifyAll' => true,
|
||||||
|
'encodeOutput' => $encodeOutput,
|
||||||
));
|
));
|
||||||
|
@@ -12,6 +12,8 @@
|
|||||||
**/
|
**/
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
// 'js' => array('//js/file1.js', '//js/file2.js'),
|
// 'testJs' => array('//minify/min/quick-test.js'),
|
||||||
// 'css' => array('//css/file1.css', '//css/file2.css'),
|
// 'testCss' => array('//minify/min/quick-test.css'),
|
||||||
|
// 'js' => array('//js/file1.js', '//js/file2.js'),
|
||||||
|
// 'css' => array('//css/file1.css', '//css/file2.css'),
|
||||||
);
|
);
|
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Front controller for default Minify implementation
|
* Sets up MinApp controller and serves files
|
||||||
*
|
*
|
||||||
* DO NOT EDIT! Configure this utility via config.php and groupsConfig.php
|
* DO NOT EDIT! Configure this utility via config.php and groupsConfig.php
|
||||||
*
|
*
|
||||||
@@ -13,7 +13,7 @@ define('MINIFY_MIN_DIR', dirname(__FILE__));
|
|||||||
$min_configPaths = array(
|
$min_configPaths = array(
|
||||||
'base' => MINIFY_MIN_DIR . '/config.php',
|
'base' => MINIFY_MIN_DIR . '/config.php',
|
||||||
'test' => MINIFY_MIN_DIR . '/config-test.php',
|
'test' => MINIFY_MIN_DIR . '/config-test.php',
|
||||||
'groups' => MINIFY_MIN_DIR . '/groupsConfig.php'
|
'groups' => MINIFY_MIN_DIR . '/groupsConfig.php',
|
||||||
);
|
);
|
||||||
|
|
||||||
// check for custom config paths
|
// check for custom config paths
|
||||||
@@ -31,17 +31,29 @@ 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;
|
// use an environment object to encapsulate all input
|
||||||
Minify::setCache(
|
$server = $_SERVER;
|
||||||
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,
|
||||||
|
));
|
||||||
|
|
||||||
|
// setup cache
|
||||||
|
if (!isset($min_cachePath)) {
|
||||||
|
$min_cachePath = '';
|
||||||
|
}
|
||||||
|
if (is_string($min_cachePath)) {
|
||||||
|
$cache = new Minify_Cache_File($min_cachePath, $min_cacheFileLocking);
|
||||||
|
} else {
|
||||||
|
$cache = $min_cachePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$server = new Minify($cache);
|
||||||
|
|
||||||
|
// TODO probably should do this elsewhere...
|
||||||
|
$min_serveOptions['minifierOptions']['text/css']['docRoot'] = $env->getDocRoot();
|
||||||
$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) {
|
||||||
@@ -49,38 +61,52 @@ foreach ($min_symlinks as $uri => $target) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($min_allowDebugFlag) {
|
if ($min_allowDebugFlag) {
|
||||||
$min_serveOptions['debug'] = Minify_DebugDetector::shouldDebugRequest($_COOKIE, $_GET, $_SERVER['REQUEST_URI']);
|
// TODO get rid of static stuff
|
||||||
|
$min_serveOptions['debug'] = Minify_DebugDetector::shouldDebugRequest($env);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($min_errorLogger) {
|
if ($min_errorLogger) {
|
||||||
if (true === $min_errorLogger) {
|
if (true === $min_errorLogger) {
|
||||||
$min_errorLogger = FirePHP::getInstance(true);
|
$min_errorLogger = FirePHP::getInstance(true);
|
||||||
}
|
}
|
||||||
|
// TODO get rid of global state
|
||||||
Minify_Logger::setLogger($min_errorLogger);
|
Minify_Logger::setLogger($min_errorLogger);
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for URI versioning
|
// check for URI versioning
|
||||||
if (preg_match('/&\\d/', $_SERVER['QUERY_STRING']) || isset($_GET['v'])) {
|
if (null !== $env->get('v') || preg_match('/&\\d/', $env->server('QUERY_STRING'))) {
|
||||||
$min_serveOptions['maxAge'] = 31536000;
|
$min_serveOptions['maxAge'] = 31536000;
|
||||||
}
|
}
|
||||||
|
|
||||||
// need groups config?
|
// need groups config?
|
||||||
if (isset($_GET['g'])) {
|
if (null !== $env->get('g')) {
|
||||||
// well need groups config
|
// well need groups config
|
||||||
$min_serveOptions['minApp']['groups'] = (require $min_configPaths['groups']);
|
$min_serveOptions['minApp']['groups'] = (require $min_configPaths['groups']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// serve or redirect
|
if ($env->get('f') || null !== $env->get('g')) {
|
||||||
if (isset($_GET['f']) || isset($_GET['g'])) {
|
// serving!
|
||||||
if (! isset($min_serveController)) {
|
if (! isset($min_serveController)) {
|
||||||
$min_serveController = new Minify_Controller_MinApp();
|
|
||||||
}
|
|
||||||
Minify::serve($min_serveController, $min_serveOptions);
|
|
||||||
|
|
||||||
} elseif ($min_enableBuilder) {
|
$sourceFactoryOptions = array();
|
||||||
header('Location: builder/');
|
|
||||||
exit;
|
// translate legacy setting to option for source factory
|
||||||
} else {
|
if (isset($min_serveOptions['minApp']['noMinPattern'])) {
|
||||||
header('Location: /');
|
$sourceFactoryOptions['noMinPattern'] = $min_serveOptions['minApp']['noMinPattern'];
|
||||||
|
}
|
||||||
|
$sourceFactory = new Minify_Source_Factory($env, $sourceFactoryOptions, $cache);
|
||||||
|
|
||||||
|
$min_serveController = new Minify_Controller_MinApp($env, $sourceFactory);
|
||||||
|
}
|
||||||
|
$server->serve($min_serveController, $min_serveOptions);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// not serving
|
||||||
|
if ($min_enableBuilder) {
|
||||||
|
header('Location: builder/');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
header('Location: /');
|
||||||
|
exit;
|
||||||
|
@@ -33,76 +33,94 @@ class Minify {
|
|||||||
const URL_DEBUG = 'http://code.google.com/p/minify/wiki/Debugging';
|
const URL_DEBUG = 'http://code.google.com/p/minify/wiki/Debugging';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* How many hours behind are the file modification times of uploaded files?
|
* Any Minify_Cache_* object or null (i.e. no server cache is used)
|
||||||
*
|
*
|
||||||
* If you upload files from Windows to a non-Windows server, Windows may report
|
* @var Minify_CacheInterface
|
||||||
* 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;
|
private $cache = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If this string is not empty AND the serve() option 'bubbleCssImports' is
|
* Active controller for current request
|
||||||
* NOT set, then serve() will check CSS files for @import declarations that
|
|
||||||
* appear too late in the combined stylesheet. If found, serve() will prepend
|
|
||||||
* the output with this warning.
|
|
||||||
*
|
*
|
||||||
* @var string $importWarning
|
* @var Minify_Controller_Base
|
||||||
*/
|
*/
|
||||||
public static $importWarning = "/* See http://code.google.com/p/minify/wiki/CommonProblems#@imports_can_appear_in_invalid_locations_in_combined_CSS_files */\n";
|
protected $controller = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Has the DOCUMENT_ROOT been set in user code?
|
* @var Minify_SourceInterface[]
|
||||||
*
|
|
||||||
* @var bool
|
|
||||||
*/
|
*/
|
||||||
public static $isDocRootSet = false;
|
protected $sources;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify a cache object (with identical interface as Minify_Cache_File) or
|
* @var string
|
||||||
* 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)
|
protected $selectionId;
|
||||||
{
|
|
||||||
if (is_string($cache)) {
|
/**
|
||||||
self::$_cache = new Minify_Cache_File($cache, $fileLocking);
|
* Options for current request
|
||||||
} else {
|
*
|
||||||
self::$_cache = $cache;
|
* @var array
|
||||||
}
|
*/
|
||||||
|
protected $options = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Minify_CacheInterface $cache
|
||||||
|
*/
|
||||||
|
public function __construct(Minify_CacheInterface $cache) {
|
||||||
|
$this->cache = $cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Minify cache, if no Cache is defined, create Minify_Cache_Null
|
* Get default Minify options.
|
||||||
*
|
*
|
||||||
* @return Minify_CacheInterface
|
* @return array options for Minify
|
||||||
*/
|
*/
|
||||||
public static function getCache()
|
public function getDefaultOptions()
|
||||||
{
|
{
|
||||||
if (!self::$_cache) {
|
return array(
|
||||||
self::$_cache = new Minify_Cache_Null();
|
'isPublic' => true,
|
||||||
}
|
'encodeOutput' => function_exists('gzdeflate'),
|
||||||
return self::$_cache;
|
'encodeMethod' => null, // determine later
|
||||||
|
'encodeLevel' => 9,
|
||||||
|
|
||||||
|
'minifiers' => array(
|
||||||
|
Minify::TYPE_JS => array('JSMin', 'minify'),
|
||||||
|
Minify::TYPE_CSS => array('Minify_CSS', 'minify'),
|
||||||
|
Minify::TYPE_HTML => array('Minify_HTML', 'minify'),
|
||||||
|
),
|
||||||
|
'minifierOptions' => array(), // no minifier options
|
||||||
|
|
||||||
|
'contentTypeCharset' => 'utf-8',
|
||||||
|
'maxAge' => 1800, // 30 minutes
|
||||||
|
'rewriteCssUris' => true,
|
||||||
|
'bubbleCssImports' => false,
|
||||||
|
'quiet' => false, // serve() will send headers and output
|
||||||
|
'debug' => false,
|
||||||
|
|
||||||
|
// if you override these, the response codes MUST be directly after
|
||||||
|
// the first space.
|
||||||
|
'badRequestHeader' => 'HTTP/1.0 400 Bad Request',
|
||||||
|
'errorHeader' => 'HTTP/1.0 500 Internal Server Error',
|
||||||
|
|
||||||
|
// callback function to see/modify content of all sources
|
||||||
|
'postprocessor' => null,
|
||||||
|
// file to require to load preprocessor
|
||||||
|
'postprocessorRequire' => null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If this string is not empty AND the serve() option 'bubbleCssImports' is
|
||||||
|
* NOT set, then serve() will check CSS files for @import declarations that
|
||||||
|
* appear too late in the combined stylesheet. If found, serve() will prepend
|
||||||
|
* the output with this warning.
|
||||||
|
*/
|
||||||
|
'importWarning' => "/* See http://code.google.com/p/minify/wiki/CommonProblems#@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 in the base controller:
|
* 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)
|
||||||
@@ -161,12 +179,15 @@ class Minify {
|
|||||||
* 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.
|
||||||
*
|
*
|
||||||
* Any controller options are documented in that controller's setupSources() method.
|
* 'importWarning' : serve() will check CSS files for @import declarations that
|
||||||
|
* 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.
|
||||||
*
|
*
|
||||||
* @param mixed $controller instance of subclass of Minify_Controller_Base or string
|
* Any controller options are documented in that controller's createConfiguration() method.
|
||||||
* name of controller. E.g. 'Files'
|
|
||||||
*
|
*
|
||||||
* @param array $options controller/serve options
|
* @param Minify_ControllerInterface $controller instance of subclass of Minify_Controller_Base
|
||||||
|
*
|
||||||
|
* @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
|
||||||
@@ -174,89 +195,82 @@ class Minify {
|
|||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function serve($controller, $options = array())
|
public function serve(Minify_ControllerInterface $controller, $options = array())
|
||||||
{
|
{
|
||||||
if (! self::$isDocRootSet && 0 === stripos(PHP_OS, 'win')) {
|
$options = array_merge($this->getDefaultOptions(), $options);
|
||||||
self::setDocRoot();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_string($controller)) {
|
$config = $controller->createConfiguration($options);
|
||||||
// make $controller into object
|
|
||||||
$class = 'Minify_Controller_' . $controller;
|
|
||||||
$controller = new $class();
|
|
||||||
/* @var Minify_Controller_Base $controller */
|
|
||||||
}
|
|
||||||
|
|
||||||
// set up controller sources and mix remaining options with
|
$this->sources = $config->getSources();
|
||||||
// controller defaults
|
$this->selectionId = $config->getSelectionId();
|
||||||
$options = $controller->setupSources($options);
|
$this->options = $this->analyzeSources($config->getOptions());
|
||||||
$options = $controller->analyzeSources($options);
|
|
||||||
self::$_options = $controller->mixInDefaultOptions($options);
|
|
||||||
|
|
||||||
// check request validity
|
// check request validity
|
||||||
if (! $controller->sources) {
|
if (!$this->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,
|
||||||
,'content' => ''
|
'content' => '',
|
||||||
,'headers' => array()
|
'headers' => array(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self::$_controller = $controller;
|
$this->controller = $controller;
|
||||||
|
|
||||||
if (self::$_options['debug']) {
|
if ($this->options['debug']) {
|
||||||
self::_setupDebug($controller->sources);
|
$this->setupDebug();
|
||||||
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) {
|
|
||||||
$cgOptions['maxAge'] = self::$_options['maxAge'];
|
if ($this->options['maxAge'] > 0) {
|
||||||
} elseif (self::$_options['debug']) {
|
$cgOptions['maxAge'] = $this->options['maxAge'];
|
||||||
|
} 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 {
|
||||||
return array(
|
return array(
|
||||||
'success' => true
|
'success' => true,
|
||||||
,'statusCode' => 304
|
'statusCode' => 304,
|
||||||
,'content' => ''
|
'content' => '',
|
||||||
,'headers' => $cg->getHeaders()
|
'headers' => $cg->getHeaders(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -265,96 +279,96 @@ class Minify {
|
|||||||
unset($cg);
|
unset($cg);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self::$_options['contentType'] === self::TYPE_CSS
|
if ($this->options['contentType'] === self::TYPE_CSS && $this->options['rewriteCssUris']) {
|
||||||
&& self::$_options['rewriteCssUris']) {
|
$this->setupUriRewrites();
|
||||||
foreach($controller->sources as $key => $source) {
|
|
||||||
$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;
|
||||||
? $cacheId . '.gz'
|
|
||||||
: $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
|
||||||
$headers['Content-Length'] = $cacheIsReady
|
if ($cacheIsReady) {
|
||||||
? $cacheContentLength
|
$headers['Content-Length'] = $cacheContentLength;
|
||||||
: ((function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2))
|
} else {
|
||||||
? mb_strlen($content, '8bit')
|
if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
|
||||||
: strlen($content)
|
$headers['Content-Length'] = mb_strlen($content, '8bit');
|
||||||
);
|
} else {
|
||||||
$headers['Content-Type'] = self::$_options['contentTypeCharset']
|
$headers['Content-Length'] = strlen($content);
|
||||||
? self::$_options['contentType'] . '; charset=' . self::$_options['contentTypeCharset']
|
}
|
||||||
: self::$_options['contentType'];
|
}
|
||||||
if (self::$_options['encodeMethod'] !== '') {
|
|
||||||
|
$headers['Content-Type'] = $this->options['contentType'];
|
||||||
|
if ($this->options['contentTypeCharset']) {
|
||||||
|
$headers['Content-Type'] .= '; charset=' . $this->options['contentTypeCharset'];
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return array(
|
return array(
|
||||||
'success' => true
|
'success' => true,
|
||||||
,'statusCode' => 200
|
'statusCode' => 200,
|
||||||
,'content' => $cacheIsReady
|
'content' => $cacheIsReady ? $this->cache->fetch($fullCacheId) : $content,
|
||||||
? self::$_cache->fetch($fullCacheId)
|
'headers' => $headers,
|
||||||
: $content
|
|
||||||
,'headers' => $headers
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -371,68 +385,34 @@ class Minify {
|
|||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function combine($sources, $options = array())
|
public function combine($sources, $options = array())
|
||||||
{
|
{
|
||||||
$cache = self::$_cache;
|
throw new BadMethodCallException(__METHOD__ . ' needs to be rewritten/replaced');
|
||||||
self::$_cache = null;
|
|
||||||
|
$cache = $this->cache;
|
||||||
|
$this->cache = new Minify_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);
|
|
||||||
self::$_cache = $cache;
|
$sourceFactory = new Minify_Source_Factory($this->env);
|
||||||
|
$controller = new Minify_Controller_Files($this->env, $sourceFactory);
|
||||||
|
$out = $this->serve($controller, $options);
|
||||||
|
|
||||||
|
$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.
|
|
||||||
*
|
|
||||||
* @param string $docRoot value to use for DOCUMENT_ROOT
|
|
||||||
*/
|
|
||||||
public static function setDocRoot($docRoot = '')
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
*
|
|
||||||
* @var Minify_CacheInterface
|
|
||||||
*/
|
|
||||||
private static $_cache = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Active controller for current request
|
|
||||||
*
|
|
||||||
* @var Minify_Controller_Base
|
|
||||||
*/
|
|
||||||
protected static $_controller = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Options for current request
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected static $_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);
|
||||||
@@ -447,17 +427,34 @@ class Minify {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up sources to use Minify_Lines
|
* Setup CSS sources for URI rewriting
|
||||||
*
|
|
||||||
* @param Minify_Source[] $sources Minify_Source instances
|
|
||||||
*/
|
*/
|
||||||
protected static function _setupDebug($sources)
|
protected function setupUriRewrites()
|
||||||
{
|
{
|
||||||
foreach ($sources as $source) {
|
foreach($this->sources as $key => $source) {
|
||||||
|
$file = $source->getFilePath();
|
||||||
|
$minifyOptions = $source->getMinifierOptions();
|
||||||
|
|
||||||
|
if ($file
|
||||||
|
&& !isset($minifyOptions['currentDir'])
|
||||||
|
&& !isset($minifyOptions['prependRelativePath'])
|
||||||
|
) {
|
||||||
|
$minifyOptions['currentDir'] = dirname($file);
|
||||||
|
$source->setMinifierOptions($minifyOptions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up sources to use Minify_Lines
|
||||||
|
*/
|
||||||
|
protected function setupDebug()
|
||||||
|
{
|
||||||
|
foreach ($this->sources as $source) {
|
||||||
$source->setMinifier(array('Minify_Lines', 'minify'));
|
$source->setMinifier(array('Minify_Lines', 'minify'));
|
||||||
$id = $source->getId();
|
$id = $source->getId();
|
||||||
$source->setMinifierOptions(array(
|
$source->setMinifierOptions(array(
|
||||||
'id' => (is_file($id) ? basename($id) : $id)
|
'id' => (is_file($id) ? basename($id) : $id),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -469,31 +466,35 @@ 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
|
||||||
$implodeSeparator = ($type === self::TYPE_JS)
|
$implodeSeparator = ($type === self::TYPE_JS) ? "\n;" : '';
|
||||||
? "\n;"
|
|
||||||
: '';
|
|
||||||
// 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])
|
if (isset($this->options['minifierOptions'][$type])) {
|
||||||
? self::$_options['minifierOptions'][$type]
|
$defaultOptions = $this->options['minifierOptions'][$type];
|
||||||
: array();
|
} else {
|
||||||
|
$defaultOptions = 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])
|
if (isset($this->options['minifiers'][$type])) {
|
||||||
? self::$_options['minifiers'][$type]
|
$defaultMinifier = $this->options['minifiers'][$type];
|
||||||
: false;
|
} else {
|
||||||
|
$defaultMinifier = 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->sources);
|
||||||
$groupToProcessTogether = array();
|
$groupToProcessTogether = array();
|
||||||
$lastMinifier = null;
|
$lastMinifier = null;
|
||||||
$lastOptions = null;
|
$lastOptions = null;
|
||||||
@@ -501,7 +502,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->sources[$i];
|
||||||
/* @var Minify_Source $source */
|
/* @var Minify_Source $source */
|
||||||
$sourceContent = $source->getContent();
|
$sourceContent = $source->getContent();
|
||||||
|
|
||||||
@@ -546,15 +547,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,18 +569,18 @@ 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->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->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
|
Minify::VERSION,
|
||||||
)));
|
)));
|
||||||
return "{$prefix}_{$name}_{$md5}";
|
return "{$prefix}_{$name}_{$md5}";
|
||||||
}
|
}
|
||||||
@@ -591,25 +592,74 @@ 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) {
|
return $css;
|
||||||
// remove comments so we don't mistake { in a comment as a block
|
}
|
||||||
$noCommentCss = preg_replace('@/\\*[\\s\\S]*?\\*/@', '', $css);
|
|
||||||
$lastImportPos = strrpos($noCommentCss, '@import');
|
if ('' === $this->options['importWarning']) {
|
||||||
$firstBlockPos = strpos($noCommentCss, '{');
|
return $css;
|
||||||
if (false !== $lastImportPos
|
}
|
||||||
&& false !== $firstBlockPos
|
|
||||||
&& $firstBlockPos < $lastImportPos
|
// remove comments so we don't mistake { in a comment as a block
|
||||||
) {
|
$noCommentCss = preg_replace('@/\\*[\\s\\S]*?\\*/@', '', $css);
|
||||||
// { appears before @import : prepend warning
|
$lastImportPos = strrpos($noCommentCss, '@import');
|
||||||
$css = self::$importWarning . $css;
|
$firstBlockPos = strpos($noCommentCss, '{');
|
||||||
}
|
if (false !== $lastImportPos
|
||||||
|
&& false !== $firstBlockPos
|
||||||
|
&& $firstBlockPos < $lastImportPos
|
||||||
|
) {
|
||||||
|
// { appears before @import : prepend warning
|
||||||
|
$css = $this->options['importWarning'] . $css;
|
||||||
}
|
}
|
||||||
return $css;
|
return $css;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Analyze sources (if there are any) and set $options 'contentType'
|
||||||
|
* and 'lastModifiedTime' if they already aren't.
|
||||||
|
*
|
||||||
|
* @param array $options options for Minify
|
||||||
|
*
|
||||||
|
* @return array options for Minify
|
||||||
|
*/
|
||||||
|
protected function analyzeSources($options = array())
|
||||||
|
{
|
||||||
|
if (!$this->sources) {
|
||||||
|
return $options;
|
||||||
|
}
|
||||||
|
|
||||||
|
$type = null;
|
||||||
|
foreach ($this->sources as $source) {
|
||||||
|
if ($type === null) {
|
||||||
|
$type = $source->getContentType();
|
||||||
|
} elseif ($source->getContentType() !== $type) {
|
||||||
|
|
||||||
|
// TODO better logging
|
||||||
|
Minify_Logger::log('ContentType mismatch');
|
||||||
|
|
||||||
|
$this->sources = array();
|
||||||
|
return $options;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (null === $type) {
|
||||||
|
$type = 'text/plain';
|
||||||
|
}
|
||||||
|
$options['contentType'] = $type;
|
||||||
|
|
||||||
|
// last modified is needed for caching, even if setExpires is set
|
||||||
|
if (!isset($options['lastModifiedTime'])) {
|
||||||
|
$max = 0;
|
||||||
|
foreach ($this->sources as $source) {
|
||||||
|
$max = max($source->getLastModified(), $max);
|
||||||
|
}
|
||||||
|
$options['lastModifiedTime'] = $max;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $options;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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 {
|
||||||
|
@@ -7,206 +7,41 @@
|
|||||||
/**
|
/**
|
||||||
* Base class for Minify controller
|
* Base class for Minify controller
|
||||||
*
|
*
|
||||||
* The controller class validates a request and uses it to create sources
|
* The controller class validates a request and uses it to create a configuration for Minify::serve().
|
||||||
* for minification and set options like contentType. It's also responsible
|
|
||||||
* for loading minifier code upon request.
|
|
||||||
*
|
*
|
||||||
* @package Minify
|
* @package Minify
|
||||||
* @author Stephen Clay <steve@mrclay.org>
|
* @author Stephen Clay <steve@mrclay.org>
|
||||||
*/
|
*/
|
||||||
abstract class Minify_Controller_Base {
|
abstract class Minify_Controller_Base implements Minify_ControllerInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup controller sources and set an needed options for Minify::source
|
* @var Minify_Env
|
||||||
*
|
*/
|
||||||
* You must override this method in your subclass controller to set
|
protected $env;
|
||||||
* $this->sources. If the request is NOT valid, make sure $this->sources
|
|
||||||
* is left an empty array. Then strip any controller-specific options from
|
/**
|
||||||
* $options and return it. To serve files, $this->sources must be an array of
|
* @var Minify_Source_Factory
|
||||||
* Minify_Source objects.
|
*/
|
||||||
|
protected $sourceFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Minify_Env $env
|
||||||
|
* @param Minify_Source_Factory $sourceFactory
|
||||||
|
*/
|
||||||
|
public function __construct(Minify_Env $env, Minify_Source_Factory $sourceFactory)
|
||||||
|
{
|
||||||
|
$this->env = $env;
|
||||||
|
$this->sourceFactory = $sourceFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create controller sources and options for Minify::serve()
|
||||||
*
|
*
|
||||||
* @param array $options controller and Minify options
|
* @param array $options controller and Minify options
|
||||||
*
|
*
|
||||||
* @return array $options Minify::serve options
|
* @return Minify_ServeConfiguration
|
||||||
*/
|
*/
|
||||||
abstract public function setupSources($options);
|
abstract public function createConfiguration(array $options);
|
||||||
|
|
||||||
/**
|
|
||||||
* Get default Minify options for this controller.
|
|
||||||
*
|
|
||||||
* Override in subclass to change defaults
|
|
||||||
*
|
|
||||||
* @return array options for Minify
|
|
||||||
*/
|
|
||||||
public function getDefaultMinifyOptions() {
|
|
||||||
return array(
|
|
||||||
'isPublic' => true
|
|
||||||
,'encodeOutput' => function_exists('gzdeflate')
|
|
||||||
,'encodeMethod' => null // determine later
|
|
||||||
,'encodeLevel' => 9
|
|
||||||
,'minifierOptions' => array() // no minifier options
|
|
||||||
,'contentTypeCharset' => 'utf-8'
|
|
||||||
,'maxAge' => 1800 // 30 minutes
|
|
||||||
,'rewriteCssUris' => true
|
|
||||||
,'bubbleCssImports' => false
|
|
||||||
,'quiet' => false // serve() will send headers and output
|
|
||||||
,'debug' => false
|
|
||||||
|
|
||||||
// if you override these, the response codes MUST be directly after
|
|
||||||
// the first space.
|
|
||||||
,'badRequestHeader' => 'HTTP/1.0 400 Bad Request'
|
|
||||||
,'errorHeader' => 'HTTP/1.0 500 Internal Server Error'
|
|
||||||
|
|
||||||
// callback function to see/modify content of all sources
|
|
||||||
,'postprocessor' => null
|
|
||||||
// file to require to load preprocessor
|
|
||||||
,'postprocessorRequire' => null
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get default minifiers for this controller.
|
|
||||||
*
|
|
||||||
* Override in subclass to change defaults
|
|
||||||
*
|
|
||||||
* @return array minifier callbacks for common types
|
|
||||||
*/
|
|
||||||
public function getDefaultMinifers() {
|
|
||||||
$ret[Minify::TYPE_JS] = array('JSMin', 'minify');
|
|
||||||
$ret[Minify::TYPE_CSS] = array('Minify_CSS', 'minify');
|
|
||||||
$ret[Minify::TYPE_HTML] = array('Minify_HTML', 'minify');
|
|
||||||
return $ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Is a user-given file within an allowable directory, existing,
|
|
||||||
* and having an extension js/css/html/txt ?
|
|
||||||
*
|
|
||||||
* This is a convenience function for controllers that have to accept
|
|
||||||
* user-given paths
|
|
||||||
*
|
|
||||||
* @param string $file full file path (already processed by realpath())
|
|
||||||
*
|
|
||||||
* @param array $safeDirs directories where files are safe to serve. Files can also
|
|
||||||
* be in subdirectories of these directories.
|
|
||||||
*
|
|
||||||
* @return bool file is safe
|
|
||||||
*
|
|
||||||
* @deprecated use checkAllowDirs, checkNotHidden instead
|
|
||||||
*/
|
|
||||||
public static function _fileIsSafe($file, $safeDirs)
|
|
||||||
{
|
|
||||||
$pathOk = false;
|
|
||||||
foreach ((array)$safeDirs as $safeDir) {
|
|
||||||
if (strpos($file, $safeDir) === 0) {
|
|
||||||
$pathOk = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$base = basename($file);
|
|
||||||
if (! $pathOk || ! is_file($file) || $base[0] === '.') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
list($revExt) = explode('.', strrev($base));
|
|
||||||
return in_array(strrev($revExt), array('js', 'css', 'html', 'txt'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $file
|
|
||||||
* @param array $allowDirs
|
|
||||||
* @param string $uri
|
|
||||||
* @return bool
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public static function checkAllowDirs($file, $allowDirs, $uri)
|
|
||||||
{
|
|
||||||
foreach ((array)$allowDirs as $allowDir) {
|
|
||||||
if (strpos($file, $allowDir) === 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new Exception("File '$file' is outside \$allowDirs. If the path is"
|
|
||||||
. " resolved via an alias/symlink, look into the \$min_symlinks option."
|
|
||||||
. " E.g. \$min_symlinks['/" . dirname($uri) . "'] = '" . dirname($file) . "';");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $file
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public static function checkNotHidden($file)
|
|
||||||
{
|
|
||||||
$b = basename($file);
|
|
||||||
if (0 === strpos($b, '.')) {
|
|
||||||
throw new Exception("Filename '$b' starts with period (may be hidden)");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* instances of Minify_Source, which provide content and any individual minification needs.
|
|
||||||
*
|
|
||||||
* @var Minify_SourceInterface[]
|
|
||||||
*/
|
|
||||||
public $sources = array();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Short name to place inside cache id
|
|
||||||
*
|
|
||||||
* The setupSources() method may choose to set this, making it easier to
|
|
||||||
* recognize a particular set of sources/settings in the cache folder. It
|
|
||||||
* will be filtered and truncated to make the final cache id <= 250 bytes.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
public $selectionId = '';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mix in default controller options with user-given options
|
|
||||||
*
|
|
||||||
* @param array $options user options
|
|
||||||
*
|
|
||||||
* @return array mixed options
|
|
||||||
*/
|
|
||||||
public final function mixInDefaultOptions($options)
|
|
||||||
{
|
|
||||||
$ret = array_merge(
|
|
||||||
$this->getDefaultMinifyOptions(), $options
|
|
||||||
);
|
|
||||||
if (! isset($options['minifiers'])) {
|
|
||||||
$options['minifiers'] = array();
|
|
||||||
}
|
|
||||||
$ret['minifiers'] = array_merge(
|
|
||||||
$this->getDefaultMinifers(), $options['minifiers']
|
|
||||||
);
|
|
||||||
return $ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Analyze sources (if there are any) and set $options 'contentType'
|
|
||||||
* and 'lastModifiedTime' if they already aren't.
|
|
||||||
*
|
|
||||||
* @param array $options options for Minify
|
|
||||||
*
|
|
||||||
* @return array options for Minify
|
|
||||||
*/
|
|
||||||
public final function analyzeSources($options = array())
|
|
||||||
{
|
|
||||||
if ($this->sources) {
|
|
||||||
if (! isset($options['contentType'])) {
|
|
||||||
$options['contentType'] = Minify_SourceSet::getContentType($this->sources);
|
|
||||||
}
|
|
||||||
// last modified is needed for caching, even if setExpires is set
|
|
||||||
if (! isset($options['lastModifiedTime'])) {
|
|
||||||
$max = 0;
|
|
||||||
/** @var Minify_Source $source */
|
|
||||||
foreach ($this->sources as $source) {
|
|
||||||
$max = max($source->getLastModified(), $max);
|
|
||||||
}
|
|
||||||
$options['lastModifiedTime'] = $max;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $options;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send message to the Minify logger
|
* Send message to the Minify logger
|
||||||
@@ -215,7 +50,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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,9 +18,6 @@
|
|||||||
* ));
|
* ));
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* As a shortcut, the controller will replace "//" at the beginning
|
|
||||||
* of a filename with $_SERVER['DOCUMENT_ROOT'] . '/'.
|
|
||||||
*
|
|
||||||
* @package Minify
|
* @package Minify
|
||||||
* @author Stephen Clay <steve@mrclay.org>
|
* @author Stephen Clay <steve@mrclay.org>
|
||||||
*/
|
*/
|
||||||
@@ -30,13 +27,13 @@ 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 array Minify options
|
* @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 setupSources($options) {
|
public function createConfiguration(array $options) {
|
||||||
// strip controller options
|
// strip controller options
|
||||||
|
|
||||||
$files = $options['files'];
|
$files = $options['files'];
|
||||||
@@ -50,27 +47,20 @@ class Minify_Controller_Files extends Minify_Controller_Base {
|
|||||||
|
|
||||||
$sources = array();
|
$sources = array();
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
if ($file instanceof Minify_Source) {
|
if ($file instanceof Minify_SourceInterface) {
|
||||||
$sources[] = $file;
|
$sources[] = $file;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (0 === strpos($file, '//')) {
|
try {
|
||||||
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
|
$sources[] = $this->sourceFactory->makeSource(array(
|
||||||
}
|
'filepath' => $file,
|
||||||
$realPath = realpath($file);
|
|
||||||
if (is_file($realPath)) {
|
|
||||||
$sources[] = new Minify_Source(array(
|
|
||||||
'filepath' => $realPath
|
|
||||||
));
|
));
|
||||||
} else {
|
} catch (Minify_Source_FactoryException $e) {
|
||||||
$this->log("The path \"{$file}\" could not be found (or was not a file)");
|
$this->log($e->getMessage());
|
||||||
return $options;
|
return new Minify_ServeConfiguration($options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($sources) {
|
return new Minify_ServeConfiguration($options, $sources);
|
||||||
$this->sources = $sources;
|
|
||||||
}
|
|
||||||
return $options;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -20,13 +20,10 @@
|
|||||||
* 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
|
||||||
*
|
*
|
||||||
* As a shortcut, the controller will replace "//" at the beginning
|
|
||||||
* of a filename with $_SERVER['DOCUMENT_ROOT'] . '/'.
|
|
||||||
*
|
|
||||||
* @package Minify
|
* @package Minify
|
||||||
* @author Stephen Clay <steve@mrclay.org>
|
* @author Stephen Clay <steve@mrclay.org>
|
||||||
*/
|
*/
|
||||||
class Minify_Controller_Groups extends Minify_Controller_Base {
|
class Minify_Controller_Groups extends Minify_Controller_Files {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up groups of files as sources
|
* Set up groups of files as sources
|
||||||
@@ -38,54 +35,37 @@ class Minify_Controller_Groups extends Minify_Controller_Base {
|
|||||||
*
|
*
|
||||||
* @return array Minify options
|
* @return array Minify options
|
||||||
*/
|
*/
|
||||||
public function setupSources($options) {
|
public function createConfiguration(array $options) {
|
||||||
// strip controller options
|
// strip controller options
|
||||||
$groups = $options['groups'];
|
$groups = $options['groups'];
|
||||||
unset($options['groups']);
|
unset($options['groups']);
|
||||||
|
|
||||||
|
$server = $this->env->server();
|
||||||
|
|
||||||
// mod_fcgid places PATH_INFO in ORIG_PATH_INFO
|
// mod_fcgid places PATH_INFO in ORIG_PATH_INFO
|
||||||
$pi = isset($_SERVER['ORIG_PATH_INFO'])
|
$pathInfo = isset($server['ORIG_PATH_INFO'])
|
||||||
? substr($_SERVER['ORIG_PATH_INFO'], 1)
|
? substr($server['ORIG_PATH_INFO'], 1)
|
||||||
: (isset($_SERVER['PATH_INFO'])
|
: (isset($server['PATH_INFO'])
|
||||||
? substr($_SERVER['PATH_INFO'], 1)
|
? substr($server['PATH_INFO'], 1)
|
||||||
: false
|
: false
|
||||||
);
|
);
|
||||||
if (false === $pi || ! isset($groups[$pi])) {
|
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 \"$pi\"");
|
$this->log("Missing PATH_INFO or no group set for \"$pathInfo\"");
|
||||||
return $options;
|
return new Minify_ServeConfiguration($options);
|
||||||
}
|
}
|
||||||
$sources = array();
|
|
||||||
|
|
||||||
$files = $groups[$pi];
|
$files = $groups[$pathInfo];
|
||||||
// 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)) {
|
||||||
$files = array($files);
|
$files = array($files);
|
||||||
} elseif (! is_array($files)) {
|
} elseif (! is_array($files)) {
|
||||||
$files = (array)$files;
|
$files = (array)$files;
|
||||||
}
|
}
|
||||||
foreach ($files as $file) {
|
|
||||||
if ($file instanceof Minify_Source) {
|
$options['files'] = $files;
|
||||||
$sources[] = $file;
|
|
||||||
continue;
|
return parent::createConfiguration($options);
|
||||||
}
|
|
||||||
if (0 === strpos($file, '//')) {
|
|
||||||
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
|
|
||||||
}
|
|
||||||
$realPath = realpath($file);
|
|
||||||
if (is_file($realPath)) {
|
|
||||||
$sources[] = new Minify_Source(array(
|
|
||||||
'filepath' => $realPath
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
$this->log("The path \"{$file}\" could not be found (or was not a file)");
|
|
||||||
return $options;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($sources) {
|
|
||||||
$this->sources = $sources;
|
|
||||||
}
|
|
||||||
return $options;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -19,43 +19,44 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
|
|||||||
*
|
*
|
||||||
* @return array Minify options
|
* @return array Minify options
|
||||||
*/
|
*/
|
||||||
public function setupSources($options) {
|
public function createConfiguration(array $options) {
|
||||||
// PHP insecure by default: realpath() and other FS functions can't handle null bytes.
|
// PHP insecure by default: realpath() and other FS functions can't handle null bytes.
|
||||||
|
$get = $this->env->get();
|
||||||
foreach (array('g', 'b', 'f') as $key) {
|
foreach (array('g', 'b', 'f') as $key) {
|
||||||
if (isset($_GET[$key])) {
|
if (isset($get[$key])) {
|
||||||
$_GET[$key] = str_replace("\x00", '', (string)$_GET[$key]);
|
$get[$key] = str_replace("\x00", '', (string)$get[$key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter controller options
|
// filter controller options
|
||||||
$cOptions = array_merge(
|
$localOptions = array_merge(
|
||||||
array(
|
array(
|
||||||
'allowDirs' => '//'
|
'groupsOnly' => false,
|
||||||
,'groupsOnly' => false
|
'groups' => array(),
|
||||||
,'groups' => array()
|
|
||||||
,'noMinPattern' => '@[-\\.]min\\.(?:js|css)$@i' // matched against basename
|
|
||||||
)
|
)
|
||||||
,(isset($options['minApp']) ? $options['minApp'] : array())
|
,(isset($options['minApp']) ? $options['minApp'] : array())
|
||||||
);
|
);
|
||||||
unset($options['minApp']);
|
unset($options['minApp']);
|
||||||
|
|
||||||
$sources = array();
|
$sources = array();
|
||||||
$this->selectionId = '';
|
$selectionId = '';
|
||||||
$firstMissingResource = null;
|
$firstMissingResource = null;
|
||||||
if (isset($_GET['g'])) {
|
|
||||||
|
if (isset($get['g'])) {
|
||||||
// add group(s)
|
// add group(s)
|
||||||
$this->selectionId .= 'g=' . $_GET['g'];
|
$selectionId .= 'g=' . $get['g'];
|
||||||
$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 $options;
|
return new Minify_ServeConfiguration($options);
|
||||||
}
|
}
|
||||||
$keys = explode(',', $_GET['g']);
|
$keys = explode(',', $get['g']);
|
||||||
foreach ($keys as $key) {
|
foreach ($keys as $key) {
|
||||||
if (! isset($cOptions['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 $options;
|
return new Minify_ServeConfiguration($options);
|
||||||
}
|
}
|
||||||
$files = $cOptions['groups'][$key];
|
$files = $localOptions['groups'][$key];
|
||||||
// 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)) {
|
||||||
$files = array($files);
|
$files = array($files);
|
||||||
@@ -63,182 +64,110 @@ class Minify_Controller_MinApp extends Minify_Controller_Base {
|
|||||||
$files = (array)$files;
|
$files = (array)$files;
|
||||||
}
|
}
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
if ($file instanceof Minify_Source) {
|
if ($file instanceof Minify_SourceInterface) {
|
||||||
$sources[] = $file;
|
$sources[] = $file;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (0 === strpos($file, '//')) {
|
try {
|
||||||
$file = $_SERVER['DOCUMENT_ROOT'] . substr($file, 1);
|
$source = $this->sourceFactory->makeSource(array(
|
||||||
}
|
'filepath' => $file,
|
||||||
$realpath = realpath($file);
|
));
|
||||||
if ($realpath && is_file($realpath)) {
|
$sources[] = $source;
|
||||||
$sources[] = $this->_getFileSource($realpath, $cOptions);
|
} catch (Minify_Source_FactoryException $e) {
|
||||||
} else {
|
$this->log($e->getMessage());
|
||||||
$this->log("The path \"{$file}\" (realpath \"{$realpath}\") could not be found (or was not a file)");
|
|
||||||
if (null === $firstMissingResource) {
|
if (null === $firstMissingResource) {
|
||||||
$firstMissingResource = basename($file);
|
$firstMissingResource = basename($file);
|
||||||
continue;
|
continue;
|
||||||
} 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 $options;
|
return new Minify_ServeConfiguration($options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($sources) {
|
|
||||||
try {
|
|
||||||
$this->checkType($sources[0]);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$this->log($e->getMessage());
|
|
||||||
return $options;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (! $cOptions['groupsOnly'] && isset($_GET['f'])) {
|
if (! $localOptions['groupsOnly'] && isset($get['f'])) {
|
||||||
// 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|js)(?:,[^,]+\\.\\1)*$/', $_GET['f'], $m)
|
! preg_match('/^[^,]+\\.(css|less|js)(?:,[^,]+\\.\\1)*$/', $get['f'], $m)
|
||||||
// no "//"
|
// no "//"
|
||||||
|| strpos($_GET['f'], '//') !== false
|
|| strpos($get['f'], '//') !== false
|
||||||
// no "\"
|
// no "\"
|
||||||
|| strpos($_GET['f'], '\\') !== false
|
|| strpos($get['f'], '\\') !== false
|
||||||
) {
|
) {
|
||||||
$this->log("GET param 'f' was invalid");
|
$this->log("GET param 'f' was invalid");
|
||||||
return $options;
|
return new Minify_ServeConfiguration($options);
|
||||||
}
|
}
|
||||||
$ext = ".{$m[1]}";
|
$ext = ".{$m[1]}";
|
||||||
try {
|
$files = explode(',', $get['f']);
|
||||||
$this->checkType($m[1]);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$this->log($e->getMessage());
|
|
||||||
return $options;
|
|
||||||
}
|
|
||||||
$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 $options;
|
return new Minify_ServeConfiguration($options);
|
||||||
}
|
}
|
||||||
if (isset($_GET['b'])) {
|
if (isset($get['b'])) {
|
||||||
// check for validity
|
// check for validity
|
||||||
if (preg_match('@^[^/]+(?:/[^/]+)*$@', $_GET['b'])
|
if (preg_match('@^[^/]+(?:/[^/]+)*$@', $get['b'])
|
||||||
&& false === strpos($_GET['b'], '..')
|
&& false === strpos($get['b'], '..')
|
||||||
&& $_GET['b'] !== '.') {
|
&& $get['b'] !== '.') {
|
||||||
// valid base
|
// valid base
|
||||||
$base = "/{$_GET['b']}/";
|
$base = "/{$get['b']}/";
|
||||||
} else {
|
} else {
|
||||||
$this->log("GET param 'b' was invalid");
|
$this->log("GET param 'b' was invalid");
|
||||||
return $options;
|
return new Minify_ServeConfiguration($options);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$base = '/';
|
$base = '/';
|
||||||
}
|
}
|
||||||
$allowDirs = array();
|
|
||||||
foreach ((array)$cOptions['allowDirs'] as $allowDir) {
|
|
||||||
$allowDirs[] = realpath(str_replace('//', $_SERVER['DOCUMENT_ROOT'] . '/', $allowDir));
|
|
||||||
}
|
|
||||||
$basenames = array(); // just for cache id
|
$basenames = array(); // just for cache id
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
$uri = $base . $file;
|
$uri = $base . $file;
|
||||||
$path = $_SERVER['DOCUMENT_ROOT'] . $uri;
|
$path = $this->env->getDocRoot() . $uri;
|
||||||
$realpath = realpath($path);
|
|
||||||
if (false === $realpath || ! is_file($realpath)) {
|
try {
|
||||||
$this->log("The path \"{$path}\" (realpath \"{$realpath}\") could not be found (or was not a file)");
|
$source = $this->sourceFactory->makeSource(array(
|
||||||
|
'filepath' => $path,
|
||||||
|
));
|
||||||
|
$sources[] = $source;
|
||||||
|
$basenames[] = basename($path, $ext);
|
||||||
|
} catch (Minify_Source_FactoryException $e) {
|
||||||
|
$this->log($e->getMessage());
|
||||||
if (null === $firstMissingResource) {
|
if (null === $firstMissingResource) {
|
||||||
$firstMissingResource = $uri;
|
$firstMissingResource = $uri;
|
||||||
continue;
|
continue;
|
||||||
} 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 $options;
|
return new Minify_ServeConfiguration($options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
parent::checkNotHidden($realpath);
|
|
||||||
parent::checkAllowDirs($realpath, $allowDirs, $uri);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$this->log($e->getMessage());
|
|
||||||
return $options;
|
|
||||||
}
|
|
||||||
$sources[] = $this->_getFileSource($realpath, $cOptions);
|
|
||||||
$basenames[] = basename($realpath, $ext);
|
|
||||||
}
|
}
|
||||||
if ($this->selectionId) {
|
if ($selectionId) {
|
||||||
$this->selectionId .= '_f=';
|
$selectionId .= '_f=';
|
||||||
}
|
}
|
||||||
$this->selectionId .= implode(',', $basenames) . $ext;
|
$selectionId .= implode(',', $basenames) . $ext;
|
||||||
}
|
}
|
||||||
if ($sources) {
|
|
||||||
if (null !== $firstMissingResource) {
|
if (!$sources) {
|
||||||
array_unshift($sources, new Minify_Source(array(
|
|
||||||
'id' => 'missingFile'
|
|
||||||
// should not cause cache invalidation
|
|
||||||
,'lastModified' => 0
|
|
||||||
// due to caching, filename is unreliable.
|
|
||||||
,'content' => "/* Minify: at least one missing file. See " . Minify::URL_DEBUG . " */\n"
|
|
||||||
,'minifier' => ''
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
$this->sources = $sources;
|
|
||||||
} else {
|
|
||||||
$this->log("No sources to serve");
|
$this->log("No sources to serve");
|
||||||
}
|
return new Minify_ServeConfiguration($options);
|
||||||
return $options;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $file
|
|
||||||
*
|
|
||||||
* @param array $cOptions
|
|
||||||
*
|
|
||||||
* @return Minify_Source
|
|
||||||
*/
|
|
||||||
protected function _getFileSource($file, $cOptions)
|
|
||||||
{
|
|
||||||
$spec['filepath'] = $file;
|
|
||||||
if ($cOptions['noMinPattern'] && preg_match($cOptions['noMinPattern'], basename($file))) {
|
|
||||||
if (preg_match('~\.css$~i', $file)) {
|
|
||||||
$spec['minifyOptions']['compress'] = false;
|
|
||||||
} else {
|
|
||||||
$spec['minifier'] = '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new Minify_Source($spec);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected $_type = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Make sure that only source files of a single type are registered
|
|
||||||
*
|
|
||||||
* @param Minify_SourceInterface|string $sourceOrExt
|
|
||||||
*
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function checkType($sourceOrExt)
|
|
||||||
{
|
|
||||||
if ($sourceOrExt instanceof Minify_SourceInterface) {
|
|
||||||
$type = $sourceOrExt->getContentType();
|
|
||||||
if (!$type) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ($sourceOrExt === 'js') {
|
|
||||||
$type = Minify::TYPE_JS;
|
|
||||||
} elseif ($sourceOrExt === 'css') {
|
|
||||||
$type = Minify::TYPE_CSS;
|
|
||||||
} else {
|
|
||||||
$type = "text/$sourceOrExt";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->_type === null) {
|
if (null !== $firstMissingResource) {
|
||||||
$this->_type = $type;
|
array_unshift($sources, new Minify_Source(array(
|
||||||
} elseif ($this->_type !== $type) {
|
'id' => 'missingFile'
|
||||||
throw new Exception('Content-Type mismatch');
|
// should not cause cache invalidation
|
||||||
|
,'lastModified' => 0
|
||||||
|
// due to caching, filename is unreliable.
|
||||||
|
,'content' => "/* Minify: at least one missing file. See " . Minify::URL_DEBUG . " */\n"
|
||||||
|
,'minifier' => ''
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return new Minify_ServeConfiguration($options, $sources, $selectionId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -30,10 +30,8 @@ class Minify_Controller_Page extends Minify_Controller_Base {
|
|||||||
*
|
*
|
||||||
* 'minifyAll': should all CSS and Javascript blocks be individually
|
* 'minifyAll': should all CSS and Javascript blocks be individually
|
||||||
* minified? (default false)
|
* minified? (default false)
|
||||||
*
|
|
||||||
* @todo Add 'file' option to read HTML file.
|
|
||||||
*/
|
*/
|
||||||
public function setupSources($options) {
|
public function createConfiguration(array $options) {
|
||||||
if (isset($options['file'])) {
|
if (isset($options['file'])) {
|
||||||
$sourceSpec = array(
|
$sourceSpec = array(
|
||||||
'filepath' => $options['file']
|
'filepath' => $options['file']
|
||||||
@@ -49,7 +47,7 @@ class Minify_Controller_Page extends Minify_Controller_Base {
|
|||||||
unset($options['content'], $options['id']);
|
unset($options['content'], $options['id']);
|
||||||
}
|
}
|
||||||
// something like "builder,index.php" or "directory,file.html"
|
// something like "builder,index.php" or "directory,file.html"
|
||||||
$this->selectionId = strtr(substr($f, 1 + strlen(dirname(dirname($f)))), '/\\', ',,');
|
$selectionId = strtr(substr($f, 1 + strlen(dirname(dirname($f)))), '/\\', ',,');
|
||||||
|
|
||||||
if (isset($options['minifyAll'])) {
|
if (isset($options['minifyAll'])) {
|
||||||
// this will be the 2nd argument passed to Minify_HTML::minify()
|
// this will be the 2nd argument passed to Minify_HTML::minify()
|
||||||
@@ -59,10 +57,11 @@ class Minify_Controller_Page extends Minify_Controller_Base {
|
|||||||
);
|
);
|
||||||
unset($options['minifyAll']);
|
unset($options['minifyAll']);
|
||||||
}
|
}
|
||||||
$this->sources[] = new Minify_Source($sourceSpec);
|
|
||||||
|
|
||||||
$options['contentType'] = Minify::TYPE_HTML;
|
$sourceSpec['contentType'] = Minify::TYPE_HTML;
|
||||||
return $options;
|
$sources[] = new Minify_Source($sourceSpec);
|
||||||
|
|
||||||
|
return new Minify_ServeConfiguration($options, $sources, $selectionId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,119 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Class Minify_Controller_Version1
|
|
||||||
* @package Minify
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Controller class for emulating version 1 of minify.php (mostly a proof-of-concept)
|
|
||||||
*
|
|
||||||
* <code>
|
|
||||||
* Minify::serve('Version1');
|
|
||||||
* </code>
|
|
||||||
*
|
|
||||||
* @package Minify
|
|
||||||
* @author Stephen Clay <steve@mrclay.org>
|
|
||||||
*/
|
|
||||||
class Minify_Controller_Version1 extends Minify_Controller_Base {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set up groups of files as sources
|
|
||||||
*
|
|
||||||
* @param array $options controller and Minify options
|
|
||||||
* @return array Minify options
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function setupSources($options) {
|
|
||||||
// PHP insecure by default: realpath() and other FS functions can't handle null bytes.
|
|
||||||
if (isset($_GET['files'])) {
|
|
||||||
$_GET['files'] = str_replace("\x00", '', (string)$_GET['files']);
|
|
||||||
}
|
|
||||||
|
|
||||||
self::_setupDefines();
|
|
||||||
if (MINIFY_USE_CACHE) {
|
|
||||||
$cacheDir = defined('MINIFY_CACHE_DIR')
|
|
||||||
? MINIFY_CACHE_DIR
|
|
||||||
: '';
|
|
||||||
Minify::setCache($cacheDir);
|
|
||||||
}
|
|
||||||
$options['badRequestHeader'] = 'HTTP/1.0 404 Not Found';
|
|
||||||
$options['contentTypeCharset'] = MINIFY_ENCODING;
|
|
||||||
|
|
||||||
// The following restrictions are to limit the URLs that minify will
|
|
||||||
// respond to. Ideally there should be only one way to reference a file.
|
|
||||||
if (! isset($_GET['files'])
|
|
||||||
// verify at least one file, files are single comma separated,
|
|
||||||
// and are all same extension
|
|
||||||
|| ! preg_match('/^[^,]+\\.(css|js)(,[^,]+\\.\\1)*$/', $_GET['files'], $m)
|
|
||||||
// no "//" (makes URL rewriting easier)
|
|
||||||
|| strpos($_GET['files'], '//') !== false
|
|
||||||
// no "\"
|
|
||||||
|| strpos($_GET['files'], '\\') !== false
|
|
||||||
// no "./"
|
|
||||||
|| preg_match('/(?:^|[^\\.])\\.\\//', $_GET['files'])
|
|
||||||
) {
|
|
||||||
return $options;
|
|
||||||
}
|
|
||||||
|
|
||||||
$files = explode(',', $_GET['files']);
|
|
||||||
if (count($files) > MINIFY_MAX_FILES) {
|
|
||||||
return $options;
|
|
||||||
}
|
|
||||||
|
|
||||||
// strings for prepending to relative/absolute paths
|
|
||||||
$prependRelPaths = dirname($_SERVER['SCRIPT_FILENAME'])
|
|
||||||
. DIRECTORY_SEPARATOR;
|
|
||||||
$prependAbsPaths = $_SERVER['DOCUMENT_ROOT'];
|
|
||||||
|
|
||||||
$goodFiles = array();
|
|
||||||
$hasBadSource = false;
|
|
||||||
|
|
||||||
$allowDirs = isset($options['allowDirs'])
|
|
||||||
? $options['allowDirs']
|
|
||||||
: MINIFY_BASE_DIR;
|
|
||||||
|
|
||||||
foreach ($files as $file) {
|
|
||||||
// prepend appropriate string for abs/rel paths
|
|
||||||
$file = ($file[0] === '/' ? $prependAbsPaths : $prependRelPaths) . $file;
|
|
||||||
// make sure a real file!
|
|
||||||
$file = realpath($file);
|
|
||||||
// don't allow unsafe or duplicate files
|
|
||||||
if (parent::_fileIsSafe($file, $allowDirs)
|
|
||||||
&& !in_array($file, $goodFiles))
|
|
||||||
{
|
|
||||||
$goodFiles[] = $file;
|
|
||||||
$srcOptions = array(
|
|
||||||
'filepath' => $file
|
|
||||||
);
|
|
||||||
$this->sources[] = new Minify_Source($srcOptions);
|
|
||||||
} else {
|
|
||||||
$hasBadSource = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($hasBadSource) {
|
|
||||||
$this->sources = array();
|
|
||||||
}
|
|
||||||
if (! MINIFY_REWRITE_CSS_URLS) {
|
|
||||||
$options['rewriteCssUris'] = false;
|
|
||||||
}
|
|
||||||
return $options;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static function _setupDefines()
|
|
||||||
{
|
|
||||||
$defaults = array(
|
|
||||||
'MINIFY_BASE_DIR' => realpath($_SERVER['DOCUMENT_ROOT'])
|
|
||||||
,'MINIFY_ENCODING' => 'utf-8'
|
|
||||||
,'MINIFY_MAX_FILES' => 16
|
|
||||||
,'MINIFY_REWRITE_CSS_URLS' => true
|
|
||||||
,'MINIFY_USE_CACHE' => true
|
|
||||||
);
|
|
||||||
foreach ($defaults as $const => $val) {
|
|
||||||
if (! defined($const)) {
|
|
||||||
define($const, $val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
13
min/lib/Minify/ControllerInterface.php
Normal file
13
min/lib/Minify/ControllerInterface.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
interface Minify_ControllerInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create controller sources and options for Minify::serve()
|
||||||
|
*
|
||||||
|
* @param array $options controller and Minify options
|
||||||
|
*
|
||||||
|
* @return Minify_ServeConfiguration
|
||||||
|
*/
|
||||||
|
public function createConfiguration(array $options);
|
||||||
|
}
|
@@ -7,16 +7,17 @@
|
|||||||
* @author Stephen Clay <steve@mrclay.org>
|
* @author Stephen Clay <steve@mrclay.org>
|
||||||
*/
|
*/
|
||||||
class Minify_DebugDetector {
|
class Minify_DebugDetector {
|
||||||
public static function shouldDebugRequest($cookie, $get, $requestUri)
|
public static function shouldDebugRequest(Minify_Env $env)
|
||||||
{
|
{
|
||||||
if (isset($get['debug'])) {
|
if ($env->get('debug')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (! empty($cookie['minifyDebug'])) {
|
$cookieValue = $env->cookie('minifyDebug');
|
||||||
foreach (preg_split('/\\s+/', $cookie['minifyDebug']) as $debugUri) {
|
if ($cookieValue) {
|
||||||
|
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, $requestUri)) {
|
if (preg_match($pattern, $env->getRequestUri())) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
90
min/lib/Minify/Env.php
Normal file
90
min/lib/Minify/Env.php
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class Minify_Env {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 = null)
|
||||||
|
{
|
||||||
|
if (null === $key) {
|
||||||
|
return $this->server;
|
||||||
|
}
|
||||||
|
return isset($this->server[$key])
|
||||||
|
? $this->server[$key]
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cookie($key = null)
|
||||||
|
{
|
||||||
|
if (null === $key) {
|
||||||
|
return $this->cookie;
|
||||||
|
}
|
||||||
|
return isset($this->cookie[$key])
|
||||||
|
? $this->cookie[$key]
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($key = null)
|
||||||
|
{
|
||||||
|
if (null === $key) {
|
||||||
|
return $this->get;
|
||||||
|
}
|
||||||
|
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 (empty($server['SERVER_SOFTWARE'])
|
||||||
|
|| 0 !== strpos($server['SERVER_SOFTWARE'], 'Microsoft-IIS/')) {
|
||||||
|
throw new InvalidArgumentException('DOCUMENT_ROOT is not provided and could not be computed');
|
||||||
|
}
|
||||||
|
$docRoot = substr(
|
||||||
|
$server['SCRIPT_FILENAME']
|
||||||
|
,0
|
||||||
|
,strlen($server['SCRIPT_FILENAME']) - strlen($server['SCRIPT_NAME'])
|
||||||
|
);
|
||||||
|
return rtrim($docRoot, '\\');
|
||||||
|
}
|
||||||
|
}
|
107
min/lib/Minify/LessCssSource.php
Normal file
107
min/lib/Minify/LessCssSource.php
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class Minify_LessCssSource extends Minify_Source {
|
||||||
|
/**
|
||||||
|
* @var Minify_CacheInterface
|
||||||
|
*/
|
||||||
|
private $cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parsed lessphp cache object
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $parsed;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
public function __construct(array $spec, Minify_CacheInterface $cache) {
|
||||||
|
parent::__construct($spec);
|
||||||
|
|
||||||
|
$this->cache = $cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get last modified of all parsed files
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getLastModified() {
|
||||||
|
$cache = $this->getCache();
|
||||||
|
|
||||||
|
$lastModified = 0;
|
||||||
|
foreach ($cache['files'] as $mtime) {
|
||||||
|
$lastModified = max($lastModified, $mtime);
|
||||||
|
|
||||||
|
}
|
||||||
|
return $lastModified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get content
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getContent() {
|
||||||
|
$cache = $this->getCache();
|
||||||
|
|
||||||
|
return $cache['compiled'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get lessphp cache object
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getCache() {
|
||||||
|
// cache for single run
|
||||||
|
// so that getLastModified and getContent in single request do not add additional cache roundtrips (i.e memcache)
|
||||||
|
if (isset($this->parsed)) {
|
||||||
|
return $this->parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check from cache first
|
||||||
|
$cache = null;
|
||||||
|
$cacheId = $this->getCacheId();
|
||||||
|
if ($this->cache->isValid($cacheId, 0)) {
|
||||||
|
if ($cache = $this->cache->fetch($cacheId)) {
|
||||||
|
$cache = unserialize($cache);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$less = $this->getCompiler();
|
||||||
|
$input = $cache ? $cache : $this->filepath;
|
||||||
|
$cache = $less->cachedCompile($input);
|
||||||
|
|
||||||
|
if (!is_array($input) || $cache['updated'] > $input['updated']) {
|
||||||
|
$this->cache->store($cacheId, serialize($cache));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->parsed = $cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make a unique cache id for for this source.
|
||||||
|
*
|
||||||
|
* @param string $prefix
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function getCacheId($prefix = 'minify') {
|
||||||
|
$md5 = md5($this->filepath);
|
||||||
|
return "{$prefix}_less_{$md5}";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get instance of less compiler
|
||||||
|
*
|
||||||
|
* @return lessc
|
||||||
|
*/
|
||||||
|
private function getCompiler() {
|
||||||
|
$less = new lessc();
|
||||||
|
// do not spend CPU time letting less doing minify
|
||||||
|
$less->setPreserveComments(true);
|
||||||
|
return $less;
|
||||||
|
}
|
||||||
|
}
|
70
min/lib/Minify/ServeConfiguration.php
Normal file
70
min/lib/Minify/ServeConfiguration.php
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Class Minify_ServeConfiguration
|
||||||
|
* @package Minify
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A configuration for Minify::serve() determined by a controller
|
||||||
|
*
|
||||||
|
* @package Minify
|
||||||
|
*/
|
||||||
|
class Minify_ServeConfiguration {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Minify_SourceInterface[]
|
||||||
|
*/
|
||||||
|
protected $sources;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $options;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $selectionId = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $options
|
||||||
|
* @param Minify_SourceInterface[] $sources
|
||||||
|
* @param string $selectionId
|
||||||
|
*/
|
||||||
|
function __construct(array $options, array $sources = array(), $selectionId = '')
|
||||||
|
{
|
||||||
|
$this->options = $options;
|
||||||
|
$this->sources = $sources;
|
||||||
|
$this->selectionId = $selectionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getOptions()
|
||||||
|
{
|
||||||
|
return $this->options;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Minify_SourceInterface[]
|
||||||
|
*/
|
||||||
|
public function getSources()
|
||||||
|
{
|
||||||
|
return $this->sources;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Short name to place inside cache id
|
||||||
|
*
|
||||||
|
* The setupSources() method may choose to set this, making it easier to
|
||||||
|
* recognize a particular set of sources/settings in the cache folder. It
|
||||||
|
* will be filtered and truncated to make the final cache id <= 250 bytes.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getSelectionId()
|
||||||
|
{
|
||||||
|
return $this->selectionId;
|
||||||
|
}
|
||||||
|
}
|
@@ -15,142 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
class Minify_Source implements Minify_SourceInterface {
|
class Minify_Source implements Minify_SourceInterface {
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getLastModified() {
|
|
||||||
return $this->lastModified;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getMinifier() {
|
|
||||||
return $this->minifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function setMinifier($minifier) {
|
|
||||||
$this->minifier = $minifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getMinifierOptions() {
|
|
||||||
return $this->minifyOptions;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function setMinifierOptions(array $options) {
|
|
||||||
$this->minifyOptions = $options;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getContentType() {
|
|
||||||
return $this->contentType;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a Minify_Source
|
|
||||||
*
|
|
||||||
* In the $spec array(), you can either provide a 'filepath' to an existing
|
|
||||||
* file (existence will not be checked!) or give 'id' (unique string for
|
|
||||||
* the content), 'content' (the string content) and 'lastModified'
|
|
||||||
* (unixtime of last update).
|
|
||||||
*
|
|
||||||
* As a shortcut, the controller will replace "//" at the beginning
|
|
||||||
* of a filepath with $_SERVER['DOCUMENT_ROOT'] . '/'.
|
|
||||||
*
|
|
||||||
* @param array $spec options
|
|
||||||
*/
|
|
||||||
public function __construct($spec)
|
|
||||||
{
|
|
||||||
if (isset($spec['filepath'])) {
|
|
||||||
if (0 === strpos($spec['filepath'], '//')) {
|
|
||||||
$spec['filepath'] = $_SERVER['DOCUMENT_ROOT'] . substr($spec['filepath'], 1);
|
|
||||||
}
|
|
||||||
$segments = explode('.', $spec['filepath']);
|
|
||||||
$ext = strtolower(array_pop($segments));
|
|
||||||
switch ($ext) {
|
|
||||||
case 'js' : $this->contentType = 'application/x-javascript';
|
|
||||||
break;
|
|
||||||
case 'css' : $this->contentType = 'text/css';
|
|
||||||
break;
|
|
||||||
case 'htm' : // fallthrough
|
|
||||||
case 'html' : $this->contentType = 'text/html';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$this->filepath = $spec['filepath'];
|
|
||||||
$this->_id = $spec['filepath'];
|
|
||||||
$this->lastModified = filemtime($spec['filepath'])
|
|
||||||
// offset for Windows uploaders with out of sync clocks
|
|
||||||
+ round(Minify::$uploaderHoursBehind * 3600);
|
|
||||||
} elseif (isset($spec['id'])) {
|
|
||||||
$this->_id = 'id::' . $spec['id'];
|
|
||||||
if (isset($spec['content'])) {
|
|
||||||
$this->_content = $spec['content'];
|
|
||||||
} else {
|
|
||||||
$this->_getContentFunc = $spec['getContentFunc'];
|
|
||||||
}
|
|
||||||
$this->lastModified = isset($spec['lastModified'])
|
|
||||||
? $spec['lastModified']
|
|
||||||
: time();
|
|
||||||
}
|
|
||||||
if (isset($spec['contentType'])) {
|
|
||||||
$this->contentType = $spec['contentType'];
|
|
||||||
}
|
|
||||||
if (isset($spec['minifier'])) {
|
|
||||||
$this->minifier = $spec['minifier'];
|
|
||||||
}
|
|
||||||
if (isset($spec['minifyOptions'])) {
|
|
||||||
$this->minifyOptions = $spec['minifyOptions'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getContent()
|
|
||||||
{
|
|
||||||
$content = (null !== $this->filepath)
|
|
||||||
? file_get_contents($this->filepath)
|
|
||||||
: ((null !== $this->_content)
|
|
||||||
? $this->_content
|
|
||||||
: call_user_func($this->_getContentFunc, $this->_id)
|
|
||||||
);
|
|
||||||
// remove UTF-8 BOM if present
|
|
||||||
return ("\xEF\xBB\xBF" === substr($content, 0, 3))
|
|
||||||
? substr($content, 3)
|
|
||||||
: $content;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function getId()
|
|
||||||
{
|
|
||||||
return $this->_id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function setupUriRewrites() {
|
|
||||||
if ($this->filepath
|
|
||||||
&& !isset($this->minifyOptions['currentDir'])
|
|
||||||
&& !isset($this->minifyOptions['prependRelativePath'])
|
|
||||||
) {
|
|
||||||
$this->minifyOptions['currentDir'] = dirname($this->filepath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int time of last modification
|
* @var int time of last modification
|
||||||
*/
|
*/
|
||||||
@@ -179,16 +43,165 @@ class Minify_Source implements Minify_SourceInterface {
|
|||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $_content = null;
|
protected $content = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var callable
|
* @var callable
|
||||||
*/
|
*/
|
||||||
protected $_getContentFunc = null;
|
protected $getContentFunc = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $_id = null;
|
protected $id = null;
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a Minify_Source
|
||||||
|
*
|
||||||
|
* In the $spec array(), you can either provide a 'filepath' to an existing
|
||||||
|
* file (existence will not be checked!) or give 'id' (unique string for
|
||||||
|
* the content), 'content' (the string content) and 'lastModified'
|
||||||
|
* (unixtime of last update).
|
||||||
|
*
|
||||||
|
* @param array $spec options
|
||||||
|
*/
|
||||||
|
public function __construct($spec)
|
||||||
|
{
|
||||||
|
if (isset($spec['filepath'])) {
|
||||||
|
$ext = pathinfo($spec['filepath'], PATHINFO_EXTENSION);
|
||||||
|
switch ($ext) {
|
||||||
|
case 'js' : $this->contentType = Minify::TYPE_JS;
|
||||||
|
break;
|
||||||
|
case 'less' : // fallthrough
|
||||||
|
case 'css' : $this->contentType = Minify::TYPE_CSS;
|
||||||
|
break;
|
||||||
|
case 'htm' : // fallthrough
|
||||||
|
case 'html' : $this->contentType = Minify::TYPE_HTML;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$this->filepath = $spec['filepath'];
|
||||||
|
$this->id = $spec['filepath'];
|
||||||
|
|
||||||
|
// TODO ideally not touch disk in constructor
|
||||||
|
$this->lastModified = filemtime($spec['filepath']);
|
||||||
|
|
||||||
|
if (!empty($spec['uploaderHoursBehind'])) {
|
||||||
|
// offset for Windows uploaders with out of sync clocks
|
||||||
|
$this->lastModified += round($spec['uploaderHoursBehind'] * 3600);
|
||||||
|
}
|
||||||
|
} elseif (isset($spec['id'])) {
|
||||||
|
$this->id = 'id::' . $spec['id'];
|
||||||
|
if (isset($spec['content'])) {
|
||||||
|
$this->content = $spec['content'];
|
||||||
|
} else {
|
||||||
|
$this->getContentFunc = $spec['getContentFunc'];
|
||||||
|
}
|
||||||
|
$this->lastModified = isset($spec['lastModified'])
|
||||||
|
? $spec['lastModified']
|
||||||
|
: time();
|
||||||
|
}
|
||||||
|
if (isset($spec['contentType'])) {
|
||||||
|
$this->contentType = $spec['contentType'];
|
||||||
|
}
|
||||||
|
if (isset($spec['minifier'])) {
|
||||||
|
$this->minifier = $spec['minifier'];
|
||||||
|
}
|
||||||
|
if (isset($spec['minifyOptions'])) {
|
||||||
|
$this->minifyOptions = $spec['minifyOptions'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getLastModified()
|
||||||
|
{
|
||||||
|
return $this->lastModified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getMinifier()
|
||||||
|
{
|
||||||
|
return $this->minifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function setMinifier($minifier)
|
||||||
|
{
|
||||||
|
$this->minifier = $minifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getMinifierOptions()
|
||||||
|
{
|
||||||
|
return $this->minifyOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function setMinifierOptions(array $options)
|
||||||
|
{
|
||||||
|
$this->minifyOptions = $options;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getContentType()
|
||||||
|
{
|
||||||
|
return $this->contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getContent()
|
||||||
|
{
|
||||||
|
if (null === $this->filepath) {
|
||||||
|
if (null === $this->content) {
|
||||||
|
$content = call_user_func($this->getContentFunc, $this->id);
|
||||||
|
} else {
|
||||||
|
$content = $this->content;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$content = file_get_contents($this->filepath);
|
||||||
|
}
|
||||||
|
// remove UTF-8 BOM if present
|
||||||
|
return ("\xEF\xBB\xBF" === substr($content, 0, 3)) ? substr($content, 3) : $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getFilePath()
|
||||||
|
{
|
||||||
|
return $this->filepath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function setupUriRewrites() {
|
||||||
|
if ($this->filepath
|
||||||
|
&& !isset($this->minifyOptions['currentDir'])
|
||||||
|
&& !isset($this->minifyOptions['prependRelativePath'])
|
||||||
|
) {
|
||||||
|
$this->minifyOptions['currentDir'] = dirname($this->filepath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
175
min/lib/Minify/Source/Factory.php
Normal file
175
min/lib/Minify/Source/Factory.php
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class Minify_Source_Factory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $options;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var callable[]
|
||||||
|
*/
|
||||||
|
protected $handlers = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Minify_Env
|
||||||
|
*/
|
||||||
|
protected $env;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Minify_Env $env
|
||||||
|
* @param array $options
|
||||||
|
*
|
||||||
|
* noMinPattern : Pattern matched against basename of the filepath (if present). If the pattern
|
||||||
|
* matches, Minify will try to avoid re-compressing the resource.
|
||||||
|
*
|
||||||
|
* fileChecker : Callable responsible for verifying the existence of the file.
|
||||||
|
*
|
||||||
|
* resolveDocRoot : If true, a leading "//" will be replaced with the document root.
|
||||||
|
*
|
||||||
|
* checkAllowDirs : If true, the filepath will be verified to be within one of the directories
|
||||||
|
* specified by allowDirs.
|
||||||
|
*
|
||||||
|
* allowDirs : Directory paths in which sources can be served.
|
||||||
|
*
|
||||||
|
* uploaderHoursBehind : 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.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function __construct(Minify_Env $env, array $options = array(), Minify_CacheInterface $cache = null)
|
||||||
|
{
|
||||||
|
$this->env = $env;
|
||||||
|
$this->options = array_merge(array(
|
||||||
|
'noMinPattern' => '@[-\\.]min\\.(?:[a-zA-Z]+)$@i', // matched against basename
|
||||||
|
'fileChecker' => array($this, 'checkIsFile'),
|
||||||
|
'resolveDocRoot' => true,
|
||||||
|
'checkAllowDirs' => true,
|
||||||
|
'allowDirs' => array('//'),
|
||||||
|
'uploaderHoursBehind' => 0,
|
||||||
|
), $options);
|
||||||
|
|
||||||
|
// resolve // in allowDirs
|
||||||
|
$docRoot = $env->getDocRoot();
|
||||||
|
foreach ($this->options['allowDirs'] as $i => $dir) {
|
||||||
|
$this->options['allowDirs'][$i] = $docRoot . substr($dir, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->options['fileChecker'] && !is_callable($this->options['fileChecker'])) {
|
||||||
|
throw new InvalidArgumentException("fileChecker option is not callable");
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->setHandler('~\.less$~i', function ($spec) use ($cache) {
|
||||||
|
return new Minify_LessCssSource($spec, $cache);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->setHandler('~\.(js|css)$~i', function ($spec) {
|
||||||
|
return new Minify_Source($spec);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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'] && 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|less)$~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 = call_user_func($handler, $spec);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$source) {
|
||||||
|
throw new Minify_Source_FactoryException("Handler not found for file: $basename");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $source;
|
||||||
|
}
|
||||||
|
}
|
3
min/lib/Minify/Source/FactoryException.php
Normal file
3
min/lib/Minify/Source/FactoryException.php
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class Minify_Source_FactoryException extends Exception {}
|
@@ -73,9 +73,9 @@ interface Minify_SourceInterface {
|
|||||||
public function getId();
|
public function getId();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup the current source for URI rewrites
|
* Get the path of the file that this source is based on (may be null)
|
||||||
*
|
*
|
||||||
* @return void
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function setupUriRewrites();
|
public function getFilePath();
|
||||||
}
|
}
|
||||||
|
@@ -26,24 +26,4 @@ class Minify_SourceSet {
|
|||||||
}
|
}
|
||||||
return md5(serialize($info));
|
return md5(serialize($info));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get content type from a group of sources
|
|
||||||
*
|
|
||||||
* This is called if the user doesn't pass in a 'contentType' options
|
|
||||||
*
|
|
||||||
* @param Minify_SourceInterface[] $sources Minify_Source instances
|
|
||||||
*
|
|
||||||
* @return string content type. e.g. 'text/css'
|
|
||||||
*/
|
|
||||||
public static function getContentType($sources)
|
|
||||||
{
|
|
||||||
foreach ($sources as $source) {
|
|
||||||
$contentType = $source->getContentType();
|
|
||||||
if ($contentType) {
|
|
||||||
return $contentType;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 'text/plain';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
12
min/quick-test.less
Normal file
12
min/quick-test.less
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
/*! This file exists only for testing a Minify installation. It's content is not used.
|
||||||
|
*
|
||||||
|
* http://example.org/min/f=min/quick-test.less
|
||||||
|
*/
|
||||||
|
|
||||||
|
// LESS import statement shares syntax with the CSS import statement.
|
||||||
|
// If the file being imported ends in a .less extension, or no extension, then it is treated as a LESS
|
||||||
|
// import. Otherwise it is left alone and outputted directly.
|
||||||
|
// http://leafo.net/lessphp/docs/#import
|
||||||
|
@import "quick-test.css";
|
||||||
|
|
||||||
|
@import "quick-testinc.less";
|
20
min/quick-testinc.less
Normal file
20
min/quick-testinc.less
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
@base: 24px;
|
||||||
|
@border-color: #B2B;
|
||||||
|
|
||||||
|
.underline { border-bottom: 1px solid green }
|
||||||
|
|
||||||
|
#header {
|
||||||
|
color: black;
|
||||||
|
border: 1px solid @border-color + #222222;
|
||||||
|
|
||||||
|
.navigation {
|
||||||
|
font-size: @base / 2;
|
||||||
|
a {
|
||||||
|
.underline;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.logo {
|
||||||
|
width: 300px;
|
||||||
|
:hover { text-decoration: none }
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user