Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new inertia controller statement #718

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Blueprint\Models\Statements\DispatchStatement;
use Blueprint\Models\Statements\EloquentStatement;
use Blueprint\Models\Statements\FireStatement;
use Blueprint\Models\Statements\InertiaStatement;
use Blueprint\Models\Statements\QueryStatement;
use Blueprint\Models\Statements\RedirectStatement;
use Blueprint\Models\Statements\RenderStatement;
Expand Down Expand Up @@ -174,6 +175,9 @@ protected function buildMethods(Controller $controller): string
} elseif ($statement instanceof QueryStatement) {
$body .= self::INDENT . $statement->output($controller->prefix()) . PHP_EOL;
$this->addImport($controller, $this->determineModel($controller, $statement->model()));
} elseif ($statement instanceof InertiaStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
$this->addImport($controller, 'Inertia\Inertia');
}

$body .= PHP_EOL;
Expand All @@ -187,6 +191,7 @@ protected function buildMethods(Controller $controller): string
$method = str_replace('): Response' . PHP_EOL, ')' . PHP_EOL, $method);
} else {
$returnType = match (true) {
$statement instanceof InertiaStatement => 'Inertia\Response',
$statement instanceof RenderStatement => 'Illuminate\View\View',
$statement instanceof RedirectStatement => 'Illuminate\Http\RedirectResponse',
$statement instanceof ResourceStatement => config('blueprint.namespace') . '\\Http\\Resources\\' . ($controller->namespace() ? $controller->namespace() . '\\' : '') . $statement->name(),
Expand Down
23 changes: 16 additions & 7 deletions src/Lexers/StatementLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Blueprint\Models\Statements\DispatchStatement;
use Blueprint\Models\Statements\EloquentStatement;
use Blueprint\Models\Statements\FireStatement;
use Blueprint\Models\Statements\InertiaStatement;
use Blueprint\Models\Statements\QueryStatement;
use Blueprint\Models\Statements\RedirectStatement;
use Blueprint\Models\Statements\RenderStatement;
Expand All @@ -24,26 +25,34 @@ public function analyze(array $tokens): array

foreach ($tokens as $command => $statement) {
$statements[] = match ($command) {
'query' => $this->analyzeQuery($statement),
'render' => $this->analyzeRender($statement),
'fire' => $this->analyzeEvent($statement),
'dispatch' => $this->analyzeDispatch($statement),
'send' => $this->analyzeSend($statement),
'fire' => $this->analyzeEvent($statement),
'flash', 'store' => new SessionStatement($command, $statement),
'inertia' => $this->analyzeInertia($statement),
'notify' => $this->analyzeNotify($statement),
'validate' => $this->analyzeValidate($statement),
'query' => $this->analyzeQuery($statement),
'redirect' => $this->analyzeRedirect($statement),
'respond' => $this->analyzeRespond($statement),
'render' => $this->analyzeRender($statement),
'resource' => $this->analyzeResource($statement),
'respond' => $this->analyzeRespond($statement),
'save', 'delete', 'find' => new EloquentStatement($command, $statement),
'send' => $this->analyzeSend($statement),
'update' => $this->analyzeUpdate($statement),
'flash', 'store' => new SessionStatement($command, $statement),
'validate' => $this->analyzeValidate($statement),
default => $this->analyzeDefault($command, $statement),
};
}

return array_filter($statements);
}

private function analyzeInertia(string $statement): InertiaStatement
{
[$view, $data] = $this->parseWithStatement($statement);

return new InertiaStatement($view, $data);
}

private function analyzeRender(string $statement): RenderStatement
{
[$view, $data] = $this->parseWithStatement($statement);
Expand Down
58 changes: 58 additions & 0 deletions src/Models/Statements/InertiaStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Blueprint\Models\Statements;

use Blueprint\Concerns\HasParameters;

class InertiaStatement
{
use HasParameters;

private string $view;

public function __construct(string $view, array $data = [])
{
$this->view = $view;
$this->data = $data;
}

public function output(): string
{
$code = "return Inertia::render('" . $this->view() . "'";

if ($this->data()) {
$code .= ', ' . $this->buildParameters();
}

$code .= ');';

return $code;
}

public function view(): string
{
return $this->view;
}

private function buildParameters(): string
{
$parameters = array_map(
fn ($parameter) => sprintf(
"%s'%s' => \$%s%s,%s",
str_pad(' ', 12),
$parameter,
in_array($parameter, $this->properties()) ? 'this->' : '',
$parameter,
PHP_EOL
),
$this->data()
);

return sprintf(
'[%s%s%s]',
PHP_EOL,
implode($parameters),
str_pad(' ', 8)
);
}
}
1 change: 1 addition & 0 deletions tests/Feature/Generators/ControllerGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public static function controllerTreeDataProvider(): array
['drafts/nested-components.yaml', 'app/Http/Controllers/Admin/UserController.php', 'controllers/nested-components.php'],
['drafts/respond-statements.yaml', 'app/Http/Controllers/Api/PostController.php', 'controllers/respond-statements.php'],
['drafts/resource-statements.yaml', 'app/Http/Controllers/UserController.php', 'controllers/resource-statements.php'],
['drafts/inertia-render.yaml', 'app/Http/Controllers/CustomerController.php', 'controllers/inertia-render.php'],
['drafts/save-without-validation.yaml', 'app/Http/Controllers/PostController.php', 'controllers/save-without-validation.php'],
['drafts/api-resource-pagination.yaml', 'app/Http/Controllers/PostController.php', 'controllers/api-resource-pagination.php'],
['drafts/api-routes-example.yaml', 'app/Http/Controllers/Api/CertificateController.php', 'controllers/api-routes-example.php'],
Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/Lexers/StatementLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Blueprint\Models\Statements\DispatchStatement;
use Blueprint\Models\Statements\EloquentStatement;
use Blueprint\Models\Statements\FireStatement;
use Blueprint\Models\Statements\InertiaStatement;
use Blueprint\Models\Statements\QueryStatement;
use Blueprint\Models\Statements\RedirectStatement;
use Blueprint\Models\Statements\RenderStatement;
Expand Down Expand Up @@ -73,6 +74,22 @@ public function it_returns_a_render_statement_with_data(): void
$this->assertEquals(['foo', 'bar', 'baz'], $actual[0]->data());
}

#[Test]
public function it_returns_an_inertia_statement_with_data(): void
{
$tokens = [
'inertia' => 'post.index with:foo,bar,baz',
];

$actual = $this->subject->analyze($tokens);

$this->assertCount(1, $actual);
$this->assertInstanceOf(InertiaStatement::class, $actual[0]);

$this->assertEquals('post.index', $actual[0]->view());
$this->assertEquals(['foo', 'bar', 'baz'], $actual[0]->data());
}

#[Test]
public function it_returns_an_event_statement(): void
{
Expand Down
21 changes: 21 additions & 0 deletions tests/fixtures/controllers/inertia-render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Controllers;

use App\Customer;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class CustomerController extends Controller
{
public function show(Request $request, Customer $customer): Response
{
$customers = Customer::all();

return Inertia::render('customer.show', [
'customer' => $customer,
'customers' => $customers,
]);
}
}
5 changes: 5 additions & 0 deletions tests/fixtures/drafts/inertia-render.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
controllers:
Customer:
show:
query: all
inertia: customer.show with:customer,customers
Loading