仿照laravel 的phpqrtisan寫了個類似的php cli 功能

來源:互聯網
上載者:User
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;    }}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.