Skip to content

Commit

Permalink
Spellcheck, added TODO, better CLI output (including percentage), sma…
Browse files Browse the repository at this point in the history
…ll fixes
  • Loading branch information
Jurriaan Pruis authored and technosophos committed Jul 29, 2010
1 parent 01204da commit 114df87
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
10 changes: 5 additions & 5 deletions README
@@ -1,9 +1,9 @@
phpcompressor
Author: M Butcher (matt@aleph-null.tv)
Copyright 2009
Licensed under an MIT-style license. See COPYING-MIT.
phpcompactor
Authors: M Butcher (matt@aleph-null.tv), J Pruis (email@jurriaanpruis.nl)
Copyright 2009-2010
Licensed under an MIT-style license. See COPYING-MIT.txt

This package provides a very simple PHP code compressor.

Usage:
php ./phpcompactor.php compressed_file.php source_file.php
php ./src/phpcompactor.php compressed_file.php source_file.php
8 changes: 8 additions & 0 deletions TODO
@@ -0,0 +1,8 @@
- @Matt: fix COPYING, licence is for Querypath, not for PHPCompactor
- make testcases
- more tokens.. for example T_PRINT & T_ECHO (Problems when printing constants) echo'test'; and echo$test; are allowed, echoENT_QUOTES; not (of course)
- merge the two foreach structures (is this possible?, maybe by making a for loop previous == $tokens[$i-1])
- strip require/include (_once) paths from source
- make OO compactor lib
- create function for single files
- treat inline HTML better (also HTML compression?, internally i'm using a simple regexp: /(?<=>)\s+(?=<)/)
25 changes: 12 additions & 13 deletions src/phpcompactor.php
Expand Up @@ -6,32 +6,28 @@
*
* Modified by Jurriaan Pruis - Better 'compression'
*
* TODO: merge the two foreach structures
* TODO: more tokens.. for example T_PRINT & T_ECHO (Problems when printing constants) echo'test'; and echo$test; are allowed, echoENT_QUOTES; not (of course)
* TODO: strip require/include (_once) paths from source
*
*/
**/

if ($argc < 3) {
print "Strip unecessary data from PHP source files.\n\n\tUsage: php phpcompactor.php DESTINATION.php SOURCE.php";
print "Strip unnecessary data from PHP source files.\n\n\tUsage: php phpcompactor.php DESTINATION.php SOURCE.php\n";
exit;
}


$source = $argv[2];
$target = $argv[1];
//print "Compacting $source into $target.\n";
print "Compacting $source into $target.\n";

include $source;

$files = get_included_files();
$before = 0;
$out = fopen($target, 'w');
fwrite($out, '<?php' . PHP_EOL);
$next = false;
foreach ($files as $f) {
echo $f. "\n";
if ($f !== __FILE__) {
echo '+ compacting \''.$f. "'\n";
$before += filesize($f);
$contents = trim(file_get_contents($f));
$tokens = token_get_all($contents);
$previous = false;
Expand Down Expand Up @@ -122,13 +118,10 @@
case T_CONCAT_EQUAL:
$next = true;
fwrite($out, $token[1]);
$token[0] = token_name($token[0]);

break;
default:
$next = false;
fwrite($out, $token[1]);
$token[0] = token_name($token[0]);

}

Expand All @@ -139,4 +132,10 @@
}

fclose($out);
$after = filesize($target);
$percent = sprintf('%.2f%%', (($after/$before) - 1)*100);
echo 'Compacted '.count($files) . " files \n";
echo "Filesize report: $before bytes to $after bytes ($percent)\n";
echo "Done.\n";

?>

0 comments on commit 114df87

Please sign in to comment.