1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-18 11:51:27 +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

@@ -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'])) {
$sourceFactoryOptions['noMinPattern'] = $min_serveOptions['minApp']['noMinPattern'];
}
$sourceFactory = new Minify_Source_Factory($env, $sourceFactoryOptions, $cache);
$minify = call_user_func($min_factories['minify'], $cache);
/* @var Minify $minify */
$min_serveController = new Minify_Controller_MinApp($env, $sourceFactory);
}
$server->serve($min_serveController, $min_serveOptions);
exit;
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);
}
// not serving
if ($min_enableBuilder) {
header('Location: builder/');
exit;
}
$sourceFactoryOptions = array();
header('Location: /');
exit;
// 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);
$controller = call_user_func($min_factories['controller'], $env, $sourceFactory);
/* @var Minify_ControllerInterface $controller */
$minify->serve($controller, $min_serveOptions);