1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-13 17:44:00 +02:00

A couple improvements

Show 400 if request is missing spec (instead of redirect)
Allow config.php to change factories for Minify and MinApp objects
These should've been separate commits, :(
This commit is contained in:
Steve Clay
2015-09-29 14:35:43 -04:00
parent 1aaf8f014f
commit b7b26e3a83
3 changed files with 65 additions and 36 deletions

View File

@@ -187,5 +187,15 @@ $min_symlinks = array();
$min_uploaderHoursBehind = 0;
/**
* Advanced: you can replace some of the PHP classes Minify uses to serve requests.
* To do this, assign a callable to one of the elements of the $min_factories array.
*
* You can see the default implementations (and what gets passed in) in index.php.
*/
//$min_factories['minify'] = ... a callable
//$min_factories['controller'] = ... a callable
// try to disable output_compression (may not have an effect)
ini_set('zlib.output_compression', '0');

View File

@@ -28,6 +28,20 @@ if (isset($_GET['test'])) {
include $min_configPaths['test'];
}
// setup factories
$defaultFactories = array(
'minify' => function (Minify_CacheInterface $cache) {
return new Minify($cache);
},
'controller' => function (Minify_Env $env, Minify_Source_Factory $sourceFactory) {
return new Minify_Controller_MinApp($env, $sourceFactory);
},
);
if (!isset($min_factories)) {
$min_factories = array();
}
$min_factories = array_merge($defaultFactories, $min_factories);
// use an environment object to encapsulate all input
$server = $_SERVER;
if ($min_documentRoot) {
@@ -37,18 +51,6 @@ $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;
@@ -81,32 +83,44 @@ if (null !== $env->get('v') || preg_match('/&\\d/', $env->server('QUERY_STRING')
// need groups config?
if (null !== $env->get('g')) {
// well need groups config
// we need groups config
$min_serveOptions['minApp']['groups'] = (require $min_configPaths['groups']);
}
if ($env->get('f') || null !== $env->get('g')) {
// serving!
if (! isset($min_serveController)) {
$sourceFactoryOptions = array();
// cache defaults
if (!isset($min_cachePath)) {
$min_cachePath = '';
}
if (!isset($min_cacheFileLocking)) {
$min_cacheFileLocking = true;
}
if (is_string($min_cachePath)) {
$cache = new Minify_Cache_File($min_cachePath, $min_cacheFileLocking);
} else {
// assume it meets interface.
$cache = $min_cachePath;
}
/* @var Minify_CacheInterface $cache */
// translate legacy setting to option for source factory
if (isset($min_serveOptions['minApp']['noMinPattern'])) {
$minify = call_user_func($min_factories['minify'], $cache);
/* @var Minify $minify */
if (!$env->get('f') && $env->get('g') === null) {
// no spec given
$defaults = $minify->getDefaultOptions();
$url = 'https://github.com/mrclay/minify/blob/master/docs/UserGuide.wiki.md#creating-minify-urls';
$minify->errorExit($defaults['badRequestHeader'], $url);
}
$sourceFactoryOptions = array();
// translate legacy setting to option for source factory
if (isset($min_serveOptions['minApp']['noMinPattern'])) {
$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;
}
$sourceFactory = new Minify_Source_Factory($env, $sourceFactoryOptions, $cache);
// not serving
if ($min_enableBuilder) {
header('Location: builder/');
exit;
}
$controller = call_user_func($min_factories['controller'], $env, $sourceFactory);
/* @var Minify_ControllerInterface $controller */
header('Location: /');
exit;
$minify->serve($controller, $min_serveOptions);

View File

@@ -426,11 +426,16 @@ class Minify {
}
/**
* @param string $header
* Show an error page
*
* @param string $url
* @param string $header E.g. 'HTTP/1.0 500 Internal Server Error'
* @param string $url URL to direct the user to
*
* @return void
* @internal This is not part of the public API and is subject to change
* @access private
*/
protected function errorExit($header, $url)
public function errorExit($header, $url)
{
$url = htmlspecialchars($url, ENT_QUOTES);
list(,$h1) = explode(' ', $header, 2);