Skip to content
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
16 changes: 16 additions & 0 deletions src/PropertyContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Inertia;

use Illuminate\Http\Request;

class PropertyContext
{
public function __construct(
public string $key,
public array $props,
public Request $request
) {
//
}
}
8 changes: 8 additions & 0 deletions src/ProvidesInertiaProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Inertia;

interface ProvidesInertiaProperty
{
public function toInertiaProperty(PropertyContext $prop): mixed;
}
10 changes: 8 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function resolveAlways(array $props): array
/**
* Resolve all necessary class instances in the given props.
*/
public function resolvePropertyInstances(array $props, Request $request): array
public function resolvePropertyInstances(array $props, Request $request, ?string $parentKey = null): array
{
foreach ($props as $key => $value) {
$resolveViaApp = collect([
Expand All @@ -270,6 +270,12 @@ public function resolvePropertyInstances(array $props, Request $request): array
$value = App::call($value);
}

$currentKey = $parentKey ? $parentKey.'.'.$key : $key;

if ($value instanceof ProvidesInertiaProperty) {
$value = $value->toInertiaProperty(new PropertyContext($currentKey, $props, $request));
}

if ($value instanceof Arrayable) {
$value = $value->toArray();
}
Expand All @@ -287,7 +293,7 @@ public function resolvePropertyInstances(array $props, Request $request): array
}

if (is_array($value)) {
$value = $this->resolvePropertyInstances($value, $request);
$value = $this->resolvePropertyInstances($value, $request, $currentKey);
}

$props[$key] = $value;
Expand Down
25 changes: 25 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
use Illuminate\View\View;
use Inertia\AlwaysProp;
use Inertia\DeferProp;
use Inertia\Inertia;
use Inertia\LazyProp;
use Inertia\MergeProp;
use Inertia\Response;
use Inertia\Tests\Stubs\FakeResource;
use Inertia\Tests\Stubs\MergeWithSharedProp;
use Mockery;

class ResponseTest extends TestCase
Expand Down Expand Up @@ -820,6 +822,29 @@ public function test_always_props_are_included_on_partial_reload(): void
$this->assertFalse(isset($page->props->user));
}

public function test_inertia_response_type_prop(): void
{
$request = Request::create('/user/123', 'GET');

Inertia::share('items', ['foo']);
Inertia::share('deep.foo.bar', ['foo']);

$response = new Response('User/Edit', [
'items' => new MergeWithSharedProp(['bar']),
'deep' => [
'foo' => [
'bar' => new MergeWithSharedProp(['baz']),
],
],
], 'app', '123');
$response = $response->toResponse($request);
$view = $response->getOriginalContent();
$page = $view->getData()['page'];

$this->assertSame(['foo', 'bar'], $page['props']['items']);
$this->assertSame(['foo', 'baz'], $page['props']['deep']['foo']['bar']);
}

public function test_top_level_dot_props_get_unpacked(): void
{
$props = [
Expand Down
17 changes: 17 additions & 0 deletions tests/Stubs/MergeWithSharedProp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Inertia\Tests\Stubs;

use Inertia\Inertia;
use Inertia\PropertyContext;
use Inertia\ProvidesInertiaProperty;

class MergeWithSharedProp implements ProvidesInertiaProperty
{
public function __construct(protected array $items = []) {}

public function toInertiaProperty(PropertyContext $prop): mixed
{
return array_merge(Inertia::getShared($prop->key, []), $this->items);
}
}