From b7b26e3a832a0f15c5861b71376c026bee7c5d97 Mon Sep 17 00:00:00 2001 From: Steve Clay Date: Tue, 29 Sep 2015 14:35:43 -0400 Subject: [PATCH] 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, :( --- config.php | 10 +++++++ index.php | 80 +++++++++++++++++++++++++++++--------------------- lib/Minify.php | 11 +++++-- 3 files changed, 65 insertions(+), 36 deletions(-) diff --git a/config.php b/config.php index 7667128..d731c6f 100644 --- a/config.php +++ b/config.php @@ -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'); diff --git a/index.php b/index.php index c3628ae..e001a7e 100644 --- a/index.php +++ b/index.php @@ -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); diff --git a/lib/Minify.php b/lib/Minify.php index 345c6a2..4c4db8f 100644 --- a/lib/Minify.php +++ b/lib/Minify.php @@ -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);