Skip to content

Commit fcf7ba3

Browse files
committed
Introduce InertiaResponsible interface
1 parent a1f1626 commit fcf7ba3

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

src/InertiaResponsible.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Inertia;
4+
5+
interface InertiaResponsible
6+
{
7+
public function toInertiaResponse(Prop $prop): mixed;
8+
}

src/Prop.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Inertia;
4+
5+
use Illuminate\Http\Request;
6+
7+
class Prop
8+
{
9+
public function __construct(
10+
public string $key,
11+
public array $props,
12+
public Request $request
13+
) {
14+
//
15+
}
16+
}

src/Response.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public function resolveAlways(array $props): array
245245
/**
246246
* Resolve all necessary class instances in the given props.
247247
*/
248-
public function resolvePropertyInstances(array $props, Request $request): array
248+
public function resolvePropertyInstances(array $props, Request $request, ?string $parentKey = null): array
249249
{
250250
foreach ($props as $key => $value) {
251251
$resolveViaApp = collect([
@@ -261,6 +261,12 @@ public function resolvePropertyInstances(array $props, Request $request): array
261261
$value = App::call($value);
262262
}
263263

264+
$currentKey = $parentKey ? $parentKey.'.'.$key : $key;
265+
266+
if ($value instanceof InertiaResponsible) {
267+
$value = $value->toInertiaResponse(new Prop($currentKey, $props, $request));
268+
}
269+
264270
if ($value instanceof Arrayable) {
265271
$value = $value->toArray();
266272
}
@@ -278,7 +284,7 @@ public function resolvePropertyInstances(array $props, Request $request): array
278284
}
279285

280286
if (is_array($value)) {
281-
$value = $this->resolvePropertyInstances($value, $request);
287+
$value = $this->resolvePropertyInstances($value, $request, $currentKey);
282288
}
283289

284290
$props[$key] = $value;

tests/ResponseTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
use Illuminate\View\View;
1515
use Inertia\AlwaysProp;
1616
use Inertia\DeferProp;
17+
use Inertia\Inertia;
1718
use Inertia\LazyProp;
1819
use Inertia\MergeProp;
1920
use Inertia\Response;
2021
use Inertia\Tests\Stubs\FakeResource;
22+
use Inertia\Tests\Stubs\MergeWithSharedProp;
2123
use Mockery;
2224

2325
class ResponseTest extends TestCase
@@ -757,6 +759,29 @@ public function test_always_props_are_included_on_partial_reload(): void
757759
$this->assertFalse(isset($page->props->user));
758760
}
759761

762+
public function test_inertia_response_type_prop(): void
763+
{
764+
$request = Request::create('/user/123', 'GET');
765+
766+
Inertia::share('items', ['foo']);
767+
Inertia::share('deep.foo.bar', ['foo']);
768+
769+
$response = new Response('User/Edit', [
770+
'items' => new MergeWithSharedProp(['bar']),
771+
'deep' => [
772+
'foo' => [
773+
'bar' => new MergeWithSharedProp(['baz']),
774+
],
775+
],
776+
], 'app', '123');
777+
$response = $response->toResponse($request);
778+
$view = $response->getOriginalContent();
779+
$page = $view->getData()['page'];
780+
781+
$this->assertSame(['foo', 'bar'], $page['props']['items']);
782+
$this->assertSame(['foo', 'baz'], $page['props']['deep']['foo']['bar']);
783+
}
784+
760785
public function test_top_level_dot_props_get_unpacked(): void
761786
{
762787
$props = [

tests/Stubs/MergeWithSharedProp.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Inertia\Tests\Stubs;
4+
5+
use Inertia\Inertia;
6+
use Inertia\InertiaResponsible;
7+
use Inertia\Prop;
8+
9+
class MergeWithSharedProp implements InertiaResponsible
10+
{
11+
public function __construct(protected array $items = []) {}
12+
13+
public function toInertiaResponse(Prop $prop): mixed
14+
{
15+
return array_merge(Inertia::getShared($prop->key, []), $this->items);
16+
}
17+
}

0 commit comments

Comments
 (0)