1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-11 16:44:44 +02:00

Added 'debug' option to serve() to use Minify_Lines

This commit is contained in:
Steve Clay
2008-06-28 20:23:29 +00:00
parent 46f8993292
commit e77a1cf82c
5 changed files with 95 additions and 12 deletions

View File

@@ -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();
}
@@ -162,6 +163,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
require_once 'HTTP/ConditionalGet.php';
@@ -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

View File

@@ -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.

View File

@@ -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;
}
}

View File

@@ -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
*

50
web/test/test_Lines.php Normal file
View File

@@ -0,0 +1,50 @@
<?php
require_once '_inc.php';
require_once 'Minify.php';
function test_Lines()
{
global $thisDir;
Minify::serve('Files', array(
'debug' => 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();