diff --git a/lib/Minify.php b/lib/Minify.php index a14c769..8128668 100644 --- a/lib/Minify.php +++ b/lib/Minify.php @@ -132,7 +132,8 @@ class Minify { // make $controller into object $class = 'Minify_Controller_' . $controller; if (! class_exists($class, false)) { - require_once "Minify/Controller/{$controller}.php"; + require_once "Minify/Controller/" + . str_replace('_', '/', $controller) . ".php"; } $controller = new $class(); } @@ -161,6 +162,11 @@ class Minify { } self::$_controller = $controller; + + if (self::$_options['debug']) { + self::_setupDebug($controller->sources); + self::$_options['setExpires'] = time(); + } if (null === self::$_options['setExpires']) { // conditional GET @@ -295,6 +301,21 @@ class Minify { */ protected static $_options = null; + /** + * Set up sources to use Minify_Lines + * + * @param array $sources Minify_Source instances + */ + protected static function _setupDebug($sources) + { + foreach ($sources as $source) { + $source->minifier = array('Minify_Lines', 'minify'); + $source->minifyOptions = array( + 'id' => $source->getId() + ); + } + } + /** * Combines sources and minifies the result. * @@ -307,7 +328,6 @@ class Minify { $implodeSeparator = ($type === self::TYPE_JS) ? ';' : ''; - // allow the user to pass a particular array of options to each // minifier (designated by type). source objects may still override // these diff --git a/lib/Minify/Controller/Base.php b/lib/Minify/Controller/Base.php index dd290fc..7a54435 100644 --- a/lib/Minify/Controller/Base.php +++ b/lib/Minify/Controller/Base.php @@ -47,6 +47,7 @@ abstract class Minify_Controller_Base { ,'contentTypeCharset' => 'UTF-8' ,'setExpires' => null // use conditional GET ,'quiet' => false // serve() will send headers and output + ,'debug' => false // if you override this, the response code MUST be directly after // the first space. diff --git a/lib/Minify/Lines.php b/lib/Minify/Lines.php index c9cf8fe..37301a3 100644 --- a/lib/Minify/Lines.php +++ b/lib/Minify/Lines.php @@ -34,18 +34,20 @@ class Minify_Lines { $lines = explode($eol, $content); $numLines = count($lines); // determine left padding - $padTo = max(strlen($numLines), strlen($id)); + $padTo = strlen($numLines); $inComment = false; - for ($i = 0; $i < $numLines; ++$i) { - $n = $i + 1; - $line = $lines[$i]; - $note = (('' !== $id) && (1 == $n % 30)) - ? $id - : $n; - $lines[$i] = self::_addNote($line, $note, $inComment, $padTo); + $i = 0; + while (null !== ($line = array_shift($lines))) { + if (('' !== $id) && (0 == $i % 50)) { + $newLines[] = ''; + $newLines[] = "/* {$id} */"; + $newLines[] = ''; + } + ++$i; + $newLines[] = self::_addNote($line, $i, $inComment, $padTo); $inComment = self::_eolInComment($line, $inComment); } - return implode($eol, $lines) . $eol; + return implode($eol, $newLines) . $eol; } /** @@ -89,7 +91,7 @@ class Minify_Lines { private static function _addNote($line, $note, $inComment, $padTo) { return $inComment - ? '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' * ' . $line + ? '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' *| ' . $line : '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' */ ' . $line; } } diff --git a/lib/Minify/Source.php b/lib/Minify/Source.php index 337931e..a47794f 100644 --- a/lib/Minify/Source.php +++ b/lib/Minify/Source.php @@ -90,6 +90,16 @@ class Minify_Source { : $content; } + /** + * Get id + * + * @return string + */ + public function getId() + { + return $this->_id; + } + /** * Verifies a single minification call can handle all sources * diff --git a/web/test/test_Lines.php b/web/test/test_Lines.php new file mode 100644 index 0000000..d48fb40 --- /dev/null +++ b/web/test/test_Lines.php @@ -0,0 +1,50 @@ + true + ,'files' => array( + "{$thisDir}/_test_files/minify/email.js" + ,"{$thisDir}/_test_files/minify/QueryString.js" + ,"{$thisDir}/_test_files/js/before.js" + ) + )); + + /* + // build test file list + $d = dir($cssPath); + while (false !== ($entry = $d->read())) { + if (preg_match('/^([\w\\-]+)\.css$/', $entry, $m)) { + $list[] = $m[1]; + } + } + $d->close(); + + foreach ($list as $item) { + + $options = ($item === 'paths') + ? array('prependRelativePath' => '../') + : array(); + + $src = file_get_contents($cssPath . "/{$item}.css"); + $minExpected = file_get_contents($cssPath . "/{$item}.min.css"); + $minOutput = Minify_CSS::minify($src, $options); + $passed = assertTrue($minExpected === $minOutput, 'Minify_CSS : ' . $item); + + if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { + echo "\n---Output: " .strlen($minOutput). " bytes\n\n{$minOutput}\n\n"; + if (!$passed) { + echo "---Expected: " .strlen($minExpected). " bytes\n\n{$minExpected}\n\n"; + echo "---Source: " .strlen($src). " bytes\n\n{$src}\n\n\n"; + } + } + } */ +} + +test_Lines();