97 lines
3.3 KiB
PHP
Raw Normal View History

2020-04-28 16:34:17 +08:00
<?php
2020-04-28 22:02:03 +08:00
namespace catcher\generate\factory;
use catcher\exceptions\FailedException;
2020-11-19 17:31:31 +08:00
use catcher\facade\FileSystem;
use catcher\generate\build\CatchBuild;
use catcher\generate\build\classes\Classes;
use catcher\generate\build\classes\Property;
use catcher\generate\build\classes\Traits;
use catcher\generate\build\classes\Uses;
use catcher\generate\build\types\Arr;
use catcher\traits\db\BaseOptionsTrait;
use catcher\traits\db\ScopeTrait;
use catcher\Utils;
2020-04-28 22:02:03 +08:00
use think\facade\Db;
2020-04-30 18:15:48 +08:00
use think\helper\Str;
2020-04-28 22:02:03 +08:00
class Model extends Factory
{
2020-11-19 17:31:31 +08:00
/**
* done
*
* @time 2020年11月19日
* @param $params
* @return string
*/
2021-03-14 07:41:15 +08:00
public function done(array $params): string
2020-04-29 09:02:20 +08:00
{
2020-05-05 21:52:44 +08:00
$content = $this->getContent($params);
2020-04-29 09:02:20 +08:00
2020-07-11 10:59:57 +08:00
$modelPath = $this->getGeneratePath($params['model']);
2020-04-29 09:02:20 +08:00
2020-11-19 17:31:31 +08:00
FileSystem::put($modelPath, $content);
2020-07-11 10:59:57 +08:00
if (!file_exists($modelPath)) {
2020-04-29 09:02:20 +08:00
throw new FailedException('create model failed');
}
2020-07-11 10:59:57 +08:00
return $modelPath;
2020-04-29 09:02:20 +08:00
}
/**
* get contents
*
* @time 2020年04月29日
* @param $params
* @return string|string[]
*/
2020-05-28 06:37:32 +08:00
public function getContent($params)
2020-04-28 22:02:03 +08:00
{
$extra = $params['extra'];
$table = Utils::tableWithPrefix($params['table']);
2020-04-28 22:02:03 +08:00
[$modelName, $namespace] = $this->parseFilename($params['model']);
2020-04-30 18:15:48 +08:00
// 如果填写了表名并且没有填写模型名称 使用表名作为模型名称
if (!$modelName && $table) {
2020-05-05 21:52:44 +08:00
$modelName = ucfirst(Str::camel($table));
$params['model'] = $params['model'] . $modelName;
2020-04-30 18:15:48 +08:00
}
2020-04-28 22:02:03 +08:00
if (!$modelName) {
2020-04-29 11:41:54 +08:00
throw new FailedException('model name not set');
2020-04-28 22:02:03 +08:00
}
2020-11-19 17:31:31 +08:00
$softDelete = $extra['soft_delete'];
return (new CatchBuild)->namespace($namespace)
->use((new Uses())->name('catcher\base\CatchModel', 'Model'))
->when(!$softDelete, function (CatchBuild $build){
$build->use((new Uses())->name(BaseOptionsTrait::class));
$build->use((new Uses())->name(ScopeTrait::class));
})
->class((new Classes($modelName))->extend('Model')->docComment(),
function (Classes $class) use ($softDelete, $table) {
if (!$softDelete) {
$class->addTrait(
(new Traits())->use('BaseOptionsTrait', 'ScopeTrait')
);
}
$class->addProperty(
(new Property('name'))->default(
Utils::tableWithoutPrefix($table)
)->docComment('// 表名')
2020-11-19 17:31:31 +08:00
);
2020-11-19 19:11:18 +08:00
$class->when($this->hasTableExists($table), function ($class) use ($table){
$class->addProperty(
(new Property('field'))->default(
(new Arr)->build(Db::getFields($table))
)->docComment('// 数据库字段映射'));
2020-11-19 19:11:18 +08:00
});
2020-11-19 17:31:31 +08:00
})->getContent();
2020-04-28 22:02:03 +08:00
}
}