Skip to content

Commit 5edda49

Browse files
committed
wip
1 parent c457ba3 commit 5edda49

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/Inertia.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
* @method static void setRootView(string $name)
9-
* @method static void share(string|array<string, mixed>|\Illuminate\Contracts\Support\Arrayable<array-key, mixed> $key, mixed $value = null)
9+
* @method static void share(string|array<string, mixed>|\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|\Inertia\ProvidesInertiaProperties $key, mixed $value = null)
1010
* @method static mixed getShared(string|null $key = null, mixed $default = null)
1111
* @method static void clearHistory()
1212
* @method static void encryptHistory($encrypt = true)

src/ResponseFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function setRootView(string $name): void
7676
* included with every response, making it ideal for user authentication
7777
* state, flash messages, etc.
7878
*
79-
* @param string|array<string, mixed>|\Illuminate\Contracts\Support\Arrayable<array-key, mixed> $key
79+
* @param string|array<string, mixed>|\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|\Inertia\ProvidesInertiaProperties $key
8080
* @param mixed $value
8181
*/
8282
public function share($key, $value = null): void
@@ -85,6 +85,8 @@ public function share($key, $value = null): void
8585
$this->sharedProps = array_merge($this->sharedProps, $key);
8686
} elseif ($key instanceof Arrayable) {
8787
$this->sharedProps = array_merge($this->sharedProps, $key->toArray());
88+
} elseif ($key instanceof ProvidesInertiaProperties) {
89+
$this->sharedProps = array_merge($this->sharedProps, [$key]);
8890
} else {
8991
Arr::set($this->sharedProps, $key, $value);
9092
}

tests/ResponseFactoryTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,38 @@ public function toInertiaProperties(RenderContext $context): iterable
407407
]);
408408
}
409409

410+
public function test_can_share_instances_of_provides_inertia_props(): void
411+
{
412+
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
413+
Inertia::share(new class implements ProvidesInertiaProperties
414+
{
415+
/**
416+
* @return array<string, mixed>
417+
*/
418+
public function toInertiaProperties(RenderContext $context): iterable
419+
{
420+
return [
421+
'shared' => 'data',
422+
];
423+
}
424+
});
425+
426+
return Inertia::render('User/Edit', [
427+
'regular' => 'prop',
428+
]);
429+
});
430+
431+
$response = $this->withoutExceptionHandling()->get('/', ['X-Inertia' => 'true']);
432+
$response->assertSuccessful();
433+
$response->assertJson([
434+
'component' => 'User/Edit',
435+
'props' => [
436+
'shared' => 'data',
437+
'regular' => 'prop',
438+
],
439+
]);
440+
}
441+
410442
public function test_will_throw_exception_if_component_does_not_exist_when_ensuring_is_enabled(): void
411443
{
412444
config()->set('inertia.ensure_pages_exist', true);

0 commit comments

Comments
 (0)