From 77ead86d2d5f2e3d7980d338eb1d493118b19db3 Mon Sep 17 00:00:00 2001 From: Rahul Date: Mon, 14 Nov 2022 18:28:30 +0600 Subject: [PATCH] Fix datatable generator command --- src/Generators/DataTablesHtmlCommand.php | 4 +- src/Generators/DataTablesMakeCommand.php | 75 ++++++++++++------------ 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/Generators/DataTablesHtmlCommand.php b/src/Generators/DataTablesHtmlCommand.php index 15b3c60..983cf57 100644 --- a/src/Generators/DataTablesHtmlCommand.php +++ b/src/Generators/DataTablesHtmlCommand.php @@ -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; diff --git a/src/Generators/DataTablesMakeCommand.php b/src/Generators/DataTablesMakeCommand.php index 01524c9..a0ae5bb 100644 --- a/src/Generators/DataTablesMakeCommand.php +++ b/src/Generators/DataTablesMakeCommand.php @@ -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, @@ -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. * @@ -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; } @@ -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; } @@ -128,7 +142,7 @@ protected function getAction(): string return $action; } - return Str::lower($this->getNameInput()).'.action'; + return Str::lower($this->prepareModelName()).'.action'; } /** @@ -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; } @@ -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; } @@ -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; } @@ -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; @@ -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); } /** @@ -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; }