Skip to content

Commit 53fc2be

Browse files
authored
Merge pull request #165 from rahulhaque/generator-command-fix
Fixes some issues and confusion of datatable generator command
2 parents 95b59dc + 77ead86 commit 53fc2be

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

src/Generators/DataTablesHtmlCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ protected function qualifyClass($name): string
8686
$name = str_replace('/', '\\', $name);
8787
}
8888

89-
if (! Str::contains(Str::lower($name), 'datatable')) {
89+
if (! Str::contains(Str::lower($name), 'datatablehtml')) {
9090
$name .= 'DataTableHtml';
91+
} else {
92+
$name = preg_replace('#datatablehtml$#i', 'DataTableHtml', $name);
9193
}
9294

9395
return $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name;

src/Generators/DataTablesMakeCommand.php

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function handle()
4848
$dom = config('datatables-buttons.generator.dom', 'Bfrtip');
4949

5050
$this->call('datatables:html', [
51-
'name' => $this->getNameInput(),
51+
'name' => $this->prepareHtmlBuilderName($this->getNameInput()),
5252
'--columns' => $this->option('columns') ?: $columns,
5353
'--buttons' => $this->option('buttons') ?: $buttons,
5454
'--dom' => $this->option('dom') ?: $dom,
@@ -82,6 +82,26 @@ protected function buildClass($name)
8282
return $stub;
8383
}
8484

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+
85105
/**
86106
* Replace the filename.
87107
*
@@ -90,11 +110,7 @@ protected function buildClass($name)
90110
*/
91111
protected function replaceFilename(string &$stub): static
92112
{
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);
98114

99115
return $this;
100116
}
@@ -107,9 +123,7 @@ protected function replaceFilename(string &$stub): static
107123
*/
108124
protected function replaceAction(string &$stub): static
109125
{
110-
$stub = str_replace(
111-
'DummyAction', $this->getAction(), $stub
112-
);
126+
$stub = str_replace('DummyAction', $this->getAction(), $stub);
113127

114128
return $this;
115129
}
@@ -128,7 +142,7 @@ protected function getAction(): string
128142
return $action;
129143
}
130144

131-
return Str::lower($this->getNameInput()).'.action';
145+
return Str::lower($this->prepareModelName()).'.action';
132146
}
133147

134148
/**
@@ -139,9 +153,7 @@ protected function getAction(): string
139153
*/
140154
protected function replaceTableId(string &$stub): static
141155
{
142-
$stub = str_replace(
143-
'DummyTableId', Str::lower($this->getNameInput()).'-table', $stub
144-
);
156+
$stub = str_replace('DummyTableId', Str::lower($this->prepareModelName()).'-table', $stub);
145157

146158
return $this;
147159
}
@@ -170,9 +182,7 @@ protected function replaceDOM(string &$stub): static
170182
*/
171183
protected function replaceButtons(string &$stub): static
172184
{
173-
$stub = str_replace(
174-
'DummyButtons', $this->getButtons(), $stub
175-
);
185+
$stub = str_replace('DummyButtons', $this->getButtons(), $stub);
176186

177187
return $this;
178188
}
@@ -234,9 +244,7 @@ protected function parseButtons(string $definition, int $indentation = 24): stri
234244
*/
235245
protected function replaceColumns(string &$stub): static
236246
{
237-
$stub = str_replace(
238-
'DummyColumns', $this->getColumns(), $stub
239-
);
247+
$stub = str_replace('DummyColumns', $this->getColumns(), $stub);
240248

241249
return $this;
242250
}
@@ -326,6 +334,8 @@ protected function qualifyClass($name)
326334

327335
if (! Str::contains(Str::lower($name), 'datatable')) {
328336
$name .= 'DataTable';
337+
} else {
338+
$name = preg_replace('#datatable$#i', 'DataTable', $name);
329339
}
330340

331341
return $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name;
@@ -364,27 +374,20 @@ protected function replaceModel(string &$stub): static
364374
*/
365375
protected function getModel(): string
366376
{
367-
$name = $this->getNameInput();
368-
$rootNamespace = $this->laravel->getNamespace();
369-
370377
/** @var string $modelFromOption */
371378
$modelFromOption = $this->option('model');
379+
$modelNamespaceFromOption = $this->option('model-namespace')
380+
? $this->option('model-namespace')
381+
: config('datatables-buttons.namespace.model');
372382

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();
379385

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\\';
383388
}
384389

385-
return $model
386-
? $rootNamespace.'\\'.($modelNamespace ? $modelNamespace.'\\' : '').Str::singular($name)
387-
: $rootNamespace.'\\User';
390+
return $modelNamespace.'\\'.Str::singular($name);
388391
}
389392

390393
/**
@@ -395,9 +398,7 @@ protected function getModel(): string
395398
*/
396399
protected function replaceModelImport(string &$stub): static
397400
{
398-
$stub = str_replace(
399-
'DummyModel', str_replace('\\\\', '\\', $this->getModel()), $stub
400-
);
401+
$stub = str_replace('DummyModel', str_replace('\\\\', '\\', $this->getModel()), $stub);
401402

402403
return $this;
403404
}

0 commit comments

Comments
 (0)