3
3
namespace Inertia ;
4
4
5
5
use Closure ;
6
- use Illuminate \Support \Arr ;
7
6
use Illuminate \Http \Request ;
8
7
use Illuminate \Http \JsonResponse ;
9
8
use Illuminate \Support \Facades \App ;
14
13
use Illuminate \Contracts \Support \Responsable ;
15
14
use Illuminate \Http \Resources \Json \JsonResource ;
16
15
use Illuminate \Http \Resources \Json \ResourceResponse ;
16
+ use Illuminate \Support \Arr ;
17
17
use Illuminate \Support \Facades \Response as ResponseFactory ;
18
+ use Inertia \Support \Header ;
18
19
19
20
class Response implements Responsable
20
21
{
@@ -87,15 +88,7 @@ public function rootView(string $rootView): self
87
88
*/
88
89
public function toResponse ($ request )
89
90
{
90
- $ only = array_filter (explode (', ' , $ request ->header ('X-Inertia-Partial-Data ' , '' )));
91
-
92
- $ props = ($ only && $ request ->header ('X-Inertia-Partial-Component ' ) === $ this ->component )
93
- ? Arr::only ($ this ->props , $ only )
94
- : array_filter ($ this ->props , static function ($ prop ) {
95
- return ! ($ prop instanceof LazyProp);
96
- });
97
-
98
- $ props = $ this ->resolvePropertyInstances ($ props , $ request );
91
+ $ props = $ this ->resolveProperties ($ request , $ this ->props );
99
92
100
93
$ page = [
101
94
'component ' => $ this ->component ,
@@ -104,17 +97,82 @@ public function toResponse($request)
104
97
'version ' => $ this ->version ,
105
98
];
106
99
107
- if ($ request ->header (' X-Inertia ' )) {
108
- return new JsonResponse ($ page , 200 , [' X-Inertia ' => 'true ' ]);
100
+ if ($ request ->header (Header:: INERTIA )) {
101
+ return new JsonResponse ($ page , 200 , [Header:: INERTIA => 'true ' ]);
109
102
}
110
103
111
104
return ResponseFactory::view ($ this ->rootView , $ this ->viewData + ['page ' => $ page ]);
112
105
}
113
106
107
+ /**
108
+ * Resolve the properites for the response.
109
+ */
110
+ public function resolveProperties (Request $ request , array $ props ): array
111
+ {
112
+ $ isPartial = $ request ->header (Header::PARTIAL_COMPONENT ) === $ this ->component ;
113
+
114
+ if (!$ isPartial ) {
115
+ $ props = array_filter ($ this ->props , static function ($ prop ) {
116
+ return ! ($ prop instanceof LazyProp);
117
+ });
118
+ }
119
+
120
+ $ props = $ this ->resolveArrayableProperties ($ props , $ request );
121
+
122
+ if ($ isPartial && $ request ->hasHeader (Header::PARTIAL_ONLY )) {
123
+ $ props = $ this ->resolveOnly ($ request , $ props );
124
+ }
125
+
126
+ $ props = $ this ->resolvePropertyInstances ($ props , $ request );
127
+
128
+ return $ props ;
129
+ }
130
+
131
+ /**
132
+ * Resolve all arrayables properties into an array.
133
+ */
134
+ public function resolveArrayableProperties (array $ props , Request $ request , bool $ unpackDotProps = true ): array
135
+ {
136
+ foreach ($ props as $ key => $ value ) {
137
+ if ($ value instanceof Arrayable) {
138
+ $ value = $ value ->toArray ();
139
+ }
140
+
141
+ if (is_array ($ value )) {
142
+ $ value = $ this ->resolveArrayableProperties ($ value , $ request , false );
143
+ }
144
+
145
+ if ($ unpackDotProps && str_contains ($ key , '. ' )) {
146
+ Arr::set ($ props , $ key , $ value );
147
+ unset($ props [$ key ]);
148
+ } else {
149
+ $ props [$ key ] = $ value ;
150
+ }
151
+ }
152
+
153
+ return $ props ;
154
+ }
155
+
156
+ /**
157
+ * Resolve the `only` partial request props.
158
+ */
159
+ public function resolveOnly (Request $ request , array $ props ): array
160
+ {
161
+ $ only = array_filter (explode (', ' , $ request ->header (Header::PARTIAL_ONLY , '' )));
162
+
163
+ $ value = [];
164
+
165
+ foreach ($ only as $ key ) {
166
+ Arr::set ($ value , $ key , data_get ($ props , $ key ));
167
+ }
168
+
169
+ return $ value ;
170
+ }
171
+
114
172
/**
115
173
* Resolve all necessary class instances in the given props.
116
174
*/
117
- public function resolvePropertyInstances (array $ props , Request $ request, bool $ unpackDotProps = true ): array
175
+ public function resolvePropertyInstances (array $ props , Request $ request ): array
118
176
{
119
177
foreach ($ props as $ key => $ value ) {
120
178
if ($ value instanceof Closure) {
@@ -133,20 +191,11 @@ public function resolvePropertyInstances(array $props, Request $request, bool $u
133
191
$ value = $ value ->toResponse ($ request )->getData (true );
134
192
}
135
193
136
- if ($ value instanceof Arrayable) {
137
- $ value = $ value ->toArray ();
138
- }
139
-
140
194
if (is_array ($ value )) {
141
- $ value = $ this ->resolvePropertyInstances ($ value , $ request, false );
195
+ $ value = $ this ->resolvePropertyInstances ($ value , $ request );
142
196
}
143
197
144
- if ($ unpackDotProps && str_contains ($ key , '. ' )) {
145
- Arr::set ($ props , $ key , $ value );
146
- unset($ props [$ key ]);
147
- } else {
148
- $ props [$ key ] = $ value ;
149
- }
198
+ $ props [$ key ] = $ value ;
150
199
}
151
200
152
201
return $ props ;
0 commit comments