zoukankan      html  css  js  c++  java
  • 自己用的PHP源码批量压缩

    清理注释、换行、空格。可能有bug。
    <?php
    header('Content-Type: text/html; charset=utf-8');
    
    $source = './demo/src';
    $destination = './demo/dst';
    
    $path = array($source);
    $file = array();
    
    while ($path){
        $path_cur = array_pop($path);
        @mkdir(str_replace($source, $destination, $path_cur));
        $file = array_merge($file, glob($path_cur.'/*.php'));
        $path = array_merge($path, glob($path_cur.'/*', GLOB_ONLYDIR));
    }
    
    foreach ($file as $src){
        $dst = str_replace($source, $destination, $src);
        $content = file_get_contents($src);
        $code = compress($content);
        file_put_contents ($dst, $code, LOCK_EX);
        echo sprintf("%s : %s -> %s\n", $dst, filesize($src), filesize($dst));
    }
    
    function compress($content){
            $code = array();
            $tokens = token_get_all($content);
            foreach ($tokens as $i => $token) {
                if (is_string($token)){
                    $code[$i] = $token;
                } else {
                    switch ($token[0]) {
                        case T_OPEN_TAG:
                        //case T_CLOSE_TAG:
                            $code[$i] = '<?php ';
                            break;
                        case T_COMMENT:
                        case T_DOC_COMMENT:
                        case T_WHITESPACE:
                            break;
                        case T_CASE:
                        case T_CLASS:
                        case T_CLONE:
                        case T_CONST:
                        case T_ECHO:
                        case T_FUNCTION:
                        case T_GLOBAL:
                        case T_IMPLEMENTS:
                        case T_INTERFACE:
                        case T_INCLUDE:
                        case T_INCLUDE_ONCE:
                        case T_INSTANCEOF:
                        case T_NEW:
                        case T_PRIVATE:
                        case T_PUBLIC:
                        case T_PROTECTED:
                        case T_REQUIRE:
                        case T_REQUIRE_ONCE:
                        case T_RETURN:
                        case T_STATIC:
                        case T_THROW:
                        case T_VAR:
                            $code[$i] = $token[1].' ';
                            break;
                        case T_EXTENDS:
                        case T_AS:
                        case T_LOGICAL_AND:
                        case T_LOGICAL_OR:
                        case T_LOGICAL_XOR:
                            $code[$i] = ' '.$token[1].' ';
                            break;
                        default:
                            $code[$i] = $token[1];
                    }
                }
            }
            return implode('', $code);
        }
    ?>
    
  • 相关阅读:
    软件工程第1次阅读作业
    软件工程第0次作业
    OO第四次博客
    OO第三次总结
    OO第二次总结
    OO第一次总结
    提问回顾与个人总结
    结对项目:最长单词链
    第一次阅读作业
    第四次博客
  • 原文地址:https://www.cnblogs.com/dishuostec/p/2002439.html
Copyright © 2011-2022 走看看