php cli模式 laravel,仿照laravel 的php artisan写了个类似的php cli 功能

RTPHPCLI是一个仿照Artisan命令行工具,用于快速创建PHP模型、控制器和数据库迁移。它能帮助开发者便捷地生成表结构,并通过简单的类方法定义字段类型,如String、Int和Date。在执行页面,工具会读取模型文件并生成相应的数据库创建语句,实现了数据库表的自动化创建。
摘要由CSDN通过智能技术生成

RT

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

#!/usr/bin/env php -q      //声明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)

/**

* Created by PhpStorm.

* User: gewenrui

* Date: 16/1/11

* Time: 上午9:03

*/

class model

{

//内容

public function __construct($table)

{

$this->content =

$content =

"<?php

class $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);

}

}

}

数据库声明类型界面

/**

* 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)

require_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;

}

}

执行页面

/**

* 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 

$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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值