/
SubsequentRender.php
77 lines (60 loc) · 2.37 KB
/
SubsequentRender.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace Livewire\Features\SupportTesting;
class SubsequentRender extends Render
{
function __construct(
protected RequestBroker $requestBroker,
protected ComponentState $lastState,
) {}
static function make($requestBroker, $lastState, $calls = [], $updates = [], $cookies = [])
{
$instance = new static($requestBroker, $lastState);
return $instance->makeSubsequentRequest($calls, $updates, $cookies);
}
function makeSubsequentRequest($calls = [], $updates = [], $cookies = []) {
$uri = app('livewire')->getUpdateUri();
$encodedSnapshot = json_encode($this->lastState->getSnapshot());
$payload = [
'components' => [
[
'snapshot' => $encodedSnapshot,
'calls' => $calls,
'updates' => $updates,
],
],
];
[$response, $componentInstance, $componentView] = $this->extractComponentAndBladeView(function () use ($uri, $payload, $cookies) {
return $this->requestBroker->temporarilyDisableExceptionHandlingAndMiddleware(function ($requestBroker) use ($uri, $payload, $cookies) {
return $requestBroker->addHeaders(['X-Livewire' => true])->call('POST', $uri, $payload, $cookies);
});
});
app('livewire')->flushState();
if (! $response->isOk()) {
return new ComponentState(
$componentInstance,
$response,
null,
'',
[],
[],
);
}
$json = $response->json();
// Set "original" to Blade view for assertions like "assertViewIs()"...
$response->original = $componentView;
$componentResponsePayload = $json['components'][0];
$snapshot = json_decode($componentResponsePayload['snapshot'], true);
$effects = $componentResponsePayload['effects'];
// If no new HTML has been rendered, let's forward the last known HTML...
$html = $effects['html'] ?? $this->lastState->getHtml(stripInitialData: true);
$view = $componentView ?? $this->lastState->getView();
return new ComponentState(
$componentInstance,
$response,
$view,
$html,
$snapshot,
$effects,
);
}
}