Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Generators/DataTablesHtmlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ protected function qualifyClass($name): string
$name = str_replace('/', '\\', $name);
}

if (! Str::contains(Str::lower($name), 'datatable')) {
if (! Str::contains(Str::lower($name), 'datatablehtml')) {
$name .= 'DataTableHtml';
} else {
$name = preg_replace('#datatablehtml$#i', 'DataTableHtml', $name);
}

return $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name;
Expand Down
75 changes: 38 additions & 37 deletions src/Generators/DataTablesMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function handle()
$dom = config('datatables-buttons.generator.dom', 'Bfrtip');

$this->call('datatables:html', [
'name' => $this->getNameInput(),
'name' => $this->prepareHtmlBuilderName($this->getNameInput()),
'--columns' => $this->option('columns') ?: $columns,
'--buttons' => $this->option('buttons') ?: $buttons,
'--dom' => $this->option('dom') ?: $dom,
Expand Down Expand Up @@ -82,6 +82,26 @@ protected function buildClass($name)
return $stub;
}

/**
* Prepare html builder name from input.
*
* @return string
*/
protected function prepareHtmlBuilderName(): string
{
return preg_replace('#datatable$#i', '', $this->getNameInput());
}

/**
* Prepare model name from input.
*
* @return string
*/
protected function prepareModelName(): string
{
return basename(preg_replace('#datatable$#i', '', $this->getNameInput()));
}

/**
* Replace the filename.
*
Expand All @@ -90,11 +110,7 @@ protected function buildClass($name)
*/
protected function replaceFilename(string &$stub): static
{
$stub = str_replace(
'DummyFilename',
(string) preg_replace('#datatable$#i', '', $this->getNameInput()),
$stub
);
$stub = str_replace('DummyFilename', $this->prepareModelName(), $stub);

return $this;
}
Expand All @@ -107,9 +123,7 @@ protected function replaceFilename(string &$stub): static
*/
protected function replaceAction(string &$stub): static
{
$stub = str_replace(
'DummyAction', $this->getAction(), $stub
);
$stub = str_replace('DummyAction', $this->getAction(), $stub);

return $this;
}
Expand All @@ -128,7 +142,7 @@ protected function getAction(): string
return $action;
}

return Str::lower($this->getNameInput()).'.action';
return Str::lower($this->prepareModelName()).'.action';
}

/**
Expand All @@ -139,9 +153,7 @@ protected function getAction(): string
*/
protected function replaceTableId(string &$stub): static
{
$stub = str_replace(
'DummyTableId', Str::lower($this->getNameInput()).'-table', $stub
);
$stub = str_replace('DummyTableId', Str::lower($this->prepareModelName()).'-table', $stub);

return $this;
}
Expand Down Expand Up @@ -170,9 +182,7 @@ protected function replaceDOM(string &$stub): static
*/
protected function replaceButtons(string &$stub): static
{
$stub = str_replace(
'DummyButtons', $this->getButtons(), $stub
);
$stub = str_replace('DummyButtons', $this->getButtons(), $stub);

return $this;
}
Expand Down Expand Up @@ -234,9 +244,7 @@ protected function parseButtons(string $definition, int $indentation = 24): stri
*/
protected function replaceColumns(string &$stub): static
{
$stub = str_replace(
'DummyColumns', $this->getColumns(), $stub
);
$stub = str_replace('DummyColumns', $this->getColumns(), $stub);

return $this;
}
Expand Down Expand Up @@ -326,6 +334,8 @@ protected function qualifyClass($name)

if (! Str::contains(Str::lower($name), 'datatable')) {
$name .= 'DataTable';
} else {
$name = preg_replace('#datatable$#i', 'DataTable', $name);
}

return $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name;
Expand Down Expand Up @@ -364,27 +374,20 @@ protected function replaceModel(string &$stub): static
*/
protected function getModel(): string
{
$name = $this->getNameInput();
$rootNamespace = $this->laravel->getNamespace();

/** @var string $modelFromOption */
$modelFromOption = $this->option('model');
$modelNamespaceFromOption = $this->option('model-namespace')
? $this->option('model-namespace')
: config('datatables-buttons.namespace.model');

$model = $modelFromOption == '' || $this->option('model-namespace');
$modelNamespace = $this->option('model-namespace') ? $this->option('model-namespace') : config('datatables-buttons.namespace.model');

if ($modelFromOption) {
return $modelFromOption;
}
$name = $modelFromOption ?: $this->prepareModelName();
$modelNamespace = $modelNamespaceFromOption ?: $this->laravel->getNamespace();

// check if model namespace is not set in command and Models directory already exists then use that directory in namespace.
if ($modelNamespace == '') {
$modelNamespace = is_dir(app_path('Models')) ? 'Models' : $rootNamespace;
if (empty($modelNamespaceFromOption) && is_dir(app_path('Models'))) {
$modelNamespace = $modelNamespace.'\\Models\\';
}

return $model
? $rootNamespace.'\\'.($modelNamespace ? $modelNamespace.'\\' : '').Str::singular($name)
: $rootNamespace.'\\User';
return $modelNamespace.'\\'.Str::singular($name);
}

/**
Expand All @@ -395,9 +398,7 @@ protected function getModel(): string
*/
protected function replaceModelImport(string &$stub): static
{
$stub = str_replace(
'DummyModel', str_replace('\\\\', '\\', $this->getModel()), $stub
);
$stub = str_replace('DummyModel', str_replace('\\\\', '\\', $this->getModel()), $stub);

return $this;
}
Expand Down