示例#1
0
 public function encode_file($path, $base)
 {
     $rpath = self::relativePath($path, $base);
     if (preg_match('/\\.(php|phtml)$/', $path)) {
         echo "    PHP: {$rpath}...";
         //移除脚本中的注释和回车
         $content = file_get_contents($path);
         $total = strlen($content);
         // 预编译代码
         if (class_exists('\\Gini\\Dev\\Macro')) {
             $content = Macro::compile($content);
         }
         // 混淆变量
         if (class_exists('\\Gini\\Dev\\Obfuscator')) {
             $ob = new Obfuscator($content);
             $ob->set_reserved_keywords(['$config', '$lang']);
             $content = $ob->format();
         }
         $converted = strlen($content);
         $percentage = round($converted * 100 / $total, 1);
         echo " {$percentage}%";
     } elseif (fnmatch("*.js", $path)) {
         echo "     JS: {$rpath}...";
         $content = @file_get_contents($path);
         $total = strlen($content);
         $content = shell_exec('uglifyjs ' . escapeshellarg($path) . ' 2>&1');
         $converted = strlen($content);
         $percentage = round($converted * 100 / $total, 1);
         echo " {$percentage}%";
     } elseif (fnmatch('*.css', $path)) {
         echo "    CSS: {$rpath}...";
         $content = @file_get_contents($path);
         $total = strlen($content);
         $content = \CSSMin::minify($content);
         $converted = strlen($content);
         $percentage = round($converted * 100 / $total, 1);
         echo " {$percentage}%";
     } else {
         echo "    DUP: {$rpath}...";
         //复制相关文件
         $content = @file_get_contents($path);
         $total = strlen($content);
         echo "{$total} bytes";
     }
     if ($content) {
         $this->phar[$rpath] = $content;
     } else {
         echo "... EMPTY";
     }
     echo "\n";
 }