php 原生文件转成laravel文件(controller)

此代码是把大商创的2.7.3版本admin里面的php换成laravel的Controllers

public function test()
    {
        $dir = 'D:/WWW/dsc2.7.3/admin/';//对应文件夹路径
        $handler = opendir('D:/WWW/dsc2.7.3/admin/');
        $arr = [];
        while( ($filename = readdir($handler)) !== false )
        {
            //略过linux目录的名字为'.'和‘..'的文件
            if($filename != "." && $filename != "..")
            {
                $arr[] = $filename;
                //输出文件名
//                echo $filename.'<br>';
            }
        }
        closedir($handler);
        $newDir = 'D:/WWW/new/';//要写到哪个文件夹,这里可以自定义
//        dump($arr);die;
        foreach ($arr as $key =>$value){
            if(!str_is('*.php',$value)){//不是php后缀的跳过,可以在上面读取文件名时候判断
                continue;
            }
//            if($key == 0){
                $name = $this->camelize($value);//下划线转驼峰
                $name = str_replace('.','Controller.',ucfirst($name));//转成laravel的controller模式,ucfirst首字母大写
                $myfile = fopen($newDir.$name, "w+");
                fwrite($myfile, "<?php\r");//写入首行
                fwrite($myfile, "namespace App\Http\Controllers\Inmanage;\r");
                fwrite($myfile, "use Illuminate\Http\Request;\r");
                fwrite($myfile, "use App\Http\Controllers\Controller;\r");
                fwrite($myfile, "class ".str_replace('.php','',$name)." extends BaseController\r{");//写入控制器名字
                $file = file_get_contents($dir.$value);
                $pattern ='/\[\'act\'\]\s+==\s+\'(.*?)\'|function\s+(.*?)\n{/';//正则表达
                preg_match_all($pattern,$file,$match);
                foreach ($match as $key =>$v){
                    if($key == 0){
                        continue;
                    }

                    foreach ($v as $i){
                        if(!empty($i)){
                            if(str_is('*(*',$i)){
                                fwrite($myfile, "\r\tpublic function ".$i."\t{\r\t}\r");
                            }else{
                                fwrite($myfile, "\r\tpublic function ".$i."()\r\t{\r\t}\r");
                            }

                        }
                    }
                }
                fwrite($myfile, "\r}");
                fclose($myfile);
                echo $newDir.$name.'<br>';
//            }

        }
//        dump($arr);die;
//        $this->listDir('D:/WWW/dsc2.7.3/admin/');
    }
    public function camelize($uncamelized_words,$separator='_')
    {
         $uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words));
        return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator );
    }

猜你喜欢

转载自blog.csdn.net/qq_16618179/article/details/86593764
今日推荐