33namespace Inertia ;
44
55use Closure ;
6- use Illuminate \Support \Arr ;
76use Illuminate \Http \Request ;
87use Illuminate \Http \JsonResponse ;
98use Illuminate \Support \Facades \App ;
1413use Illuminate \Contracts \Support \Responsable ;
1514use Illuminate \Http \Resources \Json \JsonResource ;
1615use Illuminate \Http \Resources \Json \ResourceResponse ;
16+ use Illuminate \Support \Arr ;
1717use Illuminate \Support \Facades \Response as ResponseFactory ;
18+ use Inertia \Support \Header ;
1819
1920class Response implements Responsable
2021{
@@ -87,15 +88,7 @@ public function rootView(string $rootView): self
8788 */
8889 public function toResponse ($ request )
8990 {
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 );
9992
10093 $ page = [
10194 'component ' => $ this ->component ,
@@ -104,17 +97,82 @@ public function toResponse($request)
10497 'version ' => $ this ->version ,
10598 ];
10699
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 ' ]);
109102 }
110103
111104 return ResponseFactory::view ($ this ->rootView , $ this ->viewData + ['page ' => $ page ]);
112105 }
113106
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+
114172 /**
115173 * Resolve all necessary class instances in the given props.
116174 */
117- public function resolvePropertyInstances (array $ props , Request $ request, bool $ unpackDotProps = true ): array
175+ public function resolvePropertyInstances (array $ props , Request $ request ): array
118176 {
119177 foreach ($ props as $ key => $ value ) {
120178 if ($ value instanceof Closure) {
@@ -133,20 +191,11 @@ public function resolvePropertyInstances(array $props, Request $request, bool $u
133191 $ value = $ value ->toResponse ($ request )->getData (true );
134192 }
135193
136- if ($ value instanceof Arrayable) {
137- $ value = $ value ->toArray ();
138- }
139-
140194 if (is_array ($ value )) {
141- $ value = $ this ->resolvePropertyInstances ($ value , $ request, false );
195+ $ value = $ this ->resolvePropertyInstances ($ value , $ request );
142196 }
143197
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 ;
150199 }
151200
152201 return $ props ;
0 commit comments