From 2471eb1a360ff2c143648bdd0b55760c377c4516 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Fri, 24 May 2013 19:41:34 +0200 Subject: [PATCH] Cleanup minification controller --- composer.json | 4 - lib/Alchemy/Phrasea/Application.php | 110 ++++++++++- lib/conf.d/config.php | 181 ------------------ ...roupsConfig.php => minifyGroupsConfig.php} | 18 +- 4 files changed, 117 insertions(+), 196 deletions(-) delete mode 100644 lib/conf.d/config.php rename lib/conf.d/{groupsConfig.php => minifyGroupsConfig.php} (89%) diff --git a/composer.json b/composer.json index f0b574bd78..c5dfc45dba 100644 --- a/composer.json +++ b/composer.json @@ -5,10 +5,6 @@ "config": { "bin-dir" : "bin/" }, - "scripts" : { - "post-install-cmd" : "Alchemy\\Phrasea\\ComposerScripts::postInstall", - "post-update-cmd" : "Alchemy\\Phrasea\\ComposerScripts::postUpdate" - }, "require": { "php" : ">=5.3.3", "alchemy/oauth2php" : "dev-master", diff --git a/lib/Alchemy/Phrasea/Application.php b/lib/Alchemy/Phrasea/Application.php index c10c50f9f0..aeea844813 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -121,8 +121,10 @@ use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Generator\UrlGenerator; +use Symfony\Component\HttpKernel\Exception\HttpException; class Application extends SilexApplication { @@ -608,8 +610,112 @@ class Application extends SilexApplication $this->mount('/datafiles/', new Datafiles()); $this->mount('/permalink/', new Permalink()); - $this->get('/include/minify/', function () { - require __DIR__ . '/../../../vendor/mrclay/minify/min/index.php'; + $this->get('/include/minify/', function (Application $app, Request $request) { + + // cache directory path + $min_cachePath = __DIR__ . '/../../../tmp/cache_minify'; + + /** + * Cache file locking. Set to false if filesystem is NFS. On at least one + * NFS system flock-ing attempts stalled PHP for 30 seconds! + */ + $min_cacheFileLocking = true; + + /** + * Combining multiple CSS files can place @import declarations after rules, which + * is invalid. Minify will attempt to detect when this happens and place a + * warning comment at the top of the CSS output. To resolve this you can either + * move the @imports within your CSS files, or enable this option, which will + * move all @imports to the top of the output. Note that moving @imports could + * affect CSS values (which is why this option is disabled by default). + */ + $min_serveOptions['bubbleCssImports'] = false; + + /** + * Set to true to disable the "f" GET parameter for specifying files. + * Only the "g" parameter will be considered. + */ + $min_serveOptions['minApp']['groupsOnly'] = false; + + /** + * Maximum # of files that can be specified in the "f" GET parameter + */ + $min_serveOptions['minApp']['maxFiles'] = 10; + + /** + * If you minify CSS files stored in symlink-ed directories, the URI rewriting + * algorithm can fail. To prevent this, provide an array of link paths to + * target paths, where the link paths are within the document root. + * + * Because paths need to be normalized for this to work, use "//" to substitute + * the doc root in the link paths (the array keys). E.g.: + * + * array('//symlink' => '/real/target/path') // unix + * array('//static' => 'D:\\staticStorage') // Windows + * + */ + $min_symlinks = array(); + + /** + * If you upload files from Windows to a non-Windows server, Windows may report + * incorrect mtimes for the files. This may cause Minify to keep serving stale + * cache files when source file changes are made too frequently (e.g. more than + * once an hour). + * + * 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. + * + * In the Windows SFTP client WinSCP, there's an option that may fix this + * issue without changing the variable below. Under login > environment, + * select the option "Adjust remote timestamp with DST". + * @link http://winscp.net/eng/docs/ui_login_environment#daylight_saving_time + */ + $min_uploaderHoursBehind = 0; + + // return an array instead of echoing output + $min_serveOptions['quiet'] = true; + + \Minify::$uploaderHoursBehind = $min_uploaderHoursBehind; + \Minify::setCache( + isset($min_cachePath) ? $min_cachePath : '' + ,$min_cacheFileLocking + ); + + // require to work well :( + $_SERVER['DOCUMENT_ROOT'] = __DIR__ . '/../../../www/'; + \Minify::$isDocRootSet = true; + + $min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks; + // auto-add targets to allowDirs + foreach ($min_symlinks as $uri => $target) { + $min_serveOptions['minApp']['allowDirs'][] = $target; + } + + if (isset($_GET['g'])) { + // well need groups config + $min_serveOptions['minApp']['groups'] = require __DIR__ . '/../../conf.d/minifyGroupsConfig.php'; + } + + if (null === $request->query->get('f') && null === $request->query->get('g')) { + throw new HttpException(400, 'Please provide an argument'); + } + + $ret = \Minify::serve(new \Minify_Controller_MinApp(), $min_serveOptions); + + if (!$ret['success']) { + throw new HttpException(500, 'Unable to generate data'); + } + + $response = new Response($ret['content'], $ret['statusCode']); + $response->setMaxAge(1800); + + foreach ($ret['headers'] as $key => $value) { + $response->headers->set($key, $value); + } + + return $response; }); $this->mount('/admin/', new Root()); diff --git a/lib/conf.d/config.php b/lib/conf.d/config.php deleted file mode 100644 index fa459322c2..0000000000 --- a/lib/conf.d/config.php +++ /dev/null @@ -1,181 +0,0 @@ - - * array('//symlink' => '/real/target/path') // unix - * array('//static' => 'D:\\staticStorage') // Windows - * - */ -$min_symlinks = array(); - - -/** - * If you upload files from Windows to a non-Windows server, Windows may report - * incorrect mtimes for the files. This may cause Minify to keep serving stale - * cache files when source file changes are made too frequently (e.g. more than - * once an hour). - * - * 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. - * - * In the Windows SFTP client WinSCP, there's an option that may fix this - * issue without changing the variable below. Under login > environment, - * select the option "Adjust remote timestamp with DST". - * @link http://winscp.net/eng/docs/ui_login_environment#daylight_saving_time - */ -$min_uploaderHoursBehind = 0; - - -/** - * Path to Minify's lib folder. If you happen to move it, change - * this accordingly. - */ -$min_libPath = __DIR__ . '/lib'; -$min_cachePath = __DIR__ . '/../../../../tmp/cache_minify'; - -// try to disable output_compression (may not have an effect) -ini_set('zlib.output_compression', '0'); diff --git a/lib/conf.d/groupsConfig.php b/lib/conf.d/minifyGroupsConfig.php similarity index 89% rename from lib/conf.d/groupsConfig.php rename to lib/conf.d/minifyGroupsConfig.php index e0a9f097eb..a41ba6a68b 100644 --- a/lib/conf.d/groupsConfig.php +++ b/lib/conf.d/minifyGroupsConfig.php @@ -35,9 +35,9 @@ $groups = array( , '//include/account/geonames.js' , '//include/jquery.tooltip.js' , '//include/jslibs/jquery.contextmenu_scroll.js' - , '//include/vendor/javascript-load-image/load-image.js' - , '//include/vendor/jquery-file-upload/js/jquery.iframe-transport.js' - , '//include/vendor/jquery-file-upload/js/jquery.fileupload.js' + , '//assets/javascript-load-image/load-image.js' + , '//assets/jquery-file-upload/js/jquery.iframe-transport.js' + , '//assets/jquery-file-upload/js/jquery.fileupload.js' ), 'report' => array( '//include/jslibs/jquery-ui-1.8.17/jquery-ui-i18n.js' @@ -63,12 +63,12 @@ $groups = array( , '//include/jslibs/jquery-ui-1.8.17/jquery-ui-i18n.js' , '//include/jslibs/jquery.cookie.js' , '//include/jquery.common.js' - , '//include/vendor/humane-js/humane.js' - , '//include/vendor/mustache-js/mustache.js' - , '//include/vendor/javascript-load-image/load-image.js' - , '//include/vendor/jquery-file-upload/js/vendor/jquery.ui.widget.js' - , '//include/vendor/jquery-file-upload/js/jquery.iframe-transport.js' - , '//include/vendor/jquery-file-upload/js/jquery.fileupload.js' + , '//assets/humane-js/humane.js' + , '//assets/mustache/mustache.js' + , '//assets/blueimp-load-image/load-image.js' + , '//assets/jquery-file-upload/js/vendor/jquery.ui.widget.js' + , '//assets/jquery-file-upload/js/jquery.iframe-transport.js' + , '//assets/jquery-file-upload/js/jquery.fileupload.js' , '//include/account/geonames.js' , '//include/jslibs/jquery.form.2.49.js' , '//include/jslibs/jquery.vertical.buttonset.js'