@@ -34,17 +34,52 @@ public function handle(): int
34
34
$ modelFound = null ;
35
35
$ indexedModel = null ;
36
36
$ namespaces = config ('elasticlens.namespaces ' );
37
+ $ paths = config ('elasticlens.index_paths ' );
38
+
39
+ $ config = [
40
+ 'model ' => [
41
+ 'name ' => '' ,
42
+ 'namespace ' => '' ,
43
+ 'full ' => '' ,
44
+ ],
45
+ 'index ' => [
46
+ 'name ' => '' ,
47
+ 'namespace ' => '' ,
48
+ 'full ' => '' ,
49
+ 'path ' => '' ,
50
+ ],
51
+
52
+ ];
53
+
37
54
$ notFound = [];
38
55
foreach ($ namespaces as $ modelNamespace => $ indexNameSpace ) {
39
56
$ modelCheck = $ modelNamespace .'\\' .$ model ;
40
57
if ($ this ->class_exists_case_sensitive ($ modelCheck )) {
41
58
$ modelFound = $ modelCheck ;
42
- $ indexedModel = $ indexNameSpace .'\\Indexed ' .$ model ;
59
+ $ config ['model ' ]['name ' ] = $ model ;
60
+ $ config ['model ' ]['namespace ' ] = $ modelNamespace ;
61
+ $ config ['model ' ]['full ' ] = $ modelCheck ;
62
+ $ config ['index ' ]['name ' ] = 'Indexed ' .$ model ;
63
+ $ config ['index ' ]['namespace ' ] = $ indexNameSpace ;
64
+ $ config ['index ' ]['full ' ] = $ indexNameSpace .'\\' .$ config ['index ' ]['name ' ];
65
+
66
+ $ path = array_search ($ indexNameSpace , $ paths );
67
+ if (! $ path ) {
68
+ $ this ->omni ->statusError ('ERROR ' , 'Path for namespace ' .$ indexNameSpace .' not found ' , [
69
+ 'Namespace found: ' .$ indexNameSpace ,
70
+ 'Check config("elasticlens.index_paths") for the correct {path} => \'' .$ indexNameSpace .'\'' ,
71
+ ]);
72
+ $ this ->newLine ();
73
+
74
+ return self ::FAILURE ;
75
+ }
76
+ $ config ['index ' ]['path ' ] = $ path ;
43
77
break ;
44
78
} else {
45
79
$ notFound [] = $ modelCheck ;
46
80
}
47
81
}
82
+
48
83
if (! $ modelFound ) {
49
84
foreach ($ notFound as $ modelCheck ) {
50
85
$ this ->omni ->statusError ('ERROR ' , 'Base Model ( ' .$ model .') was not found at: ' .$ modelCheck );
@@ -53,28 +88,26 @@ public function handle(): int
53
88
54
89
return self ::FAILURE ;
55
90
}
56
- if ($ this ->class_exists_case_sensitive ($ indexedModel )) {
57
- $ this ->omni ->statusError ('ERROR ' , 'Indexed Model (for ' .$ model .' Model) already exists at: ' .$ indexedModel );
91
+ if ($ this ->class_exists_case_sensitive ($ config [ ' index ' ][ ' full ' ] )) {
92
+ $ this ->omni ->statusError ('ERROR ' , 'Indexed Model (for ' .$ model .' Model) already exists at: ' .$ config [ ' index ' ][ ' full ' ] );
58
93
59
94
return self ::FAILURE ;
60
95
}
61
- // Set the fully qualified class name for the new indexed model
62
- $ name = $ this ->qualifyClass ($ indexedModel );
63
96
64
- // Get the destination path for the generated file
65
- $ path = $ this ->getPath ($ name );
97
+ $ path = $ config ['index ' ]['path ' ].$ config ['index ' ]['name ' ];
66
98
99
+ $ finalPath = $ this ->getPath ($ path );
67
100
// Make sure the directory exists
68
- $ this ->makeDirectory ($ path );
101
+ $ this ->makeDirectory ($ finalPath );
69
102
70
103
// Get the stub file contents
71
104
$ stub = $ this ->files ->get ($ this ->getStub ());
72
-
73
105
// Replace the stub variables
74
- $ stub = $ this ->replaceNamespace ($ stub , $ name )->replaceClass ($ stub , $ name );
106
+ $ stub = $ this ->replaceNamespaceCustom ($ stub , $ config ['model ' ]['namespace ' ]);
107
+ $ stub = $ this ->replaceModel ($ stub , $ config ['model ' ]['name ' ]);
75
108
76
109
// Write the file to disk
77
- $ this ->files ->put ($ path , $ stub );
110
+ $ this ->files ->put ($ finalPath , $ stub );
78
111
79
112
$ this ->omni ->statusSuccess ('SUCCESS ' , 'Indexed Model (for ' .$ model .' Model) created at: ' .$ indexedModel );
80
113
$ this ->omni ->statusInfo ('1 ' , 'Add the Indexable trait to your <span class="text-sky-500"> ' .$ model .'</span> model ' );
@@ -98,11 +131,21 @@ protected function getStub(): string
98
131
return $ stubPath ;
99
132
}
100
133
101
- public function replaceClass ( $ stub , $ name ): string
134
+ protected function getPath ( $ name )
102
135
{
103
- $ stub = parent ::replaceClass ($ stub , $ name );
136
+ $ name = Str::replaceFirst ($ this ->rootNamespace (), '' , $ name );
137
+
138
+ return $ this ->laravel ['path.base ' ].'/ ' .str_replace ('\\' , '/ ' , $ name ).'.php ' ;
139
+ }
104
140
105
- return str_replace ('{{ model }} ' , $ this ->argument ('model ' ), $ stub );
141
+ public function replaceNamespaceCustom ($ stub , $ namespace ): string
142
+ {
143
+ return str_replace ('{{ namespace }} ' , $ namespace , $ stub );
144
+ }
145
+
146
+ public function replaceModel ($ stub , $ name ): string
147
+ {
148
+ return str_replace ('{{ model }} ' , $ name , $ stub );
106
149
}
107
150
108
151
public function class_exists_case_sensitive (string $ class_name ): bool
0 commit comments