@@ -48,7 +48,7 @@ public function handle()
48
48
$ dom = config ('datatables-buttons.generator.dom ' , 'Bfrtip ' );
49
49
50
50
$ this ->call ('datatables:html ' , [
51
- 'name ' => $ this ->getNameInput (),
51
+ 'name ' => $ this ->prepareHtmlBuilderName ( $ this -> getNameInput () ),
52
52
'--columns ' => $ this ->option ('columns ' ) ?: $ columns ,
53
53
'--buttons ' => $ this ->option ('buttons ' ) ?: $ buttons ,
54
54
'--dom ' => $ this ->option ('dom ' ) ?: $ dom ,
@@ -82,6 +82,26 @@ protected function buildClass($name)
82
82
return $ stub ;
83
83
}
84
84
85
+ /**
86
+ * Prepare html builder name from input.
87
+ *
88
+ * @return string
89
+ */
90
+ protected function prepareHtmlBuilderName (): string
91
+ {
92
+ return preg_replace ('#datatable$#i ' , '' , $ this ->getNameInput ());
93
+ }
94
+
95
+ /**
96
+ * Prepare model name from input.
97
+ *
98
+ * @return string
99
+ */
100
+ protected function prepareModelName (): string
101
+ {
102
+ return basename (preg_replace ('#datatable$#i ' , '' , $ this ->getNameInput ()));
103
+ }
104
+
85
105
/**
86
106
* Replace the filename.
87
107
*
@@ -90,11 +110,7 @@ protected function buildClass($name)
90
110
*/
91
111
protected function replaceFilename (string &$ stub ): static
92
112
{
93
- $ stub = str_replace (
94
- 'DummyFilename ' ,
95
- (string ) preg_replace ('#datatable$#i ' , '' , $ this ->getNameInput ()),
96
- $ stub
97
- );
113
+ $ stub = str_replace ('DummyFilename ' , $ this ->prepareModelName (), $ stub );
98
114
99
115
return $ this ;
100
116
}
@@ -107,9 +123,7 @@ protected function replaceFilename(string &$stub): static
107
123
*/
108
124
protected function replaceAction (string &$ stub ): static
109
125
{
110
- $ stub = str_replace (
111
- 'DummyAction ' , $ this ->getAction (), $ stub
112
- );
126
+ $ stub = str_replace ('DummyAction ' , $ this ->getAction (), $ stub );
113
127
114
128
return $ this ;
115
129
}
@@ -128,7 +142,7 @@ protected function getAction(): string
128
142
return $ action ;
129
143
}
130
144
131
- return Str::lower ($ this ->getNameInput ()).'.action ' ;
145
+ return Str::lower ($ this ->prepareModelName ()).'.action ' ;
132
146
}
133
147
134
148
/**
@@ -139,9 +153,7 @@ protected function getAction(): string
139
153
*/
140
154
protected function replaceTableId (string &$ stub ): static
141
155
{
142
- $ stub = str_replace (
143
- 'DummyTableId ' , Str::lower ($ this ->getNameInput ()).'-table ' , $ stub
144
- );
156
+ $ stub = str_replace ('DummyTableId ' , Str::lower ($ this ->prepareModelName ()).'-table ' , $ stub );
145
157
146
158
return $ this ;
147
159
}
@@ -170,9 +182,7 @@ protected function replaceDOM(string &$stub): static
170
182
*/
171
183
protected function replaceButtons (string &$ stub ): static
172
184
{
173
- $ stub = str_replace (
174
- 'DummyButtons ' , $ this ->getButtons (), $ stub
175
- );
185
+ $ stub = str_replace ('DummyButtons ' , $ this ->getButtons (), $ stub );
176
186
177
187
return $ this ;
178
188
}
@@ -234,9 +244,7 @@ protected function parseButtons(string $definition, int $indentation = 24): stri
234
244
*/
235
245
protected function replaceColumns (string &$ stub ): static
236
246
{
237
- $ stub = str_replace (
238
- 'DummyColumns ' , $ this ->getColumns (), $ stub
239
- );
247
+ $ stub = str_replace ('DummyColumns ' , $ this ->getColumns (), $ stub );
240
248
241
249
return $ this ;
242
250
}
@@ -326,6 +334,8 @@ protected function qualifyClass($name)
326
334
327
335
if (! Str::contains (Str::lower ($ name ), 'datatable ' )) {
328
336
$ name .= 'DataTable ' ;
337
+ } else {
338
+ $ name = preg_replace ('#datatable$#i ' , 'DataTable ' , $ name );
329
339
}
330
340
331
341
return $ this ->getDefaultNamespace (trim ($ rootNamespace , '\\' )).'\\' .$ name ;
@@ -364,27 +374,20 @@ protected function replaceModel(string &$stub): static
364
374
*/
365
375
protected function getModel (): string
366
376
{
367
- $ name = $ this ->getNameInput ();
368
- $ rootNamespace = $ this ->laravel ->getNamespace ();
369
-
370
377
/** @var string $modelFromOption */
371
378
$ modelFromOption = $ this ->option ('model ' );
379
+ $ modelNamespaceFromOption = $ this ->option ('model-namespace ' )
380
+ ? $ this ->option ('model-namespace ' )
381
+ : config ('datatables-buttons.namespace.model ' );
372
382
373
- $ model = $ modelFromOption == '' || $ this ->option ('model-namespace ' );
374
- $ modelNamespace = $ this ->option ('model-namespace ' ) ? $ this ->option ('model-namespace ' ) : config ('datatables-buttons.namespace.model ' );
375
-
376
- if ($ modelFromOption ) {
377
- return $ modelFromOption ;
378
- }
383
+ $ name = $ modelFromOption ?: $ this ->prepareModelName ();
384
+ $ modelNamespace = $ modelNamespaceFromOption ?: $ this ->laravel ->getNamespace ();
379
385
380
- // check if model namespace is not set in command and Models directory already exists then use that directory in namespace.
381
- if ($ modelNamespace == '' ) {
382
- $ modelNamespace = is_dir (app_path ('Models ' )) ? 'Models ' : $ rootNamespace ;
386
+ if (empty ($ modelNamespaceFromOption ) && is_dir (app_path ('Models ' ))) {
387
+ $ modelNamespace = $ modelNamespace .'\\Models \\' ;
383
388
}
384
389
385
- return $ model
386
- ? $ rootNamespace .'\\' .($ modelNamespace ? $ modelNamespace .'\\' : '' ).Str::singular ($ name )
387
- : $ rootNamespace .'\\User ' ;
390
+ return $ modelNamespace .'\\' .Str::singular ($ name );
388
391
}
389
392
390
393
/**
@@ -395,9 +398,7 @@ protected function getModel(): string
395
398
*/
396
399
protected function replaceModelImport (string &$ stub ): static
397
400
{
398
- $ stub = str_replace (
399
- 'DummyModel ' , str_replace ('\\\\' , '\\' , $ this ->getModel ()), $ stub
400
- );
401
+ $ stub = str_replace ('DummyModel ' , str_replace ('\\\\' , '\\' , $ this ->getModel ()), $ stub );
401
402
402
403
return $ this ;
403
404
}
0 commit comments