欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP 源代码压缩小工具

程序员文章站 2022-06-12 18:29:41
使用方法:(在命令行运行) 复制代码 代码如下:php compactor.php destination.php source.php 下载: 复制代码 代码如下:#!/...
使用方法:(在命令行运行)
复制代码 代码如下:

php compactor.php destination.php source.php

下载:
复制代码 代码如下:

#!/usr/bin/env php
<?php
/**
* compact php code.
*
* strip comments, combine entire library into one file.
*/

if ($argc < 3) {
print "strip unecessary data from php source files.\n\n\tusage: php compactor.php destination.php source.php";
exit;
}


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

include $source;

$files = get_included_files();
print_r($files);

$out = fopen($target, 'w');
fwrite($out, '<?php' . php_eol);
fwrite($out, '// querypath. copyright (c) 2009, matt butcher.' . php_eol);
fwrite($out, '// this software is released under the lgpl, v. 2.1 or an mit-style license.' . php_eol);
fwrite($out ,'// http://opensource.org/licenses/lgpl-2.1.php');
fwrite($out, '// http://querypath.org.' . php_eol);
foreach ($files as $f) {
if ($f !== __file__) {
$contents = file_get_contents($f);
foreach (token_get_all($contents) as $token) {
if (is_string($token)) {
fwrite($out, $token);
}
else {
switch ($token[0]) {
case t_require:
case t_require_once:
case t_include_once:
// we leave t_include since it is rarely used to include
// libraries and often used to include html/template files.
case t_comment:
case t_doc_comment:
case t_open_tag:
case t_close_tag:
break;
case t_whitespace:
fwrite($out, ' ');
break;
default:
fwrite($out, $token[1]);
}

}
}
}
}
fclose($out);
?>