2020-04-28 16:34:17 +08:00
|
|
|
|
<?php
|
2020-04-28 22:02:03 +08:00
|
|
|
|
namespace catcher\generate\factory;
|
2020-04-28 16:34:17 +08:00
|
|
|
|
|
2021-06-10 19:38:44 +08:00
|
|
|
|
use catcher\base\CatchController;
|
|
|
|
|
use catcher\CatchResponse;
|
2020-04-28 16:34:17 +08:00
|
|
|
|
use catcher\exceptions\FailedException;
|
2020-11-19 17:31:31 +08:00
|
|
|
|
use catcher\facade\FileSystem;
|
2021-06-10 19:38:44 +08:00
|
|
|
|
use JaguarJack\Generate\Build\Class_;
|
|
|
|
|
use JaguarJack\Generate\Build\ClassMethod;
|
|
|
|
|
use JaguarJack\Generate\Build\Params;
|
|
|
|
|
use JaguarJack\Generate\Define;
|
|
|
|
|
use JaguarJack\Generate\Generator;
|
2020-04-28 16:34:17 +08:00
|
|
|
|
use think\helper\Str;
|
2021-06-10 19:38:44 +08:00
|
|
|
|
use think\Response;
|
2020-04-28 16:34:17 +08:00
|
|
|
|
|
|
|
|
|
class Controller extends Factory
|
|
|
|
|
{
|
|
|
|
|
protected $methods = [];
|
|
|
|
|
|
2020-11-19 17:31:31 +08:00
|
|
|
|
protected $uses = [
|
|
|
|
|
'catcher\base\CatchRequest as Request',
|
2021-06-10 19:38:44 +08:00
|
|
|
|
CatchResponse::class,
|
|
|
|
|
CatchController::class,
|
2020-11-19 17:31:31 +08:00
|
|
|
|
];
|
|
|
|
|
|
2020-04-28 16:34:17 +08:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年04月27日
|
2021-06-10 19:38:44 +08:00
|
|
|
|
* @param array $params
|
2020-04-28 16:34:17 +08:00
|
|
|
|
* @return bool|string|string[]
|
|
|
|
|
*/
|
2021-03-14 07:41:15 +08:00
|
|
|
|
public function done(array $params)
|
2020-04-28 22:02:03 +08:00
|
|
|
|
{
|
|
|
|
|
// 写入成功之后
|
2020-07-11 10:59:57 +08:00
|
|
|
|
$controllerPath = $this->getGeneratePath($params['controller']);
|
2020-11-19 17:31:31 +08:00
|
|
|
|
|
|
|
|
|
if (FileSystem::put($controllerPath, $this->getContent($params))) {
|
|
|
|
|
return $controllerPath;
|
2020-04-28 22:02:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new FailedException($params['controller'] . ' generate failed~');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取内容
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年04月28日
|
|
|
|
|
* @param $params
|
|
|
|
|
* @return bool|string|string[]
|
|
|
|
|
*/
|
2020-04-29 11:41:54 +08:00
|
|
|
|
public function getContent($params)
|
2020-04-28 16:34:17 +08:00
|
|
|
|
{
|
|
|
|
|
if (!$params['controller']) {
|
2020-04-28 22:02:03 +08:00
|
|
|
|
throw new FailedException('params has lost~');
|
2020-04-28 16:34:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parse controller
|
|
|
|
|
[$className, $namespace] = $this->parseFilename($params['controller']);
|
2020-11-19 17:31:31 +08:00
|
|
|
|
|
2020-04-28 16:34:17 +08:00
|
|
|
|
[$model, $modelNamespace] = $this->parseFilename($params['model']);
|
|
|
|
|
|
2021-06-10 19:38:44 +08:00
|
|
|
|
$asModel = lcfirst(Str::contains($model, 'Model') ? : $model . 'Model');
|
2020-04-28 16:34:17 +08:00
|
|
|
|
|
2021-06-10 19:38:44 +08:00
|
|
|
|
if (! $className) {
|
2020-11-19 17:31:31 +08:00
|
|
|
|
throw new FailedException('未填写控制器名称');
|
|
|
|
|
}
|
2020-04-28 16:34:17 +08:00
|
|
|
|
|
2021-06-10 19:38:44 +08:00
|
|
|
|
$this->uses[] = sprintf('%s as %s', $modelNamespace . '\\' . ucfirst($model), ucfirst($asModel));
|
|
|
|
|
$this->uses[] = Response::class;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$content = Generator::namespace($namespace)
|
|
|
|
|
->class($className, function (Class_ $class, Generator $generator) use ($model, $asModel) {
|
|
|
|
|
$class->extend('CatchController');
|
|
|
|
|
|
|
|
|
|
$generator->property($asModel, function ($property) {
|
|
|
|
|
return $property->makeProtected();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// construct 方法
|
|
|
|
|
$generator->method('__construct', function (ClassMethod $method) use ($model, $asModel) {
|
|
|
|
|
return $method->addParam(
|
|
|
|
|
Params::name($asModel, ucfirst($asModel))
|
|
|
|
|
)->body([
|
|
|
|
|
Define::variable(Define::propertyDefineIdentifier($asModel), Define::variable($asModel))
|
|
|
|
|
])->makePublic();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// index 方法
|
|
|
|
|
$generator->method('index', function (ClassMethod $method, Generator $generator) use ($asModel) {
|
|
|
|
|
return $method->body([
|
|
|
|
|
$generator->call('paginate', [
|
|
|
|
|
$generator->methodCall([Define::propertyDefineIdentifier($asModel), 'getList'], [])
|
|
|
|
|
], 'CatchResponse')->call()
|
|
|
|
|
])->makePublic()->return()->setReturnType('Response');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// save 方法
|
|
|
|
|
$generator->method('save', function (ClassMethod $method, Generator $generator) use ($asModel) {
|
|
|
|
|
return $method
|
|
|
|
|
->addParam([
|
|
|
|
|
Params::name('request')->setType('Request')
|
|
|
|
|
])
|
|
|
|
|
->body([
|
|
|
|
|
$generator->call('success', [
|
|
|
|
|
$generator->methodCall([Define::propertyDefineIdentifier($asModel), 'storeBy'], [
|
|
|
|
|
$generator->methodCall(['request', 'post'], [])
|
|
|
|
|
])
|
|
|
|
|
], 'CatchResponse')->call()
|
|
|
|
|
])->makePublic()->return('Response');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// read 方法
|
|
|
|
|
$generator->method('read', function (ClassMethod $method, Generator $generator) use ($asModel) {
|
|
|
|
|
return $method
|
|
|
|
|
->addParam([
|
|
|
|
|
Params::name('id'),
|
|
|
|
|
])
|
|
|
|
|
->body([
|
|
|
|
|
$generator->call('success', [
|
|
|
|
|
$generator->methodCall([Define::propertyDefineIdentifier($asModel), 'findBy'], [
|
|
|
|
|
Define::variable('id'),
|
|
|
|
|
])
|
|
|
|
|
], 'CatchResponse')->call()
|
|
|
|
|
])->makePublic()->return('Response');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// update 方法
|
|
|
|
|
$generator->method('update', function (ClassMethod $method, Generator $generator) use ($asModel) {
|
|
|
|
|
return $method
|
|
|
|
|
->addParam([
|
|
|
|
|
Params::name('id'),
|
|
|
|
|
Params::name('request')->setType('Request')
|
|
|
|
|
])
|
|
|
|
|
->body([
|
|
|
|
|
$generator->call('success', [
|
|
|
|
|
$generator->methodCall([Define::propertyDefineIdentifier($asModel), 'updateBy'], [
|
|
|
|
|
Define::variable('id'),
|
|
|
|
|
$generator->methodCall(['request', 'post'], [])
|
|
|
|
|
])
|
|
|
|
|
], 'CatchResponse')->call()
|
|
|
|
|
])->makePublic()->return('Response');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// read 方法
|
|
|
|
|
$generator->method('delete', function (ClassMethod $method, Generator $generator) use ($asModel) {
|
|
|
|
|
return $method
|
|
|
|
|
->addParam([
|
|
|
|
|
Params::name('id'),
|
|
|
|
|
])
|
|
|
|
|
->body([
|
|
|
|
|
$generator->call('success', [
|
|
|
|
|
$generator->methodCall([Define::propertyDefineIdentifier($asModel), 'deleteBy'], [
|
|
|
|
|
Define::variable('id'),
|
|
|
|
|
])
|
|
|
|
|
], 'CatchResponse')->call()
|
|
|
|
|
])->makePublic()->return('Response');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
})->uses($this->uses)->print();
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
throw new FailedException($e->getMessage());
|
|
|
|
|
}
|
2020-04-28 22:02:03 +08:00
|
|
|
|
|
2021-06-10 19:38:44 +08:00
|
|
|
|
return $content;
|
2020-04-28 16:34:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|