33namespace NeoIsRecursive \Inertia \Http ;
44
55use Closure ;
6+ use NeoIsRecursive \Inertia \Contracts \MergeableProp ;
67use NeoIsRecursive \Inertia \Props \AlwaysProp ;
78use NeoIsRecursive \Inertia \Props \LazyProp ;
89use NeoIsRecursive \Inertia \Support \Header ;
1112use Tempest \Router \IsResponse ;
1213use Tempest \Router \Request ;
1314use Tempest \Router \Response ;
15+ use Tempest \Support \ArrayHelper ;
1416
1517use function Tempest \invoke ;
1618use function Tempest \Support \arr ;
@@ -26,24 +28,31 @@ public function __construct(
2628 string $ rootView ,
2729 string $ version ,
2830 ) {
31+
2932 $ alwaysProps = $ this ->resolveAlwaysProps (props: $ props );
30- $ props = $ this ->resolvePartialProps (request: $ request , component: $ page , props: $ props );
33+ $ partialProps = $ this ->resolvePartialProps (request: $ request , component: $ page , props: $ props );
34+ $ mergeProps = $ this ->resolveMergeProps ($ props , $ request );
3135
3236 $ props = $ this ->evaluateProps (
33- props: array_merge ($ props , $ alwaysProps ),
37+ props: array_merge (
38+ $ alwaysProps ,
39+ $ partialProps ,
40+ $ mergeProps
41+ ),
3442 request: $ request ,
3543 unpackDotProps: true
3644 );
3745
3846 $ page = [
3947 'component ' => $ page ,
4048 'props ' => $ props ,
41- 'url ' => $ request ->getUri () ,
49+ 'url ' => $ request ->uri ,
4250 'version ' => $ version ,
51+ 'mergeProps ' => array_keys ($ mergeProps ),
4352 ];
4453
4554
46- if (array_key_exists (Header::INERTIA , $ request ->getHeaders ()) && $ request ->getHeaders () [Header::INERTIA ] == 'true ' ) {
55+ if (array_key_exists (Header::INERTIA , $ request ->headers ) && $ request ->headers [Header::INERTIA ] == 'true ' ) {
4756 $ this ->status = Status::OK ;
4857
4958 $ this ->body = $ page ;
@@ -69,7 +78,7 @@ private function resolveAlwaysProps(array $props): array
6978
7079 private function resolvePartialProps (Request $ request , string $ component , array $ props ): array
7180 {
72- $ headers = $ request ->getHeaders () ;
81+ $ headers = $ request ->headers ;
7382
7483 $ partialHeader = $ headers [Header::PARTIAL_COMPONENT ] ?? null ;
7584
@@ -84,9 +93,9 @@ private function resolvePartialProps(Request $request, string $component, array
8493 $ only = array_filter (explode (', ' , $ headers [Header::PARTIAL_ONLY ] ?? '' ));
8594 $ except = array_filter (explode (', ' , $ headers [Header::PARTIAL_EXCEPT ] ?? '' ));
8695
87- $ props = $ only ? array_intersect_key ($ props , array_flip (( array ) $ only )) : $ props ;
96+ $ props = $ only ? array_intersect_key ($ props , array_flip ($ only )) : $ props ;
8897
89- if ($ except ) {
98+ if (count ( $ except) > 0 ) {
9099 foreach ($ except as $ key ) {
91100 unset($ props [$ key ]);
92101 }
@@ -95,7 +104,20 @@ private function resolvePartialProps(Request $request, string $component, array
95104 return $ props ;
96105 }
97106
98- public function evaluateProps (array $ props , Request $ request , bool $ unpackDotProps = true ): array
107+ public static function resolveMergeProps (array $ props , Request $ request ): array
108+ {
109+ $ resetProps = arr (explode (', ' , $ request ->headers [Header::RESET ] ?? '' ));
110+ $ mergeProps = arr ($ props )
111+ ->filter (fn ($ prop ) => $ prop instanceof MergeableProp && $ prop ->shouldMerge )
112+ ->filter (
113+ fn ($ _ , $ key ) => ! $ resetProps ->contains ($ key )
114+ )
115+ ->keys ();
116+
117+ return $ mergeProps ->toArray ();
118+ }
119+
120+ public static function evaluateProps (array $ props , Request $ request , bool $ unpackDotProps = true ): array
99121 {
100122 foreach ($ props as $ key => $ value ) {
101123 if ($ value instanceof Closure) {
@@ -110,8 +132,12 @@ public function evaluateProps(array $props, Request $request, bool $unpackDotPro
110132 $ value = $ value ();
111133 }
112134
135+ if ($ value instanceof ArrayHelper) {
136+ $ value = $ value ->toArray ();
137+ }
138+
113139 if (is_array ($ value )) {
114- $ value = $ this -> evaluateProps ($ value , $ request , false );
140+ $ value = self :: evaluateProps ($ value , $ request , false );
115141 }
116142
117143 if ($ unpackDotProps && str_contains ($ key , '. ' )) {
0 commit comments