26
26
*/
27
27
final class TestLiveComponent
28
28
{
29
- private array $ props ;
30
- private string $ csrfToken ;
31
- private object $ component ;
32
-
33
29
/**
34
30
* @internal
35
31
*/
@@ -55,11 +51,9 @@ public function __construct(
55
51
$ this ->metadata ->get ('route ' ),
56
52
[
57
53
'_live_component ' => $ this ->metadata ->getName (),
58
- 'props ' => json_encode ($ props ->getProps ()),
54
+ 'props ' => json_encode ($ props ->getProps (), flags: \ JSON_THROW_ON_ERROR ),
59
55
]
60
56
));
61
-
62
- $ this ->updateState ();
63
57
}
64
58
65
59
public function render (): RenderedComponent
@@ -69,19 +63,15 @@ public function render(): RenderedComponent
69
63
70
64
public function component (): object
71
65
{
72
- if (isset ($ this ->component )) {
73
- return $ this ->component ;
74
- }
75
-
76
66
$ component = $ this ->factory ->get ($ this ->metadata ->getName ());
77
67
$ componentAttributes = $ this ->hydrator ->hydrate (
78
68
$ component ,
79
- $ this ->props ,
69
+ $ this ->props () ,
80
70
[],
81
71
$ this ->metadataFactory ->getMetadata ($ this ->metadata ->getName ()),
82
72
);
83
73
84
- return $ this -> component = (new MountedComponent ($ this ->metadata ->getName (), $ component , $ componentAttributes ))->getComponent ();
74
+ return (new MountedComponent ($ this ->metadata ->getName (), $ component , $ componentAttributes ))->getComponent ();
85
75
}
86
76
87
77
/**
@@ -117,9 +107,7 @@ public function response(): Response
117
107
118
108
private function request (array $ content = [], string $ action = null ): self
119
109
{
120
- if (!isset ($ this ->props )) {
121
- throw new \LogicException ('A live component action has redirected and you can no longer access the component. ' );
122
- }
110
+ $ csrfToken = $ this ->csrfToken ();
123
111
124
112
$ this ->client ->request (
125
113
'POST ' ,
@@ -130,31 +118,32 @@ private function request(array $content = [], string $action = null): self
130
118
'_live_action ' => $ action ,
131
119
])
132
120
),
133
- parameters: ['data ' => json_encode (array_merge ($ content , ['props ' => $ this ->props ]))],
134
- server: ['HTTP_X_CSRF_TOKEN ' => $ this -> csrfToken ],
121
+ parameters: ['data ' => json_encode (array_merge ($ content , ['props ' => $ this ->props () ]))],
122
+ server: $ csrfToken ? ['HTTP_X_CSRF_TOKEN ' => $ csrfToken] : [ ],
135
123
);
136
124
137
- return $ this -> updateState () ;
125
+ return $ this ;
138
126
}
139
127
140
- private function updateState (): self
128
+ private function props (): array
141
129
{
142
130
$ crawler = $ this ->client ->getCrawler ();
143
131
144
- unset($ this ->component );
132
+ if (!count ($ node = $ crawler ->filter ('[data-live-props-value] ' ))) {
133
+ throw new \LogicException ('A live component action has redirected and you can no longer access the component. ' );
134
+ }
145
135
146
- $ props = $ crawler -> filter ( ' [ data-live-props-value] ' );
147
- $ csrf = $ crawler -> filter ( ' [data-live-csrf-value] ' );
136
+ return json_decode ( $ node -> attr ( ' data-live-props-value ' ), true , flags: \ JSON_THROW_ON_ERROR );
137
+ }
148
138
149
- if (!\count ($ props ) || !\count ($ csrf )) {
150
- unset($ this ->props , $ this ->csrfToken );
139
+ private function csrfToken (): ?string
140
+ {
141
+ $ crawler = $ this ->client ->getCrawler ();
151
142
152
- return $ this ;
143
+ if (!count ($ node = $ crawler ->filter ('[data-live-csrf-value] ' ))) {
144
+ return null ;
153
145
}
154
146
155
- $ this ->props = json_decode ($ props ->attr ('data-live-props-value ' ), true , flags: \JSON_THROW_ON_ERROR );
156
- $ this ->csrfToken = $ csrf ->attr ('data-live-csrf-value ' );
157
-
158
- return $ this ;
147
+ return $ node ->attr ('data-live-csrf-value ' );
159
148
}
160
149
}
0 commit comments