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\traits\db\BaseOptionsTrait;
|
|
|
|
use catcher\traits\db\ScopeTrait;
|
2020-12-01 17:55:36 +08:00
|
|
|
use catcher\Utils;
|
2021-06-10 19:38:44 +08:00
|
|
|
use JaguarJack\Generate\Build\Class_;
|
|
|
|
use JaguarJack\Generate\Build\Property;
|
|
|
|
use JaguarJack\Generate\Generator;
|
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[]
|
2021-06-10 19:38:44 +08:00
|
|
|
* @throws \JaguarJack\Generate\Exceptions\TypeNotFoundException
|
2020-04-29 09:02:20 +08:00
|
|
|
*/
|
2020-05-28 06:37:32 +08:00
|
|
|
public function getContent($params)
|
2020-04-28 22:02:03 +08:00
|
|
|
{
|
|
|
|
$extra = $params['extra'];
|
|
|
|
|
2020-12-26 20:56:10 +08:00
|
|
|
$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'];
|
|
|
|
|
2021-06-10 19:38:44 +08:00
|
|
|
return Generator::namespace($namespace)
|
|
|
|
->class($modelName, function (Class_ $class, Generator $generator) use ($table){
|
|
|
|
$class->extend('Model');
|
|
|
|
|
|
|
|
$class->setDocComment($this->buildClassComment($table));
|
|
|
|
|
|
|
|
$generator->property('name', function (Property $property) use ($table){
|
|
|
|
return $property->setDefault(Utils::tableWithoutPrefix($table));
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($this->hasTableExists($table)) {
|
|
|
|
$generator->property('field', function (Property $property) use ($table){
|
|
|
|
return $property->setDefault(array_column(Db::getFields($table), 'name'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
->uses([
|
|
|
|
$softDelete ? 'catcher\base\CatchModel as Model' : 'think\Model'
|
|
|
|
])
|
|
|
|
->when(! $softDelete, function (Generator $generator){
|
|
|
|
$generator->traits([
|
|
|
|
BaseOptionsTrait::class,
|
|
|
|
ScopeTrait::class,
|
|
|
|
]);
|
|
|
|
})
|
|
|
|
->print();
|
|
|
|
/**
|
2020-11-19 17:31:31 +08:00
|
|
|
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));
|
|
|
|
})
|
2021-04-27 18:33:59 +08:00
|
|
|
->class((new Classes($modelName))
|
|
|
|
->extend('Model')
|
|
|
|
->docComment($this->buildClassComment($table)),
|
2020-11-19 17:31:31 +08:00
|
|
|
function (Classes $class) use ($softDelete, $table) {
|
|
|
|
if (!$softDelete) {
|
|
|
|
$class->addTrait(
|
|
|
|
(new Traits())->use('BaseOptionsTrait', 'ScopeTrait')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$class->addProperty(
|
2020-12-01 17:55:36 +08:00
|
|
|
(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))
|
2020-12-01 17:58:53 +08:00
|
|
|
)->docComment('// 数据库字段映射'));
|
2020-11-19 19:11:18 +08:00
|
|
|
});
|
2021-06-10 19:38:44 +08:00
|
|
|
})->getContent();*/
|
2020-04-28 22:02:03 +08:00
|
|
|
}
|
2021-04-27 18:33:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 提供模型字段属性提示
|
|
|
|
*
|
|
|
|
* @time 2021年04月27日
|
|
|
|
* @param $table
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function buildClassComment($table): string
|
|
|
|
{
|
2021-05-03 10:54:37 +08:00
|
|
|
$fields = Db::name(Utils::tableWithoutPrefix($table))->getFieldsType();
|
2021-04-27 18:33:59 +08:00
|
|
|
|
|
|
|
$comment = '/**' . PHP_EOL . ' *' . PHP_EOL;
|
|
|
|
|
|
|
|
foreach ($fields as $field => $type) {
|
|
|
|
$comment .= sprintf(' * @property %s $%s', $type, $field) . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
$comment .= ' */';
|
|
|
|
|
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
}
|