Skip to content

Commit 369faff

Browse files
committed
lazy -> optional
1 parent 7ad2ffb commit 369faff

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed

src/Inertia.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @method static void flushShared()
1212
* @method static void version(\Closure|string|null $version)
1313
* @method static string getVersion()
14+
* @method static \Inertia\OptionalProp optional(callable $callback)
1415
* @method static \Inertia\LazyProp lazy(callable $callback)
1516
* @method static \Inertia\DeferProp defer(callable $callback, string $group = 'default')
1617
* @method static \Inertia\AlwaysProp always(mixed $value)

src/OptionalProp.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Inertia;
4+
5+
use Illuminate\Support\Facades\App;
6+
7+
class OptionalProp
8+
{
9+
protected $callback;
10+
11+
public function __construct(callable $callback)
12+
{
13+
$this->callback = $callback;
14+
}
15+
16+
public function __invoke()
17+
{
18+
return App::call($this->callback);
19+
}
20+
}

src/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function resolveProperties(Request $request, array $props): array
114114

115115
if (! $isPartial) {
116116
$props = array_filter($this->props, static function ($prop) {
117-
return ! ($prop instanceof LazyProp) && ! ($prop instanceof DeferProp);
117+
return ! ($prop instanceof OptionalProp) && ! ($prop instanceof LazyProp) && ! ($prop instanceof DeferProp);
118118
});
119119
}
120120

@@ -228,7 +228,7 @@ public function resolvePropertyInstances(array $props, Request $request): array
228228
$value = App::call($value);
229229
}
230230

231-
if ($value instanceof LazyProp) {
231+
if ($value instanceof LazyProp || $value instanceof OptionalProp || $value instanceof DeferProp) {
232232
$value = App::call($value);
233233
}
234234

src/ResponseFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,19 @@ public function getVersion(): string
8282
return (string) $version;
8383
}
8484

85+
/**
86+
* @deprecated Use `optional` instead.
87+
*/
8588
public function lazy(callable $callback): LazyProp
8689
{
8790
return new LazyProp($callback);
8891
}
8992

93+
public function optional(callable $callback): OptionalProp
94+
{
95+
return new OptionalProp($callback);
96+
}
97+
9098
public function defer(callable $callback, string $group = 'default'): DeferProp
9199
{
92100
return new DeferProp($callback, $group);

tests/OptionalPropTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Inertia\Tests;
4+
5+
use Illuminate\Http\Request;
6+
use Inertia\OptionalProp;
7+
8+
class OptionalPropTest extends TestCase
9+
{
10+
public function test_can_invoke(): void
11+
{
12+
$optionalProp = new OptionalProp(function () {
13+
return 'A lazy value';
14+
});
15+
16+
$this->assertSame('A lazy value', $optionalProp());
17+
}
18+
19+
public function test_can_resolve_bindings_when_invoked(): void
20+
{
21+
$optionalProp = new OptionalProp(function (Request $request) {
22+
return $request;
23+
});
24+
25+
$this->assertInstanceOf(Request::class, $optionalProp());
26+
}
27+
}

tests/ResponseFactoryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
use Illuminate\Support\Facades\Request;
1313
use Illuminate\Support\Facades\Route;
1414
use Inertia\AlwaysProp;
15+
use Inertia\DeferProp;
1516
use Inertia\Inertia;
1617
use Inertia\LazyProp;
18+
use Inertia\OptionalProp;
1719
use Inertia\ResponseFactory;
1820
use Inertia\Tests\Stubs\ExampleMiddleware;
1921

@@ -161,6 +163,26 @@ public function test_can_create_lazy_prop(): void
161163
$this->assertInstanceOf(LazyProp::class, $lazyProp);
162164
}
163165

166+
public function test_can_create_deferred_prop(): void
167+
{
168+
$factory = new ResponseFactory();
169+
$deferredProp = $factory->defer(function () {
170+
return 'A deferred value';
171+
});
172+
173+
$this->assertInstanceOf(DeferProp::class, $deferredProp);
174+
}
175+
176+
public function test_can_create_optional_prop(): void
177+
{
178+
$factory = new ResponseFactory();
179+
$optionalProp = $factory->optional(function () {
180+
return 'An optional value';
181+
});
182+
183+
$this->assertInstanceOf(OptionalProp::class, $optionalProp);
184+
}
185+
164186
public function test_can_create_always_prop(): void
165187
{
166188
$factory = new ResponseFactory();

0 commit comments

Comments
 (0)