Skip to content

Commit da8dabd

Browse files
Add new inertia controller statement (#718)
1 parent e050c91 commit da8dabd

File tree

7 files changed

+123
-7
lines changed

7 files changed

+123
-7
lines changed

src/Generators/ControllerGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Blueprint\Models\Statements\DispatchStatement;
1111
use Blueprint\Models\Statements\EloquentStatement;
1212
use Blueprint\Models\Statements\FireStatement;
13+
use Blueprint\Models\Statements\InertiaStatement;
1314
use Blueprint\Models\Statements\QueryStatement;
1415
use Blueprint\Models\Statements\RedirectStatement;
1516
use Blueprint\Models\Statements\RenderStatement;
@@ -174,6 +175,9 @@ protected function buildMethods(Controller $controller): string
174175
} elseif ($statement instanceof QueryStatement) {
175176
$body .= self::INDENT . $statement->output($controller->prefix()) . PHP_EOL;
176177
$this->addImport($controller, $this->determineModel($controller, $statement->model()));
178+
} elseif ($statement instanceof InertiaStatement) {
179+
$body .= self::INDENT . $statement->output() . PHP_EOL;
180+
$this->addImport($controller, 'Inertia\Inertia');
177181
}
178182

179183
$body .= PHP_EOL;
@@ -187,6 +191,7 @@ protected function buildMethods(Controller $controller): string
187191
$method = str_replace('): Response' . PHP_EOL, ')' . PHP_EOL, $method);
188192
} else {
189193
$returnType = match (true) {
194+
$statement instanceof InertiaStatement => 'Inertia\Response',
190195
$statement instanceof RenderStatement => 'Illuminate\View\View',
191196
$statement instanceof RedirectStatement => 'Illuminate\Http\RedirectResponse',
192197
$statement instanceof ResourceStatement => config('blueprint.namespace') . '\\Http\\Resources\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $statement->name(),

src/Lexers/StatementLexer.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Blueprint\Models\Statements\DispatchStatement;
77
use Blueprint\Models\Statements\EloquentStatement;
88
use Blueprint\Models\Statements\FireStatement;
9+
use Blueprint\Models\Statements\InertiaStatement;
910
use Blueprint\Models\Statements\QueryStatement;
1011
use Blueprint\Models\Statements\RedirectStatement;
1112
use Blueprint\Models\Statements\RenderStatement;
@@ -24,26 +25,34 @@ public function analyze(array $tokens): array
2425

2526
foreach ($tokens as $command => $statement) {
2627
$statements[] = match ($command) {
27-
'query' => $this->analyzeQuery($statement),
28-
'render' => $this->analyzeRender($statement),
29-
'fire' => $this->analyzeEvent($statement),
3028
'dispatch' => $this->analyzeDispatch($statement),
31-
'send' => $this->analyzeSend($statement),
29+
'fire' => $this->analyzeEvent($statement),
30+
'flash', 'store' => new SessionStatement($command, $statement),
31+
'inertia' => $this->analyzeInertia($statement),
3232
'notify' => $this->analyzeNotify($statement),
33-
'validate' => $this->analyzeValidate($statement),
33+
'query' => $this->analyzeQuery($statement),
3434
'redirect' => $this->analyzeRedirect($statement),
35-
'respond' => $this->analyzeRespond($statement),
35+
'render' => $this->analyzeRender($statement),
3636
'resource' => $this->analyzeResource($statement),
37+
'respond' => $this->analyzeRespond($statement),
3738
'save', 'delete', 'find' => new EloquentStatement($command, $statement),
39+
'send' => $this->analyzeSend($statement),
3840
'update' => $this->analyzeUpdate($statement),
39-
'flash', 'store' => new SessionStatement($command, $statement),
41+
'validate' => $this->analyzeValidate($statement),
4042
default => $this->analyzeDefault($command, $statement),
4143
};
4244
}
4345

4446
return array_filter($statements);
4547
}
4648

49+
private function analyzeInertia(string $statement): InertiaStatement
50+
{
51+
[$view, $data] = $this->parseWithStatement($statement);
52+
53+
return new InertiaStatement($view, $data);
54+
}
55+
4756
private function analyzeRender(string $statement): RenderStatement
4857
{
4958
[$view, $data] = $this->parseWithStatement($statement);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Blueprint\Models\Statements;
4+
5+
use Blueprint\Concerns\HasParameters;
6+
7+
class InertiaStatement
8+
{
9+
use HasParameters;
10+
11+
private string $view;
12+
13+
public function __construct(string $view, array $data = [])
14+
{
15+
$this->view = $view;
16+
$this->data = $data;
17+
}
18+
19+
public function output(): string
20+
{
21+
$code = "return Inertia::render('" . $this->view() . "'";
22+
23+
if ($this->data()) {
24+
$code .= ', ' . $this->buildParameters();
25+
}
26+
27+
$code .= ');';
28+
29+
return $code;
30+
}
31+
32+
public function view(): string
33+
{
34+
return $this->view;
35+
}
36+
37+
private function buildParameters(): string
38+
{
39+
$parameters = array_map(
40+
fn ($parameter) => sprintf(
41+
"%s'%s' => \$%s%s,%s",
42+
str_pad(' ', 12),
43+
$parameter,
44+
in_array($parameter, $this->properties()) ? 'this->' : '',
45+
$parameter,
46+
PHP_EOL
47+
),
48+
$this->data()
49+
);
50+
51+
return sprintf(
52+
'[%s%s%s]',
53+
PHP_EOL,
54+
implode($parameters),
55+
str_pad(' ', 8)
56+
);
57+
}
58+
}

tests/Feature/Generators/ControllerGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ public static function controllerTreeDataProvider(): array
241241
['drafts/nested-components.yaml', 'app/Http/Controllers/Admin/UserController.php', 'controllers/nested-components.php'],
242242
['drafts/respond-statements.yaml', 'app/Http/Controllers/Api/PostController.php', 'controllers/respond-statements.php'],
243243
['drafts/resource-statements.yaml', 'app/Http/Controllers/UserController.php', 'controllers/resource-statements.php'],
244+
['drafts/inertia-render.yaml', 'app/Http/Controllers/CustomerController.php', 'controllers/inertia-render.php'],
244245
['drafts/save-without-validation.yaml', 'app/Http/Controllers/PostController.php', 'controllers/save-without-validation.php'],
245246
['drafts/api-resource-pagination.yaml', 'app/Http/Controllers/PostController.php', 'controllers/api-resource-pagination.php'],
246247
['drafts/api-routes-example.yaml', 'app/Http/Controllers/Api/CertificateController.php', 'controllers/api-routes-example.php'],

tests/Feature/Lexers/StatementLexerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Blueprint\Models\Statements\DispatchStatement;
77
use Blueprint\Models\Statements\EloquentStatement;
88
use Blueprint\Models\Statements\FireStatement;
9+
use Blueprint\Models\Statements\InertiaStatement;
910
use Blueprint\Models\Statements\QueryStatement;
1011
use Blueprint\Models\Statements\RedirectStatement;
1112
use Blueprint\Models\Statements\RenderStatement;
@@ -73,6 +74,22 @@ public function it_returns_a_render_statement_with_data(): void
7374
$this->assertEquals(['foo', 'bar', 'baz'], $actual[0]->data());
7475
}
7576

77+
#[Test]
78+
public function it_returns_an_inertia_statement_with_data(): void
79+
{
80+
$tokens = [
81+
'inertia' => 'post.index with:foo,bar,baz',
82+
];
83+
84+
$actual = $this->subject->analyze($tokens);
85+
86+
$this->assertCount(1, $actual);
87+
$this->assertInstanceOf(InertiaStatement::class, $actual[0]);
88+
89+
$this->assertEquals('post.index', $actual[0]->view());
90+
$this->assertEquals(['foo', 'bar', 'baz'], $actual[0]->data());
91+
}
92+
7693
#[Test]
7794
public function it_returns_an_event_statement(): void
7895
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Customer;
6+
use Illuminate\Http\Request;
7+
use Inertia\Inertia;
8+
use Inertia\Response;
9+
10+
class CustomerController extends Controller
11+
{
12+
public function show(Request $request, Customer $customer): Response
13+
{
14+
$customers = Customer::all();
15+
16+
return Inertia::render('customer.show', [
17+
'customer' => $customer,
18+
'customers' => $customers,
19+
]);
20+
}
21+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
controllers:
2+
Customer:
3+
show:
4+
query: all
5+
inertia: customer.show with:customer,customers

0 commit comments

Comments
 (0)