From 7dbd2c87e429467a0ed72207bd53285a7f192017 Mon Sep 17 00:00:00 2001 From: Steve Clay Date: Fri, 26 Feb 2016 11:16:16 -0500 Subject: [PATCH] Minify no longer tries to minify -min.js/.min.js files 2.2 used empty string as a magical value meaning do not minify, but in the refactoring `Minify::combineMinify` forgot to interpret this value that way and instead inherited the default compressor for the type. This eliminates `""` as a magical value, but for BC rewrites it to `Minify::nullMinifier` in the sources. Fixes #499 --- docs/CustomSource.wiki.md | 2 +- lib/Minify.php | 18 ++++++++++++++++-- lib/Minify/Controller/MinApp.php | 2 +- lib/Minify/Source.php | 11 +++++++++-- lib/Minify/Source/Factory.php | 2 +- lib/Minify/SourceInterface.php | 2 +- min_extras/cli/minify.php | 2 +- min_extras/cli/rewrite-uris.php | 2 +- 8 files changed, 31 insertions(+), 10 deletions(-) diff --git a/docs/CustomSource.wiki.md b/docs/CustomSource.wiki.md index 14859d2..9e33cbf 100644 --- a/docs/CustomSource.wiki.md +++ b/docs/CustomSource.wiki.md @@ -39,7 +39,7 @@ $src1 = new Minify_Source(array( )); $src2 = new Minify_Source(array( 'filepath' => '//js/file2.js', - 'minifier' => '', // don't compress + 'minifier' => 'Minify::nullMinifier', // don't compress )); ``` In the above, `JmyJsMinifier()` is only called when the contents of `$src1` is needed. diff --git a/lib/Minify.php b/lib/Minify.php index 8ecc4d8..91ac7b6 100644 --- a/lib/Minify.php +++ b/lib/Minify.php @@ -168,7 +168,7 @@ class Minify { * $options['minifiers'][Minify::TYPE_CSS] = 'customCssMinifier'; * * // don't minify Javascript at all - * $options['minifiers'][Minify::TYPE_JS] = ''; + * $options['minifiers'][Minify::TYPE_JS] = 'Minify::nullMinifier'; * * * 'minifierOptions' : to send options to the minifier function, specify your options @@ -293,7 +293,7 @@ class Minify { $this->options['minifiers'][self::TYPE_JS] = false; foreach ($this->sources as $key => $source) { if ($this->options['contentType'] === self::TYPE_JS) { - $source->setMinifier(""); + $source->setMinifier('Minify::nullMinifier'); } elseif ($this->options['contentType'] === self::TYPE_CSS) { $source->setMinifier(array('Minify_CSSmin', 'minify')); $sourceOpts = $source->getMinifierOptions(); @@ -458,6 +458,20 @@ class Minify { exit; } + /** + * Default minifier for .min or -min JS files. + * + * @param string $content + * @return string + */ + public static function nullMinifier($content) { + if (isset($content[0]) && $content[0] === "\xef") { + $content = substr($content, 3); + } + $content = str_replace("\r\n", "\n", $content); + return trim($content); + } + /** * Setup CSS sources for URI rewriting */ diff --git a/lib/Minify/Controller/MinApp.php b/lib/Minify/Controller/MinApp.php index 6517e8a..f66b42e 100644 --- a/lib/Minify/Controller/MinApp.php +++ b/lib/Minify/Controller/MinApp.php @@ -190,7 +190,7 @@ class Minify_Controller_MinApp extends Minify_Controller_Base { ,'lastModified' => 0 // due to caching, filename is unreliable. ,'content' => "/* Minify: at least one missing file. See " . Minify::URL_DEBUG . " */\n" - ,'minifier' => '' + ,'minifier' => 'Minify::nullMinifier' ))); } diff --git a/lib/Minify/Source.php b/lib/Minify/Source.php index 34daadd..b44107e 100644 --- a/lib/Minify/Source.php +++ b/lib/Minify/Source.php @@ -104,7 +104,7 @@ class Minify_Source implements Minify_SourceInterface { $this->contentType = $spec['contentType']; } if (isset($spec['minifier'])) { - $this->minifier = $spec['minifier']; + $this->setMinifier($spec['minifier']); } if (isset($spec['minifyOptions'])) { $this->minifyOptions = $spec['minifyOptions']; @@ -130,8 +130,15 @@ class Minify_Source implements Minify_SourceInterface { /** * {@inheritdoc} */ - public function setMinifier($minifier) + public function setMinifier($minifier = null) { + if ($minifier === '') { + error_log(__METHOD__ . " cannot accept empty string. Use 'Minify::nullMinifier' or 'trim'."); + $minifier = 'Minify::nullMinifier'; + } + if ($minifier !== null && !is_callable($minifier, true)) { + throw new \InvalidArgumentException('minifier must be null or a valid callable'); + } $this->minifier = $minifier; } diff --git a/lib/Minify/Source/Factory.php b/lib/Minify/Source/Factory.php index c65ce6d..ef1ec22 100644 --- a/lib/Minify/Source/Factory.php +++ b/lib/Minify/Source/Factory.php @@ -173,7 +173,7 @@ class Minify_Source_Factory { $spec['minifyOptions']['compress'] = false; // we still want URI rewriting to work for CSS } else { - $spec['minifier'] = ''; + $spec['minifier'] = 'Minify::nullMinifier'; } } diff --git a/lib/Minify/SourceInterface.php b/lib/Minify/SourceInterface.php index fa721f9..797d08c 100644 --- a/lib/Minify/SourceInterface.php +++ b/lib/Minify/SourceInterface.php @@ -27,7 +27,7 @@ interface Minify_SourceInterface { * @param callable $minifier * @return void */ - public function setMinifier($minifier); + public function setMinifier($minifier = null); /** * Get options for the minifier diff --git a/min_extras/cli/minify.php b/min_extras/cli/minify.php index 30394e9..407685b 100755 --- a/min_extras/cli/minify.php +++ b/min_extras/cli/minify.php @@ -55,7 +55,7 @@ if ($paths) { $sources[] = new Minify_Source(array( 'id' => $path, 'content' => "/*** $path not found ***/\n", - 'minifier' => '', + 'minifier' => 'Minify::nullMinifier', )); } } diff --git a/min_extras/cli/rewrite-uris.php b/min_extras/cli/rewrite-uris.php index 908d1f3..0ca560f 100755 --- a/min_extras/cli/rewrite-uris.php +++ b/min_extras/cli/rewrite-uris.php @@ -45,7 +45,7 @@ foreach ($paths as $path) { $sources[] = new Minify_Source(array( 'id' => $path, 'content' => "/*** $path not found ***/\n", - 'minifier' => '', + 'minifier' => 'Minify::nullMinifier', )); } }