@@ -59,52 +59,77 @@ public function getWidgets()
59
59
public function addView (View $ view )
60
60
{
61
61
$ name = $ view ->getName ();
62
- $ path = $ view -> getPath () ;
62
+ $ type = null ;
63
63
$ data = $ view ->getData ();
64
- $ shortPath = '' ;
65
- $ type = '' ;
64
+ $ path = $ view ->getPath ();
66
65
66
+ if (class_exists ('\Inertia\Inertia ' )) {
67
+ list ($ name , $ type , $ data , $ path ) = $ this ->getInertiaView ($ name , $ data , $ path );
68
+ }
67
69
68
- // Prevent duplicates
69
- $ hash = $ type . $ path . $ name . $ this ->collect_data ? implode (array_keys ($ view ->getData ())) : '' ;
70
+ if (is_object ($ path )) {
71
+ $ type = get_class ($ view );
72
+ $ path = null ;
73
+ }
70
74
71
- if (class_exists ('\Inertia\Inertia ' ) && isset ($ data ['page ' ]['props ' ], $ data ['page ' ]['component ' ])) {
72
- $ name = $ data ['page ' ]['component ' ];
73
- $ data = $ data ['page ' ]['props ' ];
75
+ if ($ path ) {
76
+ if (!$ type ) {
77
+ if (substr ($ path , -10 ) == '.blade.php ' ) {
78
+ $ type = 'blade ' ;
79
+ } else {
80
+ $ type = pathinfo ($ path , PATHINFO_EXTENSION );
81
+ }
82
+ }
74
83
75
- if (!@file_exists ($ path = resource_path ('js/Pages/ ' . $ name . '.js ' ))) {
76
- if (!@file_exists ($ path = resource_path ('js/Pages/ ' . $ name . '.vue ' ))) {
77
- if (!@file_exists ($ path = resource_path ('js/Pages/ ' . $ name . '.svelte ' ))) {
78
- $ path = $ view ->getPath ();
79
- }
84
+ foreach ($ this ->exclude_paths as $ excludePath ) {
85
+ if (str_starts_with ($ path , $ excludePath )) {
86
+ return ;
80
87
}
81
- } else {
82
- $ type = 'react ' ;
83
88
}
84
89
}
85
90
86
- if ($ path && is_string ($ path )) {
87
- $ path = $ this ->normalizeFilePath ($ path );
88
- $ shortPath = ltrim (str_replace (base_path (), '' , $ path ), '/ ' );
89
- } elseif (is_object ($ path )) {
90
- $ type = get_class ($ view );
91
- $ path = '' ;
91
+ $ this ->addTemplate ($ name , $ data , $ type , $ path );
92
+ }
93
+
94
+ private function getInertiaView (string $ name , array $ data , ?string $ path )
95
+ {
96
+ if (isset ($ data ['page ' ]) && is_array ($ data ['page ' ])) {
97
+ $ data = $ data ['page ' ];
92
98
}
93
99
94
- if ($ path && !$ type ) {
95
- if (substr ($ path , -10 ) == '.blade.php ' ) {
96
- $ type = 'blade ' ;
97
- } else {
100
+ if (isset ($ data ['props ' ], $ data ['component ' ])) {
101
+ $ name = $ data ['component ' ];
102
+ $ data = $ data ['props ' ];
103
+
104
+ if ($ files = glob (resource_path ('js/Pages/ ' . $ name . '.* ' ))) {
105
+ $ path = $ files [0 ];
98
106
$ type = pathinfo ($ path , PATHINFO_EXTENSION );
107
+
108
+ if (in_array ($ type , ['js ' , 'jsx ' ])) {
109
+ $ type = 'react ' ;
110
+ }
99
111
}
100
112
}
101
113
102
- foreach ($ this ->exclude_paths as $ excludePath ) {
103
- if (str_starts_with ($ path , $ excludePath )) {
104
- return ;
105
- }
114
+ return [$ name , $ type ?? '' , $ data , $ path ];
115
+ }
116
+
117
+ public function addInertiaAjaxView (array $ data )
118
+ {
119
+ list ($ name , $ type , $ data , $ path ) = $ this ->getInertiaView ('' , $ data , '' );
120
+
121
+ if (! $ name ) {
122
+ return ;
106
123
}
107
124
125
+ $ this ->addTemplate ($ name , $ data , $ type , $ path );
126
+ }
127
+
128
+ private function addTemplate (string $ name , array $ data , ?string $ type , ?string $ path )
129
+ {
130
+ // Prevent duplicates
131
+ $ hash = $ type . $ path . $ name . ($ this ->collect_data ? implode (array_keys ($ data )) : '' );
132
+
108
133
if ($ this ->collect_data === 'keys ' ) {
109
134
$ params = array_keys ($ data );
110
135
} elseif ($ this ->collect_data ) {
@@ -125,8 +150,8 @@ public function addView(View $view)
125
150
'hash ' => $ hash ,
126
151
];
127
152
128
- if ($ view -> getPath () && $ this ->getXdebugLinkTemplate ()) {
129
- $ template ['xdebug_link ' ] = $ this ->getXdebugLink (realpath ( $ view -> getPath ()) );
153
+ if ($ path && $ this ->getXdebugLinkTemplate ()) {
154
+ $ template ['xdebug_link ' ] = $ this ->getXdebugLink ($ path );
130
155
}
131
156
132
157
$ this ->templates [] = $ template ;
0 commit comments