one.php 多项目、函数库、类库 统一为一个版本的方法

吾爱主题 阅读:141 2021-10-22 14:42:00 评论:0

现在 手里 有好几个 项目在进行,每个项目都有部分通用的代码,只想维护一个 函数库类库,并且每个项目都不想有冗余代码,函数功能更新后,其他项目的函数也需要更新。晚上抽空写了个 简单的打包小脚本:one.php,以后 更新函数或类时,只需要在唯一的 函数库、类库 中更新,其他项目使用 打包后的 php 脚本即可(理论上也能提高PHP的运行速度,只需要加载、分析一个文件)。

因为我的 函数库、类库都在一个目录下,所以没有针对相对路径 做处理(懒),cmd 进入 core 目录,执行 php one.php 即可按规则打包成一个独立的文件,运行效果如下。

打包流程,以 public.php 为例。

现在功能有限,仅支持 同一个目录(因为我只用到了单目录),如果有哪位大神 在此基础上修改了多目录版本,请一定要分享一分给我。

至于用处,除了 方便维护多个项目(A项目、B项目)或同一个项目的多个版本(比如:VIP版、普通版),最大的用处,可以用于商业版程序混淆加密。比如商业软件:index.php,product.php 每个文件都打包混淆加密,每个文件都包含了所有的代码(几万行)。破解者解密后,看到几万行代码,上百个函数(可能都还有用),同一个功能,各个文件内的函数名都不一致,会哭死的。。。。

测试包下载地址:

one.php 源代码:onephp.rar

核心代码

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 <?php /**   * 类名:One   * 作者:mqycn   * 博客:http://www.miaoqiyuan.cn   * 源码:http://www.miaoqiyuan.cn/p/one-php   * 说明:多项目 函数库、类库 统一为一个版本的方法   */   class OneFile {      //已经合并的文件    public static $includes ;      //处理一个文件    public static function run( $index_file , $output_file ) {      self:: $includes = array ();      self::log( 'Input' , $index_file );      $output = self::parse( $index_file );      file_put_contents ( $output_file , self::repair( $output ));      self::log( 'Output' , $output_file );    }      //分析PHP文件    public static function parse( $file ) {      if ( empty (self:: $includes [ $file ])) {        self::log( 'Append' , $file );        self:: $includes [ $file ] = true;        $code = file_get_contents ( $file );        if (preg_match_all( "/(require_once|require|include_once|include)\s+'([^']*)';/" , $code , $match )) {          for ( $i = 0; $i < count ( $match [0]); $i ++) {            $code = str_replace ( $match [0][ $i ], self::parse( $match [2][ $i ]), $code );          }          }        return $code ;      } else {        self::log( 'Ignore' , $file );        return '' ;      }    }      //代码修复    public static function repair( $code ) {      $php_prefix = "<?php\r\n" ;      $php_suffix = "\r\n?>" ;      $code = str_replace ( "\n" , "\r\n" , $code );      $code = str_replace ( "\r\r\n" , "\r\n" , $code );      $code = str_replace ( $php_prefix , '' , $code );      $code = str_replace ( $php_suffix , '' , $code );      for ( $i = 0; $i < 5; $i ++) {        $code = str_replace ( "\r\n\r\n" , "\r\n" , $code );      }      return $php_prefix . $code . $php_suffix ;    }      //输出日志    public static function log( $type , $text , $status = '' ) {      if (in_array( $type , array ( 'Append' , 'Ignore' ))) {        $status = "- ${type}" ;        $type = " |-- " ;      } else {        $type = "${type}:" ;      }      echo "${type} ${text} {$status}\r\n" ;    } }   OneFile::run( 'vip.php' , '../vip.php' ); OneFile::run( 'public.php' , '../public.php' );

到此这篇关于one.php 多项目、函数库、类库 统一为一个版本的方法的文章就介绍到这了,更多相关多项目、函数库、类库统一为一个内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:http://www.miaoqiyuan.cn/p/one-php

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容