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

namespace Inertia;

use Illuminate\Support\Facades\App;

class LazyProp
{
protected $callback;

public function __construct(callable $callback)
{
$this->callback = $callback;
}

public function __invoke()
{
return App::call($this->callback);
}
}
8 changes: 7 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ public function toResponse($request)

$props = ($only && $request->header('X-Inertia-Partial-Component') === $this->component)
? Arr::only($this->props, $only)
: $this->props;
: array_filter($this->props, function ($prop) {
return ! ($prop instanceof LazyProp);
});

array_walk_recursive($props, function (&$prop) use ($request) {
if ($prop instanceof LazyProp) {
$prop = App::call($prop);
}

if ($prop instanceof Closure) {
$prop = App::call($prop);
}
Expand Down
5 changes: 5 additions & 0 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public function getVersion()
return (string) $version;
}

public function lazy(callable $callback)
{
return new LazyProp($callback);
}

public function render($component, $props = [])
{
if ($props instanceof Arrayable) {
Expand Down
27 changes: 27 additions & 0 deletions tests/LazyPropTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Inertia\Tests;

use Illuminate\Http\Request;
use Inertia\LazyProp;

class LazyPropTest extends TestCase
{
public function test_can_invoke()
{
$lazyProp = new LazyProp(function () {
return 'A lazy value';
});

$this->assertSame('A lazy value', $lazyProp());
}

public function test_can_resolve_bindings_when_invoked()
{
$lazyProp = new LazyProp(function (Request $request) {
return $request;
});

$this->assertInstanceOf(Request::class, $lazyProp());
}
}
11 changes: 11 additions & 0 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Session\Middleware\StartSession;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
use Inertia\LazyProp;
use Inertia\ResponseFactory;
use Inertia\Tests\Stubs\ExampleMiddleware;

Expand Down Expand Up @@ -69,4 +70,14 @@ public function test_shared_data_can_be_shared_from_anywhere()
],
]);
}

public function test_can_create_lazy_prop()
{
$factory = new ResponseFactory();
$lazyProp = $factory->lazy(function () {
return 'A lazy value';
});

$this->assertInstanceOf(LazyProp::class, $lazyProp);
}
}
37 changes: 37 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Fluent;
use Illuminate\View\View;
use Inertia\LazyProp;
use Inertia\Response;

class ResponseTest extends TestCase
Expand Down Expand Up @@ -182,4 +183,40 @@ public function test_xhr_partial_response()
$this->assertSame('/user/123', $page->url);
$this->assertSame('123', $page->version);
}

public function test_lazy_props_are_not_included_by_default()
{
$request = Request::create('/users', 'GET');
$request->headers->add(['X-Inertia' => 'true']);

$lazyProp = new LazyProp(function () {
return 'A lazy value';
});

$response = new Response('Users', ['users' => [], 'lazy' => $lazyProp], 'app', '123');
$response = $response->toResponse($request);
$page = $response->getData();

$this->assertSame([], $page->props->users);
$this->assertObjectNotHasAttribute('lazy', $page->props);
}

public function test_lazy_props_are_included_in_partial_reload()
{
$request = Request::create('/users', 'GET');
$request->headers->add(['X-Inertia' => 'true']);
$request->headers->add(['X-Inertia-Partial-Component' => 'Users']);
$request->headers->add(['X-Inertia-Partial-Data' => 'lazy']);

$lazyProp = new LazyProp(function () {
return 'A lazy value';
});

$response = new Response('Users', ['users' => [], 'lazy' => $lazyProp], 'app', '123');
$response = $response->toResponse($request);
$page = $response->getData();

$this->assertObjectNotHasAttribute('users', $page->props);
$this->assertSame('A lazy value', $page->props->lazy);
}
}