updae:基于AST重构代码生成

This commit is contained in:
JaguarJack
2020-11-19 17:31:31 +08:00
parent e01790aa23
commit 5713d12ce1
13 changed files with 611 additions and 271 deletions

View File

@@ -2,30 +2,129 @@
namespace catcher\generate\build\traits;
class MethodReturn
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Return_;
trait CatchMethodReturn
{
public function index()
/**
* 列表
*
* @time 2020年11月18日
* @param $model
* @return $this
*/
public function index($model)
{
$class = new Name('CatchResponse');
$arg = new Arg(new MethodCall(
new PropertyFetch(
new Variable('this'), new Identifier($model)
),
new Identifier('getList')
));
$this->methodBuild->addStmt(new Return_(new StaticCall($class, 'paginate', [$arg])));
return $this;
}
public function save()
/**
* 保存
*
* @time 2020年11月18日
* @param $model
* @return $this
*/
public function save($model)
{
$arg = new Arg(new MethodCall(
new PropertyFetch(
new Variable('this'), new Identifier($model)
),
new Identifier('storeBy'), [new Arg(new MethodCall(new Variable('request'), new Identifier('post')))]
));
$class = new Name('CatchResponse');
$this->methodBuild->addStmt(new Return_(new StaticCall($class, 'success', [$arg])));
return $this;
}
public function update()
/**
* 更新
*
* @time 2020年11月18日
* @param $model
* @return $this
*/
public function update($model)
{
$arg = new Arg(new MethodCall(
new PropertyFetch(
new Variable('this'), new Identifier($model)
),
new Identifier('updateBy'), [
new Arg(new Variable('id')),
new Arg(new MethodCall(new Variable('request'), new Identifier('post')))
]
));
$class = new Name('CatchResponse');
$this->methodBuild->addStmt(new Return_(new StaticCall($class, 'success', [$arg])));
return $this;
}
public function read()
public function read($model)
{
$arg = new Arg(new MethodCall(
new PropertyFetch(
new Variable('this'), new Identifier($model)
),
new Identifier('findBy'), [
new Arg(new Variable('id'))
]
));
$class = new Name('CatchResponse');
$this->methodBuild->addStmt(new Return_(new StaticCall($class, 'success', [$arg])));
return $this;
}
public function delete()
/**
* 删除
*
* @time 2020年11月18日
* @param $model
* @return $this
*/
public function delete($model)
{
$arg = new Arg(new MethodCall(
new PropertyFetch(
new Variable('this'), new Identifier($model)
),
new Identifier('deleteBy'), [
new Arg(new Variable('id'))
]
));
$class = new Name('CatchResponse');
$this->methodBuild->addStmt(new Return_(new StaticCall($class, 'success', [$arg])));
return $this;
}
}