update 模版生成
This commit is contained in:
@@ -1,24 +1,17 @@
|
||||
<?php
|
||||
namespace catcher\generate\factory;
|
||||
|
||||
use catcher\CatchAdmin;
|
||||
use catcher\base\CatchController;
|
||||
use catcher\CatchResponse;
|
||||
use catcher\exceptions\FailedException;
|
||||
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;
|
||||
use JaguarJack\Generate\Build\Class_;
|
||||
use JaguarJack\Generate\Build\ClassMethod;
|
||||
use JaguarJack\Generate\Build\Params;
|
||||
use JaguarJack\Generate\Define;
|
||||
use JaguarJack\Generate\Generator;
|
||||
use think\helper\Str;
|
||||
use PhpParser\Error;
|
||||
use PhpParser\NodeDumper;
|
||||
use PhpParser\ParserFactory;
|
||||
use PhpParser\PrettyPrinter;
|
||||
use PhpParser\Node;
|
||||
use think\Response;
|
||||
|
||||
class Controller extends Factory
|
||||
{
|
||||
@@ -26,14 +19,14 @@ class Controller extends Factory
|
||||
|
||||
protected $uses = [
|
||||
'catcher\base\CatchRequest as Request',
|
||||
'catcher\CatchResponse',
|
||||
'catcher\base\CatchController'
|
||||
CatchResponse::class,
|
||||
CatchController::class,
|
||||
];
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @param $params
|
||||
* @param array $params
|
||||
* @return bool|string|string[]
|
||||
*/
|
||||
public function done(array $params)
|
||||
@@ -66,130 +59,111 @@ class Controller extends Factory
|
||||
|
||||
[$model, $modelNamespace] = $this->parseFilename($params['model']);
|
||||
|
||||
$asModel = ucfirst(Str::contains($model, 'Model') ? : $model . 'Model');
|
||||
$asModel = lcfirst(Str::contains($model, 'Model') ? : $model . 'Model');
|
||||
|
||||
if (!$className) {
|
||||
if (! $className) {
|
||||
throw new FailedException('未填写控制器名称');
|
||||
}
|
||||
|
||||
$use = new Uses();
|
||||
$class = new Classes($className);
|
||||
$this->uses[] = sprintf('%s as %s', $modelNamespace . '\\' . ucfirst($model), ucfirst($asModel));
|
||||
$this->uses[] = Response::class;
|
||||
|
||||
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);
|
||||
}
|
||||
try {
|
||||
$content = Generator::namespace($namespace)
|
||||
->class($className, function (Class_ $class, Generator $generator) use ($model, $asModel) {
|
||||
$class->extend('CatchController');
|
||||
|
||||
$class->addProperty(
|
||||
(new Property($asModel))->protected()
|
||||
);
|
||||
})
|
||||
->getContent();
|
||||
}
|
||||
$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');
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* 方法集合
|
||||
*
|
||||
* @time 2020年11月19日
|
||||
* @param $model
|
||||
* @return array
|
||||
*/
|
||||
protected function getMethods($model)
|
||||
{
|
||||
$date = date('Y年m月d日 H:i');
|
||||
// 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');
|
||||
});
|
||||
|
||||
|
||||
return [
|
||||
(new Methods('__construct'))
|
||||
->public()
|
||||
->param($model, ucfirst($model))
|
||||
->docComment("\r\n")
|
||||
->declare($model, $model),
|
||||
// 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');
|
||||
});
|
||||
|
||||
(new Methods('index'))->public()
|
||||
->param('request', 'Request')
|
||||
->docComment(
|
||||
<<<TEXT
|
||||
// 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');
|
||||
});
|
||||
|
||||
/**
|
||||
* 列表
|
||||
* @time $date
|
||||
* @param Request \$request
|
||||
*/
|
||||
TEXT
|
||||
)
|
||||
->returnType('\think\Response')->index($model),
|
||||
})->uses($this->uses)->print();
|
||||
} catch (\Exception $e) {
|
||||
throw new FailedException($e->getMessage());
|
||||
}
|
||||
|
||||
(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),
|
||||
|
||||
];
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
23
extend/catcher/generate/factory/HeaderDoc.php
Normal file
23
extend/catcher/generate/factory/HeaderDoc.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace catcher\generate\factory;
|
||||
|
||||
trait HeaderDoc
|
||||
{
|
||||
public function header(): string
|
||||
{
|
||||
$year = date('Y', time());
|
||||
|
||||
return <<<TMP
|
||||
// +----------------------------------------------------------------------
|
||||
// | CatchAdmin [Just Like ~ ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~{$year} http://catchadmin.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
TMP;
|
||||
}
|
||||
}
|
@@ -3,15 +3,12 @@ namespace catcher\generate\factory;
|
||||
|
||||
use catcher\exceptions\FailedException;
|
||||
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;
|
||||
use JaguarJack\Generate\Build\Class_;
|
||||
use JaguarJack\Generate\Build\Property;
|
||||
use JaguarJack\Generate\Generator;
|
||||
use think\facade\Db;
|
||||
use think\helper\Str;
|
||||
|
||||
@@ -45,6 +42,7 @@ class Model extends Factory
|
||||
* @time 2020年04月29日
|
||||
* @param $params
|
||||
* @return string|string[]
|
||||
* @throws \JaguarJack\Generate\Exceptions\TypeNotFoundException
|
||||
*/
|
||||
public function getContent($params)
|
||||
{
|
||||
@@ -66,6 +64,33 @@ class Model extends Factory
|
||||
|
||||
$softDelete = $extra['soft_delete'];
|
||||
|
||||
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();
|
||||
/**
|
||||
return (new CatchBuild)->namespace($namespace)
|
||||
->use((new Uses())->name('catcher\base\CatchModel', 'Model'))
|
||||
->when(!$softDelete, function (CatchBuild $build){
|
||||
@@ -94,7 +119,7 @@ class Model extends Factory
|
||||
(new Arr)->build(Db::getFields($table))
|
||||
)->docComment('// 数据库字段映射'));
|
||||
});
|
||||
})->getContent();
|
||||
})->getContent();*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -2,11 +2,14 @@
|
||||
namespace catcher\generate\factory;
|
||||
|
||||
use catcher\facade\FileSystem;
|
||||
use catcher\generate\template\Content;
|
||||
use JaguarJack\Generate\Build\Value;
|
||||
use JaguarJack\Generate\Define;
|
||||
use PhpParser\ParserFactory;
|
||||
use JaguarJack\Generate\Generator;
|
||||
|
||||
class Route extends Factory
|
||||
{
|
||||
use Content;
|
||||
use HeaderDoc;
|
||||
|
||||
protected $controllerName;
|
||||
|
||||
@@ -14,68 +17,111 @@ class Route extends Factory
|
||||
|
||||
protected $restful;
|
||||
|
||||
protected $module;
|
||||
|
||||
protected $methods = [];
|
||||
|
||||
const VARIABLE_ST = 'scapegoat';
|
||||
|
||||
/**
|
||||
* 实现
|
||||
*
|
||||
* @time 2021年06月09日
|
||||
* @param array $params
|
||||
* @throws \Exception
|
||||
* @return mixed
|
||||
*/
|
||||
public function done(array $params = [])
|
||||
{
|
||||
$route = [];
|
||||
$router = $this->getModulePath($this->controller) . DIRECTORY_SEPARATOR . 'route.php';
|
||||
|
||||
$content = $this->generateRoute($router);
|
||||
|
||||
$content = '<?php' . PHP_EOL .
|
||||
trim(str_replace(['$scapegoat', '='], ['', ''], $content), ';') . ';';
|
||||
|
||||
if (! file_exists($router)) {
|
||||
return FileSystem::put($router, $content);
|
||||
}
|
||||
|
||||
return FileSystem::put($router, $content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* parse router methods
|
||||
*
|
||||
* @time 2021年06月09日
|
||||
* @return array
|
||||
* @throws \JaguarJack\Generate\Exceptions\TypeNotFoundException
|
||||
*/
|
||||
protected function parseRouteMethods(): array
|
||||
{
|
||||
$generate = new Generator();
|
||||
|
||||
$stmts = [];
|
||||
|
||||
if ($this->restful) {
|
||||
$route[] = sprintf("\$router->resource('%s', '\%s');", $this->controllerName, $this->controller);
|
||||
$stmts[] = Define::variable(self::VARIABLE_ST, $generate->methodCall(['router', 'resource'], [
|
||||
Value::fetch($this->controllerName),
|
||||
Value::fetch(Define::classConstFetch($this->controller))
|
||||
]), sprintf('// %s 路由', $this->controllerName));
|
||||
}
|
||||
|
||||
if (!empty($this->methods)) {
|
||||
foreach ($this->methods as $method) {
|
||||
$route[] = sprintf("\$router->%s('%s/%s', '\%s@%s');", $method[1], $this->controllerName, $method[0], $this->controller, $method[0] );
|
||||
$stmts[] = Define::variable(self::VARIABLE_ST,
|
||||
$generate->methodCall(['router', $method[1]], [
|
||||
Value::fetch(sprintf('%s/%s', $this->controllerName, $method[0])),
|
||||
Value::fetch(sprintf('%s@%s', $this->controller, $method[0]))
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
$router = $this->getModulePath($this->controller) . DIRECTORY_SEPARATOR . 'route.php';
|
||||
|
||||
$comment = '// ' . $this->controllerName . '路由';
|
||||
|
||||
array_unshift($route, $comment);
|
||||
|
||||
if (file_exists($router)) {
|
||||
return FileSystem::put($router, $this->parseRoute($router, $route));
|
||||
}
|
||||
|
||||
return FileSystem::put($router, $this->header() . $comment. implode(';'. PHP_EOL , $route) . ';');
|
||||
return $stmts;
|
||||
}
|
||||
|
||||
protected function parseRoute($path, $route)
|
||||
/**
|
||||
* generate route
|
||||
*
|
||||
* @time 2021年06月09日
|
||||
* @param string $router
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function generateRoute(string $router): string
|
||||
{
|
||||
$file = new \SplFileObject($path);
|
||||
// 保留所有行
|
||||
$lines = [];
|
||||
// 结尾之后的数据
|
||||
$down = [];
|
||||
// 结尾数据
|
||||
$end = '';
|
||||
while (!$file->eof()) {
|
||||
$lines[] = rtrim($file->current(), PHP_EOL);
|
||||
$file->next();
|
||||
$generate = new Generator();
|
||||
|
||||
if (! FileSystem::exists($router)) {
|
||||
$stmts = $this->parseRouteMethods();
|
||||
|
||||
$expr = Define::variable(self::VARIABLE_ST, $generate->call(
|
||||
$generate->methodCall(['router', 'group'], [
|
||||
Value::fetch($this->module),
|
||||
$generate->closure()->uses('router')->body($stmts)
|
||||
]))
|
||||
->call('middleware', [Value::fetch('auth')])
|
||||
->call(), $this->header() . PHP_EOL . '/* @var \think\Route $router */');
|
||||
|
||||
return $generate->getContent([$expr]);
|
||||
} else {
|
||||
$factory = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
||||
|
||||
$ast = $factory->parse(file_get_contents($router));
|
||||
|
||||
$expression = $ast[0];
|
||||
|
||||
$stmts = $expression->expr->var->args[1]->value->stmts;
|
||||
|
||||
$stmts = array_merge($stmts, $this->parseRouteMethods());
|
||||
|
||||
$expression->expr->var->args[1]->value->stmts = $stmts;
|
||||
|
||||
$ast[0] = $expression;
|
||||
|
||||
return $generate->getContent($ast);
|
||||
}
|
||||
|
||||
while (count($lines)) {
|
||||
$line = array_pop($lines);
|
||||
if (strpos($line, '})') !== false) {
|
||||
$end = $line;
|
||||
break;
|
||||
}
|
||||
array_unshift($down, $line);
|
||||
}
|
||||
|
||||
$router = implode(PHP_EOL, $lines) . PHP_EOL;
|
||||
|
||||
$routes = array_merge($down, $route);
|
||||
|
||||
foreach ($routes as $r) {
|
||||
if ($r) {
|
||||
$router .= "\t" . $r . PHP_EOL;
|
||||
}
|
||||
}
|
||||
return $router .= $end;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,7 +131,7 @@ class Route extends Factory
|
||||
* @param $class
|
||||
* @return $this
|
||||
*/
|
||||
public function controller($class)
|
||||
public function controller($class): Route
|
||||
{
|
||||
$this->controller = $class;
|
||||
|
||||
@@ -93,6 +139,10 @@ class Route extends Factory
|
||||
|
||||
$this->controllerName = lcfirst(array_pop($class));
|
||||
|
||||
array_pop($class);
|
||||
|
||||
$this->module = array_pop($class);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -103,7 +153,7 @@ class Route extends Factory
|
||||
* @param $restful
|
||||
* @return $this
|
||||
*/
|
||||
public function restful($restful)
|
||||
public function restful($restful): Route
|
||||
{
|
||||
$this->restful = $restful;
|
||||
|
||||
@@ -117,7 +167,7 @@ class Route extends Factory
|
||||
* @param $methods
|
||||
* @return $this
|
||||
*/
|
||||
public function methods($methods)
|
||||
public function methods($methods): Route
|
||||
{
|
||||
$this->methods = $methods;
|
||||
|
||||
|
Reference in New Issue
Block a user