Heim  >  Artikel  >  Backend-Entwicklung  >  仿照laravel 的phpqrtisan写了个类似的php cli 功能

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

WBOY
WBOYOriginal
2016-06-23 13:19:03937Durchsuche

RT

GITHUB资源

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

#!/usr/bin/env php -q      //声明PHP文件<?php/** * Created by PhpStorm. * User: gewenrui * Date: 16/1/11 * Time: 上午7:37 */require_once __DIR__.'/kakoi/Creator/Creator.class.php';$obj = new Creator();/*传入的参数    1,modle      创建MODEL    2,controller 创建CONTROLLER    3,migrate    同步数据库*/$input  = $_SERVER["argv"][1];@$status = $_SERVER["argv"][2];$obj->input($input,@$status);//echo __DIR__;//require_once

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

<?php/** * Created by PhpStorm. * User: gewenrui * Date: 16/1/11 * Time: 上午9:03 */class model{    //内容    public function __construct($table)    {        $this->content =        $content =            "<?phpclass $table{      public function create(){            }        }";        $path = $this->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);        }    }}

数据库声明类型界面

<?php/** * Created by PhpStorm. * User: gewenrui * Date: 16/1/11 * Time: 上午11:12 */class Migration{    /*     *     * 设置类型     * */    public function String($data,$digit = 10)    {        $this->$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)

<?phprequire_once dirname(__DIR__) . '/Migration.class.php';class Message extends Migration{    public function create(Migration $migration)    {        $migration->table('Message');        $migration->Int('id',10)->autoincrement('id');        $migration->String('name',10);        $migration->String('password',10);        return $migration;    }}

执行页面

<?php/** * Created by PhpStorm. * User: gewenrui * Date: 16/1/11 * Time: 上午9:03 */require_once dirname(__DIR__) . '/DB/DB.class.php';class migrate extends \kakoi\DB{    public function __construct($table)    {        $data = $this->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;    }}


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn