Skip to content

Commit cb01959

Browse files
authored
Merge pull request #26 from inertiajs/improved-share
Add ability to use share() with only a callback
2 parents 1c9acd0 + ae96ff2 commit cb01959

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/Response.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ class Response implements Responsable
1111
protected $component;
1212
protected $props;
1313
protected $rootView;
14-
protected $sharedProps;
1514
protected $version;
1615
protected $viewData = [];
1716

18-
public function __construct($component, $props, $rootView = 'app', $sharedProps = [], $version = null)
17+
public function __construct($component, $props, $rootView = 'app', $version = null)
1918
{
2019
$this->component = $component;
2120
$this->props = $props;
2221
$this->rootView = $rootView;
23-
$this->sharedProps = $sharedProps;
2422
$this->version = $version;
2523
}
2624

@@ -46,7 +44,7 @@ public function toResponse($request)
4644
{
4745
$page = [
4846
'component' => $this->component,
49-
'props' => array_merge($this->sharedProps, $this->props),
47+
'props' => $this->props,
5048
'url' => $request->getRequestUri(),
5149
'version' => $this->version,
5250
];

src/ResponseFactory.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@
22

33
namespace Inertia;
44

5+
use Closure;
56
use Illuminate\Support\Arr;
67
use Illuminate\Support\Facades\App;
78

89
class ResponseFactory
910
{
1011
protected $rootView = 'app';
1112
protected $sharedProps = [];
13+
protected $sharedPropsCallbacks = [];
1214
protected $version = null;
1315

1416
public function setRootView($name)
1517
{
1618
$this->rootView = $name;
1719
}
1820

19-
public function share($key, $value)
21+
public function share($key, $value = null)
2022
{
21-
Arr::set($this->sharedProps, $key, $value);
23+
if ($key instanceof Closure) {
24+
$this->sharedPropsCallbacks[] = $key;
25+
} else {
26+
Arr::set($this->sharedProps, $key, $value);
27+
}
2228
}
2329

2430
public function getShared($key = null)
@@ -42,12 +48,18 @@ public function getVersion()
4248

4349
public function render($component, $props = [])
4450
{
45-
array_walk_recursive($this->sharedProps, function (&$item, $key) {
46-
if (is_callable($item)) {
47-
$item = App::call($item);
51+
$props = array_merge($this->sharedProps, $props);
52+
53+
foreach ($this->sharedPropsCallbacks as $callback) {
54+
$props = array_merge($props, App::call($callback));
55+
}
56+
57+
array_walk_recursive($props, function (&$prop) {
58+
if (is_callable($prop)) {
59+
$prop = App::call($prop);
4860
}
4961
});
5062

51-
return new Response($component, $props, $this->rootView, $this->sharedProps, $this->getVersion());
63+
return new Response($component, $props, $this->rootView, $this->getVersion());
5264
}
5365
}

tests/ResponseTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public function test_server_response()
1717
'User/Edit',
1818
['user' => ['name' => 'Jonathan']],
1919
'app',
20-
['foo' => 'bar'],
2120
'123'
2221
);
2322

@@ -27,10 +26,9 @@ public function test_server_response()
2726
$this->assertInstanceOf(View::class, $response);
2827
$this->assertSame('User/Edit', $page['component']);
2928
$this->assertSame('Jonathan', $page['props']['user']['name']);
30-
$this->assertSame('bar', $page['props']['foo']);
3129
$this->assertSame('/user/123', $page['url']);
3230
$this->assertSame('123', $page['version']);
33-
$this->assertSame('<div id="app" data-page="{&quot;component&quot;:&quot;User\/Edit&quot;,&quot;props&quot;:{&quot;foo&quot;:&quot;bar&quot;,&quot;user&quot;:{&quot;name&quot;:&quot;Jonathan&quot;}},&quot;url&quot;:&quot;\/user\/123&quot;,&quot;version&quot;:&quot;123&quot;}"></div>'."\n", $response->render());
31+
$this->assertSame('<div id="app" data-page="{&quot;component&quot;:&quot;User\/Edit&quot;,&quot;props&quot;:{&quot;user&quot;:{&quot;name&quot;:&quot;Jonathan&quot;}},&quot;url&quot;:&quot;\/user\/123&quot;,&quot;version&quot;:&quot;123&quot;}"></div>'."\n", $response->render());
3432
}
3533

3634
public function test_xhr_response()
@@ -42,7 +40,6 @@ public function test_xhr_response()
4240
'User/Edit',
4341
['user' => ['name' => 'Jonathan']],
4442
'app',
45-
['foo' => 'bar'],
4643
'123'
4744
);
4845

@@ -52,7 +49,6 @@ public function test_xhr_response()
5249
$this->assertInstanceOf(JsonResponse::class, $response);
5350
$this->assertSame('User/Edit', $page->component);
5451
$this->assertSame('Jonathan', $page->props->user->name);
55-
$this->assertSame('bar', $page->props->foo);
5652
$this->assertSame('/user/123', $page->url);
5753
$this->assertSame('123', $page->version);
5854
}

0 commit comments

Comments
 (0)