当前位置:Gxlcms > PHP教程 > 仿照laravel的phpqrtisan写了个类似的phpcli功能

仿照laravel的phpqrtisan写了个类似的phpcli功能

时间:2021-07-01 10:21:17 帮助过:24人阅读

RT

GITHUB资源

PHP CLI 实现快速创建表 仿照ARTISAN 的QUICK

#!/usr/bin/env php -q      //声明PHP文件input($input,@$status);//echo __DIR__;//require_once

快速创建model层(php quick model Message)

content =        $content =            "create($table);        $this->write($path);    }    public function create($table)    {        $path = __DIR__ . '/Tables/' . $table . '.class.php';        //创建文件        if (!is_file($path)) {            touch($path, 0777, true);            return $path;        } else {            return $path;        }    }    //写入    public function write($path)    {        if (is_file($path)) {            $myfile = fopen($path, "w") or die("Unable to open file!");            fwrite($myfile, $this->content);        }    }}

数据库声明类型界面

$data = "varchar($digit)";        return $this;    }    public function Int($data,$digit =10)    {        $this->$data = "int($digit)";        return $this;    }    public function date($data)    {        $this->$data = 'date';        return $this;    }    public function autoincrement($data)    {        $this->$data = $this->$data . '|primary key auto_increment';    }    public function index()    {        //return $this;    }    public function table($data)    {        $this->table = $data;    }}

创建model层(php quick model Message)

table('Message');        $migration->Int('id',10)->autoincrement('id');        $migration->String('name',10);        $migration->String('password',10);        return $migration;    }}

执行页面

serach();        $value = require_once './Event/Config/Config.php';        static $db;        $db = new \kakoi\DB($value);        $this->load($data);    }    //search    public function serach()    {        $path = __DIR__ . '/Tables';        $dir = opendir($path);        global $value;        //扫描文件夹下的数据        while (($file = readdir($dir)) !== false) {            if ($file != '.' && $file != '..') {                $data = explode('.', $file);                $value .= $data[0] . '|';            }        }        return $value;        closedir($dir);    }    //数据处理    public function load($data)    {        $data = rtrim($data, '|');        $data = explode('|', $data);        foreach ($data as $value) {            require_once __DIR__ . '/Tables/' . $value . '.class.php';            $value = new $value();            $data = $value->create(new Migration());            $this->handle($data);        }    }    /*public function loop($key){        global $data;        if(strpos($key,'|')){            $value = explode('|',$key);        }    }*/    //数据处理*2    public function handle($data)    {        global $case;        //因为字段信息通过“xx|xx” 来传递 如果出现了 INT(10)|AUTO_INCREMENT 通过下面的方法来便利取出值          foreach ($data as $value => $key) {            if ($value != 'table') {                $count = substr_count($key, '|');                if ($count > 0) {                    $temp = explode('|', $key);                    $key = '';                    $length = count($temp);                    for ($i = 0; $i < $length; $i++) {                        $key .= $temp[$i] . '   ';                    }                }                $case .= $value . '   ' . $key . ',';            }        }        //去掉最后的逗号        $case = rtrim($case, ',') . ')';        $temp = "create table {$data->table}(";        $sql = $temp . $case;        $record = migrate::execute($sql);        if ($record == false) {            echo "{$data->table}创建失败了=";        } else {            echo "{$data->table}创建成功了=";        }        unset($case);        // echo $sql;    }}

人气教程排行