@@ -59,52 +59,77 @@ public function getWidgets()
5959 public function addView (View $ view )
6060 {
6161 $ name = $ view ->getName ();
62- $ path = $ view -> getPath () ;
62+ $ type = null ;
6363 $ data = $ view ->getData ();
64- $ shortPath = '' ;
65- $ type = '' ;
64+ $ path = $ view ->getPath ();
6665
66+ if (class_exists ('\Inertia\Inertia ' )) {
67+ list ($ name , $ type , $ data , $ path ) = $ this ->getInertiaView ($ name , $ data , $ path );
68+ }
6769
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+ }
7074
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+ }
7483
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 ;
8087 }
81- } else {
82- $ type = 'react ' ;
8388 }
8489 }
8590
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 ' ];
9298 }
9399
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 ];
98106 $ type = pathinfo ($ path , PATHINFO_EXTENSION );
107+
108+ if (in_array ($ type , ['js ' , 'jsx ' ])) {
109+ $ type = 'react ' ;
110+ }
99111 }
100112 }
101113
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 ;
106123 }
107124
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+
108133 if ($ this ->collect_data === 'keys ' ) {
109134 $ params = array_keys ($ data );
110135 } elseif ($ this ->collect_data ) {
@@ -125,8 +150,8 @@ public function addView(View $view)
125150 'hash ' => $ hash ,
126151 ];
127152
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 );
130155 }
131156
132157 $ this ->templates [] = $ template ;
0 commit comments