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
|
|
|
|
|
2020-11-19 17:31:31 +08:00
|
|
|
|
use catcher\CatchAdmin;
|
2020-04-28 16:34:17 +08:00
|
|
|
|
use catcher\exceptions\FailedException;
|
2020-11-19 17:31:31 +08:00
|
|
|
|
use catcher\facade\FileSystem;
|
|
|
|
|
use catcher\generate\build\classes\Methods;
|
|
|
|
|
use catcher\generate\build\CatchBuild;
|
|
|
|
|
use catcher\generate\build\classes\Classes;
|
|
|
|
|
use catcher\generate\build\classes\Property;
|
|
|
|
|
use catcher\generate\build\classes\Uses;
|
|
|
|
|
use PhpParser\BuilderFactory;
|
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
|
use PhpParser\Node\Expr\ClosureUse;
|
|
|
|
|
use PhpParser\PrettyPrinter\Standard;
|
2020-04-28 16:34:17 +08:00
|
|
|
|
use think\helper\Str;
|
2020-11-19 17:31:31 +08:00
|
|
|
|
use PhpParser\Error;
|
|
|
|
|
use PhpParser\NodeDumper;
|
|
|
|
|
use PhpParser\ParserFactory;
|
|
|
|
|
use PhpParser\PrettyPrinter;
|
|
|
|
|
use PhpParser\Node;
|
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',
|
|
|
|
|
'catcher\CatchResponse',
|
|
|
|
|
'catcher\base\CatchController'
|
|
|
|
|
];
|
|
|
|
|
|
2020-04-28 16:34:17 +08:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @time 2020年04月27日
|
|
|
|
|
* @param $params
|
|
|
|
|
* @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']);
|
|
|
|
|
|
2020-11-19 17:31:31 +08:00
|
|
|
|
$asModel = lcfirst(Str::contains($model, 'Model') ? : $model . 'Model');
|
2020-04-28 16:34:17 +08:00
|
|
|
|
|
2020-11-19 17:31:31 +08:00
|
|
|
|
if (!$className) {
|
|
|
|
|
throw new FailedException('未填写控制器名称');
|
|
|
|
|
}
|
2020-04-28 16:34:17 +08:00
|
|
|
|
|
2020-11-19 17:31:31 +08:00
|
|
|
|
$use = new Uses();
|
|
|
|
|
$class = new Classes($className);
|
|
|
|
|
|
|
|
|
|
return (new CatchBuild())->namespace($namespace)
|
|
|
|
|
->use($use->name('catcher\base\CatchRequest', 'Request'))
|
|
|
|
|
->use($use->name('catcher\CatchResponse'))
|
|
|
|
|
->use($use->name('catcher\base\CatchController'))
|
|
|
|
|
->use($use->name($modelNamespace . '\\' . ucfirst($model), $asModel))
|
|
|
|
|
->class($class->extend('CatchController')->docComment(), function (Classes $class) use ($asModel) {
|
|
|
|
|
foreach ($this->getMethods($asModel) as $method) {
|
|
|
|
|
$class->addMethod($method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$class->addProperty(
|
2020-11-19 17:44:11 +08:00
|
|
|
|
(new Property($asModel))->protected()
|
2020-11-19 17:31:31 +08:00
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
->getContent();
|
2020-04-28 16:34:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 22:02:03 +08:00
|
|
|
|
|
2020-04-28 16:34:17 +08:00
|
|
|
|
/**
|
2020-11-19 17:31:31 +08:00
|
|
|
|
* 方法集合
|
2020-04-28 16:34:17 +08:00
|
|
|
|
*
|
2020-11-19 17:31:31 +08:00
|
|
|
|
* @time 2020年11月19日
|
|
|
|
|
* @param $model
|
|
|
|
|
* @return array
|
2020-04-28 16:34:17 +08:00
|
|
|
|
*/
|
2020-11-19 17:31:31 +08:00
|
|
|
|
protected function getMethods($model)
|
2020-04-28 16:34:17 +08:00
|
|
|
|
{
|
2020-11-19 17:31:31 +08:00
|
|
|
|
$date = date('Y年m月d日 H:i');
|
2020-04-28 16:34:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
2020-11-19 17:31:31 +08:00
|
|
|
|
(new Methods('__construct'))
|
|
|
|
|
->public()
|
|
|
|
|
->param($model, ucfirst($model))
|
|
|
|
|
->docComment("\r\n")
|
|
|
|
|
->declare($model, $model),
|
|
|
|
|
|
|
|
|
|
(new Methods('index'))->public()
|
|
|
|
|
->param('request', 'Request')
|
|
|
|
|
->docComment(
|
|
|
|
|
<<<TEXT
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 列表
|
|
|
|
|
* @time $date
|
|
|
|
|
* @param Request \$request
|
|
|
|
|
*/
|
|
|
|
|
TEXT
|
|
|
|
|
)
|
|
|
|
|
->returnType('\think\Response')->index($model),
|
|
|
|
|
|
|
|
|
|
(new Methods('save'))
|
|
|
|
|
->public()
|
|
|
|
|
->param('request', 'Request')
|
|
|
|
|
->docComment(
|
|
|
|
|
<<<TEXT
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 保存信息
|
|
|
|
|
* @time $date
|
|
|
|
|
* @param Request \$request
|
|
|
|
|
*/
|
|
|
|
|
TEXT
|
|
|
|
|
)
|
|
|
|
|
->returnType('\think\Response')
|
|
|
|
|
->save($model),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(new Methods('read'))->public()
|
|
|
|
|
->param('id')
|
|
|
|
|
->docComment(
|
|
|
|
|
<<<TEXT
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 读取
|
|
|
|
|
* @time $date
|
|
|
|
|
* @param \$id
|
|
|
|
|
*/
|
|
|
|
|
TEXT
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
->returnType('\think\Response')->read($model),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(new Methods('update'))->public()
|
|
|
|
|
->param('request', 'Request')
|
|
|
|
|
->param('id')
|
|
|
|
|
->docComment(
|
|
|
|
|
<<<TEXT
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新
|
|
|
|
|
* @time $date
|
|
|
|
|
* @param Request \$request
|
|
|
|
|
* @param \$id
|
|
|
|
|
*/
|
|
|
|
|
TEXT
|
|
|
|
|
)
|
|
|
|
|
->returnType('\think\Response')->update($model),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(new Methods('delete'))->public()
|
|
|
|
|
->param('id')
|
|
|
|
|
->docComment(
|
|
|
|
|
<<<TEXT
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除
|
|
|
|
|
* @time $date
|
|
|
|
|
* @param \$id
|
|
|
|
|
*/
|
|
|
|
|
TEXT
|
|
|
|
|
)
|
|
|
|
|
->returnType('\think\Response')->delete($model),
|
|
|
|
|
|
2020-04-28 16:34:17 +08:00
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|