|
7 | 7 | use Closure; |
8 | 8 | use NeoIsRecursive\Inertia\Contracts\CallableProp; |
9 | 9 | use NeoIsRecursive\Inertia\Contracts\MergeableProp; |
| 10 | +use NeoIsRecursive\Inertia\PageData; |
10 | 11 | use NeoIsRecursive\Inertia\Props\AlwaysProp; |
11 | 12 | use NeoIsRecursive\Inertia\Props\DeferProp; |
12 | 13 | use NeoIsRecursive\Inertia\Props\LazyProp; |
@@ -34,45 +35,38 @@ public function __construct( |
34 | 35 | readonly bool $clearHistory = false, |
35 | 36 | readonly bool $encryptHistory = false, |
36 | 37 | ) { |
37 | | - $deferredProps = self::resolvePropKeysThatShouldDefer( |
38 | | - props: $props, |
39 | | - request: $request, |
| 38 | + $pageData = new PageData( |
40 | 39 | component: $component, |
41 | | - ); |
42 | | - |
43 | | - $mergeProps = self::resolvePropKeysThatShouldMerge( |
44 | | - props: $props, |
45 | | - request: $request, |
46 | | - ); |
47 | | - |
48 | | - // Build page data immutably |
49 | | - $pageData = array_merge( |
50 | | - [ |
51 | | - 'component' => $component, |
52 | | - 'props' => self::composeProps( |
53 | | - props: $props, |
54 | | - request: $request, |
55 | | - component: $component, |
56 | | - ), |
57 | | - 'url' => $request->uri, |
58 | | - 'version' => $version, |
59 | | - 'clearHistory' => $clearHistory, |
60 | | - 'encryptHistory' => $encryptHistory, |
61 | | - ], |
62 | | - count($deferredProps) ? ['deferredProps' => $deferredProps] : [], |
63 | | - count($mergeProps) ? ['mergeProps' => $mergeProps] : [], |
| 40 | + props: self::composeProps( |
| 41 | + props: $props, |
| 42 | + request: $request, |
| 43 | + component: $component, |
| 44 | + ), |
| 45 | + url: $request->uri, |
| 46 | + version: $version, |
| 47 | + clearHistory: $clearHistory, |
| 48 | + encryptHistory: $encryptHistory, |
| 49 | + propKeysToDefer: self::resolvePropKeysThatShouldDefer( |
| 50 | + props: $props, |
| 51 | + request: $request, |
| 52 | + component: $component, |
| 53 | + ), |
| 54 | + propsKeysToMerge: self::resolvePropKeysThatShouldMerge( |
| 55 | + props: $props, |
| 56 | + request: $request, |
| 57 | + ), |
64 | 58 | ); |
65 | 59 |
|
66 | 60 | $this->body = $request->headers->has(Header::INERTIA) |
67 | 61 | ? (function () use ($pageData) { |
68 | 62 | // side effect to set Inertia header |
69 | 63 | $this->addHeader(Header::INERTIA, value: 'true'); |
70 | 64 |
|
71 | | - return $pageData; |
| 65 | + return $pageData->toArray(); |
72 | 66 | })() |
73 | 67 | : new InertiaBaseView( |
74 | | - view: $rootView, |
75 | | - pageData: $pageData, |
| 68 | + path: $rootView, |
| 69 | + page: $pageData, |
76 | 70 | ); |
77 | 71 | } |
78 | 72 |
|
@@ -128,36 +122,37 @@ private static function resolvePartialProps(Request $request, string $component, |
128 | 122 | return array_filter($filtered, static fn($key) => !in_array($key, $except, strict: true), ARRAY_FILTER_USE_KEY); |
129 | 123 | } |
130 | 124 |
|
131 | | - private static function resolvePropKeysThatShouldDefer(array $props, Request $request, string $component): array |
| 125 | + private static function resolvePropKeysThatShouldDefer(array $props, Request $request, string $component): ?array |
132 | 126 | { |
133 | 127 | if (static::isPartial($request, $component)) { |
134 | | - return []; |
| 128 | + return null; |
135 | 129 | } |
136 | 130 |
|
137 | | - return arr($props) |
138 | | - ->filter(function ($prop) { |
139 | | - return $prop instanceof DeferProp; |
140 | | - }) |
141 | | - ->map(fn(DeferProp $prop, string $key) => [ |
| 131 | + $propKeysToMerge = arr($props) |
| 132 | + ->filter(static fn($prop) => $prop instanceof DeferProp) |
| 133 | + ->map(static fn(DeferProp $prop, string $key) => [ |
142 | 134 | 'group' => $prop->group, |
143 | 135 | 'key' => $key, |
144 | 136 | ]) |
145 | | - ->groupBy(fn(array $prop) => $prop['group']) |
146 | | - ->map(fn(array $group) => arr($group)->pluck(value: 'key')->toArray()) |
147 | | - ->toArray(); |
| 137 | + ->groupBy(static fn(array $prop) => $prop['group']) |
| 138 | + ->map(static fn(array $group) => arr($group)->pluck(value: 'key')->toArray()); |
| 139 | + |
| 140 | + return $propKeysToMerge->isEmpty() ? null : $propKeysToMerge->toArray(); |
148 | 141 | } |
149 | 142 |
|
150 | | - private static function resolvePropKeysThatShouldMerge(array $props, Request $request): array |
| 143 | + private static function resolvePropKeysThatShouldMerge(array $props, Request $request): ?array |
151 | 144 | { |
152 | 145 | $resetProps = arr(explode( |
153 | 146 | separator: ',', |
154 | 147 | string: $request->headers->get(Header::RESET) ?? '', |
155 | 148 | )); |
156 | | - return arr($props) |
| 149 | + |
| 150 | + $propKeysToMerge = arr($props) |
157 | 151 | ->filter(fn($prop) => $prop instanceof MergeableProp && $prop->shouldMerge) |
158 | 152 | ->filter(fn($_, $key) => !$resetProps->contains($key)) |
159 | | - ->keys() |
160 | | - ->toArray(); |
| 153 | + ->keys(); |
| 154 | + |
| 155 | + return $propKeysToMerge->isEmpty() ? null : $propKeysToMerge->toArray(); |
161 | 156 | } |
162 | 157 |
|
163 | 158 | /** |
|
0 commit comments