From 490a6b69718a4f48d15a884d991be160ef1f48c3 Mon Sep 17 00:00:00 2001 From: Anders Jurisoo Date: Wed, 25 May 2022 09:31:59 +0200 Subject: [PATCH] Allow passing a closure to insertStmt --- src/Support/AST/ASTQueryBuilder.php | 3 +++ tests/Unit/Support/AST/ASTQueryBuilderTest.php | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/Support/AST/ASTQueryBuilder.php b/src/Support/AST/ASTQueryBuilder.php index 7e900c4..1195e62 100644 --- a/src/Support/AST/ASTQueryBuilder.php +++ b/src/Support/AST/ASTQueryBuilder.php @@ -20,6 +20,7 @@ use Exception; use Illuminate\Support\Arr; use Illuminate\Support\Collection; +use PhpParser\BuilderFactory; use PhpParser\ConstExprEvaluator; class ASTQueryBuilder @@ -351,6 +352,8 @@ public function insertStmts($newNodes): self public function insertStmt($newNode): self { + if($newNode instanceof Closure) $newNode = $newNode(new BuilderFactory); + $this->currentNodes()->each(function ($node) use ($newNode) { $target = $node->result; diff --git a/tests/Unit/Support/AST/ASTQueryBuilderTest.php b/tests/Unit/Support/AST/ASTQueryBuilderTest.php index a36236b..7f3ba24 100644 --- a/tests/Unit/Support/AST/ASTQueryBuilderTest.php +++ b/tests/Unit/Support/AST/ASTQueryBuilderTest.php @@ -203,4 +203,16 @@ ->render(); assertEquals($original, $after); +}); + +it('can insert stmt with a closure', function() { + PHPFile::make()->class(\App\Dummy::class) + ->astQuery() + ->class() + ->insertStmt(function(BuilderFactory $builder) { + return $builder->property('someProperty')->getNode(); + }) + ->commit() + ->end() + ->preview(); }); \ No newline at end of file