正则表达式,代码文件批量加版权注释信息

PHP · Fecmall · 于 2年前 发布 · 744 次阅读
// todo terry: 上线前删除掉
    public function actionVersion()
    {
        //$string = $this->readFile('/www/wwwroot/xxxx/services/role/ManagerService.php');
        //$string = $this->addVersion($string);
        //echo $string;exit;



        $basePatch = '/www/wwwroot/xxx.xxx.cn';
        //1.便利所有的php文件
        FileHelper::findFiles($basePatch, [
            'filter' => function ($path) use ($basePatch) {
                //echo $path . "\n";
                if (!file_exists($path)) {
                    return;
                }
                $extension = $this->getExtension($path);
                if ($extension != 'php') {
                    return;
                }
                if (strstr($path, $basePatch .'/vendor')) {
                    return;
                }
                if ($path == $basePatch .'/modules/console/controllers/SystemController.php') {
                    return;
                }
                $string = $this->readFile($path);
                if (!$string) {
                    return;
                }
                $string = $this->addVersion($string);
                $this->writeFile($path, $string);
                echo $path . "\n";
                //exit;
            }
        ]);
        //2.替换字符

        //3.覆盖写入
    }

    private function writeFile($file_path, $txt)
    {
        $myfile = fopen($file_path, "w") or die("Unable to open file!");
        fwrite($myfile, $txt);
        fclose($myfile);
    }

    private function readFile($file_path) {
        if(file_exists($file_path)){
            return file_get_contents($file_path);//将整个文件内容读入到一个字符串中
        }
    }


    private function addVersion($string)
    {
        $pattern = '/^<\?php([\s\S]*?)\*\/([\s\S]*?)\nnamespace\s{1}/i';
        $replacement = '<?php
namespace ';
        $string = preg_replace($pattern, $replacement, $string);

        $pattern = '/^<\?php{1}/i';
        $replacement = '<?php

/**
 * 商城系统1.0
 * @author xxxxx有限公司
 * @copyright Copyright (c) 2015-2021 
 * @link https://www.xxxx.com
 */
';
        $string = preg_replace($pattern, $replacement, $string);

        return $string;
    }

    private function getExtension($file)
    {
        return substr(strrchr($file, '.'), 1);
    }
共收到 0 条回复
没有找到数据。
添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics