easyswoole代码自动生成crud
- 根据表生成模型和控制器
- 根据表生成模型
- 根据表生成控制器
- 控制器模板
- 核心控制器代码curd.php
根据表生成模型和控制器
会在 App/Model目录下生成驼峰方式命名的模型文件
会在App/HttpController/Api 目录下生成驼峰方式命名的控制文件
curl http:localhost:9501/Curd/t?table_name=xxx
根据表生成模型
会在 App/Model目录下生成驼峰方式命名的模型文件
curl http://localhost:9501/curd/m?table_name=xxx
根据表生成控制器
会在App/HttpController/Api 目录下生成驼峰方式命名的控制文件
curl http://localhost:9501/curd/c?table_name=xxx
控制器模板
核心控制器代码curd.php
<?php
/*************************************************************************
* File Name: Curd.php
* Author: wangxd@yisa.com
* Created Time: 2022/9/9 15:39
* Description: 代码生成控制器
*
*************************************************************************/
namespace App\HttpController;
use EasySwoole\FastCache\Cache;
use EasySwoole\EasySwoole\Config;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\ORM\DbManager;
class Curd extends Controller {
/**
*
* 根据表自动生成模型和控制器
* @Param(name="table_name", alias="表名")
* @throws \EasySwoole\ORM\Exception\Exception
* @throws \Throwable
*/
public function t()
{
$params = $this->request()->getQueryParams();
$table_name=$params['table_name'];
$this->create_model($table_name);
$this->create_cotroller($table_name);
$this->writeJson(200, [], "success");
}
/**
*
* 根据表自动生成模型
* @Param(name="table_name", alias="表名")
* @throws \EasySwoole\ORM\Exception\Exception
* @throws \Throwable
*/
public function m(){
$params = $this->request()->getQueryParams();
$table_name=$params['table_name'];
$model_name =$this->camelize($table_name);
$this->create_model($table_name,$model_name);
$this->writeJson(200, [], "success");
}
/**
*
* 根据表自动生成控制器
* @Param(name="table_name", alias="表名")
* @throws \EasySwoole\ORM\Exception\Exception
* @throws \Throwable
*/
public function c(){
$params = $this->request()->getQueryParams();
$table_name=$params['table_name'];
$model_name =$this->camelize($table_name);
$this->create_cotroller($table_name);
$this->writeJson(200, [], "success");
}
/**
* 创建模型
* @table_name 表名
* @model_name 模型名
*/
public function create_model($table_name){
$model_name =$this->camelize($table_name).'Model';
$created_time=date("Y/m/d H:i");
$model_code=<<<EOF
<?php
/*************************************************************************
* Author: gerenator@yisa.com
* Created Time: $created_time
* Description: 模型
*************************************************************************/
namespace App\Model;
use EasySwoole\ORM\DbManager;
class $model_name extends \EasySwoole\ORM\AbstractModel {
protected \$connectionName = 'default';
protected \$tableName = '$table_name';
}
EOF;
$model_file_name='App/Model/'.$model_name.'.php';
if (!file_exists($model_file_name)){
file_put_contents($model_file_name,$model_code);
}
}
/**
* 创建模型
* @table_name 表名
* @model_name 模型名
*/
public function create_cotroller($table_name){
$controller_name =$this->camelize($table_name);
$model_name =$this->camelize($table_name).'Model';
$created_time=date("Y/m/d H:i");
$cotroller_file_name='App/HttpController/Api/'.$controller_name.'.php';
//从数据库中获取表字段信息
$sql = "desc " .$table_name;
//加载主表的列
$queryBuild = new QueryBuilder();
// 支持参数绑定 第二个参数非必传
$queryBuild->raw($sql);
// $queryBuild->raw("select * from test where name = 'siam'");
// 第二个参数 raw 指定true,表示执行原生sql
// 第三个参数 connectionName 指定使用的连接名,默认 default
$data = DbManager::getInstance()->query($queryBuild, true, 'default');
$feld_data=$data->getResult();
$param_to_str='';
$conditionQueryStr='';
$param_to_updateData_str='';
foreach ($feld_data as $k=> $fda){
if ($fda['Field'] !='id' ){
$param_to_str.= " \$data['{$fda['Field']}'] = \$param['{$fda['Field']}'];".PHP_EOL;
$conditionQueryStr.=" Tool::conditionQuery(\$where,\$param, '{$fda['Field']}', '{$fda['Field']}', '=');".PHP_EOL ;
$param_to_updateData_str.=" if (!empty( \$param['{$fda['Field']}'])) \$updateData['{$fda['Field']}'] = \$param['{$fda['Field']}'];".PHP_EOL ;
}
}
if (!file_exists($cotroller_file_name)){
$controller_code=file_get_contents('generator/Template/CurdController.stub');
$controller_code=str_replace('{%model_name%}',$model_name,$controller_code);
$controller_code=str_replace('{%controller_name%}',$controller_name,$controller_code);
$controller_code=str_replace('{%param_to_data%}',$param_to_str,$controller_code);
$controller_code=str_replace('{%conditionQuery%}',$conditionQueryStr,$controller_code);
$controller_code=str_replace('{%param_to_updateData%}',$param_to_updateData_str,$controller_code);
file_put_contents($cotroller_file_name,$controller_code);
}
}
/**
* 下划线转驼峰 首字母大写
* 思路:
* step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符
* step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符.
*/
public function camelize($uncamelized_words,$separator='_')
{
$uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words));
return ucfirst( ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator ));
}
/**
* 驼峰命名转下划线命名
* 思路:
* 小写和大写紧挨一起的地方,加上分隔符,然后全部转小写
*/
public function uncamelize($camelCaps,$separator='_')
{
return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
}
}