CqhModel.class.php
<?php
namespace Cqh\Model;
use Think\Model;
class CqhModel extends Model
{
protected $trueTabelName; //对应数据库中的表名
protected $moduleName; //对应的模块名称
protected $tpName; //表对应的TP名
protected $fields; //所有字段信息
protected $tableComment; //表注释
public function iniSet($tableName,$moduleName)
{
$this->buildDir($moduleName);
$this->setAttribute($tableName,$moduleName);
return TRUE;
}
/*********************** 生成控制器 ***********************/
public function gController()
{
$cDir = APP_PATH . $this->moduleName . ‘/Controller‘;
$file = $cDir . ‘/‘ . $this->tpName . ‘Controller.class.php‘;
ob_start();
include(APP_PATH . ‘Cqh/Template/Controller.php‘);
$str = ob_get_clean();
if(!is_file($file))
{
file_put_contents($file,"<?php\r\n".$str);
}
return TRUE;
}
/*********************** 生成模型 ***********************/
public function gModel()
{
$mDir = APP_PATH.$this->moduleName.‘/Model‘;
if(!is_dir($mDir)) {
mkdir($mDir,0755,true);
}
$file = $mDir . ‘/‘ . $this->tpName . ‘Model.class.php‘;
ob_start();
include(APP_PATH . ‘Cqh/Template/model.php‘);
$str = ob_get_clean();
if(!is_file($file))
{
file_put_contents($file,"<?php\r\n".$str);
}
return TRUE;
}
/*********************** 生成静态页 ***********************/
public function gView()
{
$vDir = APP_PATH . $this->moduleName . ‘/View/‘.$this->tpName;
if(!is_dir($vDir)) {
mkdir($vDir,755,TRUE);
}
$tableComment=$this->tableComment;
$arr = array(‘add‘,‘save‘,‘lst‘);
foreach($arr as $v)
{
$file = $vDir."/$v.html";
ob_start();
include(APP_PATH . "Cqh/Template/$v.html");
$str = ob_get_clean();
if(!is_file($file)) {
file_put_contents($file, $str);
}
}
return TRUE;
}
/********************** 初始化属性 **********************/
private function setAttribute($tableName,$moduleName)
{
/**************** 初始化属性$moduleName ****************/
$this->moduleName = $moduleName;
/**************** 初始化属性$trueTabelName ****************/
//判断如果没有表前缀就加上
$prefix = C(‘DB_PREFIX‘);
if(strpos($tableName,$prefix) !== 0)
$this->trueTabelName = $prefix.$tableName;
else
$this->trueTabelName = $tableName;
/**************** 初始化属性$tpName ****************/
//去掉表前缀
if(strpos($tableName,$prefix) === 0)
{
$len = strlen($prefix);
//把表名从前缀开始截取到最后
$tableName = substr($this->trueTabelName, $len);
}
//去掉下划线
$tableName = explode(‘_‘,$tableName);
//把$tableName中第一个元素都使用ucfirst处理一遍
$tableName = array_map(‘ucfirst‘,$tableName);
$tableName = implode(‘‘,$tableName);
$this->tpName = $tableName;
/**************** 初始化属性$fields ****************/
//取出所有的字段的信息
$this->fields = $this->query(‘SHOW FULL FIELDS FROM ‘.$this->trueTabelName);
/**************** 初始化属性$tableComment ****************/
$result = $this->query("SELECT TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA=‘".C(‘DB_NAME‘)."‘ and TABLE_NAME=‘".$this->trueTabelName."‘");
$this->tableComment = $result[0][‘TABLE_COMMENT‘];
return TRUE;
}
/********************** 创建模块目录 **********************/
public function buildDir($module) {
// 没有创建的话自动创建
if(is_writeable(APP_PATH)) {
$dirs = array(
APP_PATH.$module.‘/‘,
APP_PATH.$module.‘/Common/‘,
APP_PATH.$module.‘/Controller/‘,
APP_PATH.$module.‘/Model/‘,
APP_PATH.$module.‘/Conf/‘,
APP_PATH.$module.‘/View/‘,
);
foreach ($dirs as $dir){
if(!is_dir($dir)) mkdir($dir,0755,true);
}
}
}
}
CqhController.class.php
<?php
namespace Cqh\Controller;
use Think\Controller;
class CqhController extends Controller
{
public function index()
{
if(IS_POST)
{
$tableName = I(‘post.tableName‘); //接收表单中的表名
$moduleName = ucfirst(I(‘post.moduleName‘)); //接收模块名
$generaType = I(‘post.generaType‘);//接收需要生成的类型
$this->validate($tableName,$moduleName,$generaType);//验证表单
$gModel = D(‘Cqh‘);
$gModel->iniSet($tableName,$moduleName); //初始化,传入表名和模块名
if($generaType[‘controller‘])
$gModel->gController(); //生成控制器
if($generaType[‘model‘])
$gModel->gModel(); //生成模型
if($generaType[‘view‘])
$gModel->gView(); //生成视图
$this->success(‘生成成功‘);
exit;
}
$this->display();
}
public function validate($tableName,$moduleName,$generaType)
{
if(!((preg_match(‘/\S+/‘,$tableName) === 1)))
$this->error(‘请输入表名‘);
if(!((preg_match(‘/\S+/‘,$moduleName) === 1)))
$this->error(‘请输入模块名‘);
if(!$generaType)
$this->error(‘请选择要生成的类型‘);
}
}
View/Cqh/index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cqh代码生成器</title>
<style>
*{font-size:25px;}
.title{font-size:35px;;font-weight: bold;margin:auto auto;}
input[type=text]{width:200px;height:30px;font-size:25px;}
</style>
</head>
<body>
<div class="title">Cqh代码生成器</div>
<form method="post" action="__SELF__">
<table>
<tr>
<td>表名</td>
<td>
<input type="text" name="tableName"/>
</td>
</tr>
<tr>
<td>模块名</td>
<td>
<input type="text" name="moduleName"/>
</td>
</tr>
<tr>
<td>选择要生成的内容</td>
<td>
<input type="checkbox" name="generaType[controller]" value="1" checked="checked"/>控制器
<input type="checkbox" name="generaType[model]" value="1" checked="checked"/>模型
<input type="checkbox" name="generaType[view]" value="1" checked="checked"/>视图
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" class="button" value=" 确定 "/>
<input type="reset" class="button" value=" 重置 "/>
</td>
</tr>
</table>
</form>
</body>
</html>
原文:http://www.cnblogs.com/chenqionghe/p/4374422.html