|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Yajra\DataTables\Generators; |
| 4 | + |
| 5 | +use Illuminate\Support\Str; |
| 6 | + |
| 7 | +class DataTablesHtmlCommand extends DataTablesMakeCommand |
| 8 | +{ |
| 9 | + /** |
| 10 | + * The name and signature of the console command. |
| 11 | + * |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + protected $signature = 'datatables:html |
| 15 | + {name : The name of the DataTable html.} |
| 16 | + {--dom= : The dom of the DataTable.} |
| 17 | + {--buttons= : The buttons of the DataTable.} |
| 18 | + {--table= : Scaffold columns from the table.} |
| 19 | + {--builder : Ignore, added to work with parent generator.} |
| 20 | + {--columns= : The columns of the DataTable.}'; |
| 21 | + |
| 22 | + /** |
| 23 | + * The console command description. |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + protected $description = 'Create a new DataTable html class.'; |
| 28 | + |
| 29 | + /** |
| 30 | + * The type of class being generated. |
| 31 | + * |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected $type = 'DataTableHtml'; |
| 35 | + |
| 36 | + /** |
| 37 | + * Build the class with the given name. |
| 38 | + * |
| 39 | + * @param string $name |
| 40 | + * @return string |
| 41 | + */ |
| 42 | + protected function buildClass($name) |
| 43 | + { |
| 44 | + $stub = $this->files->get($this->getStub()); |
| 45 | + |
| 46 | + $stub = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); |
| 47 | + |
| 48 | + $this->replaceBuilder($stub) |
| 49 | + ->replaceColumns($stub) |
| 50 | + ->replaceButtons($stub) |
| 51 | + ->replaceDOM($stub) |
| 52 | + ->replaceTableId($stub); |
| 53 | + |
| 54 | + return $stub; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get the stub file for the generator. |
| 59 | + * |
| 60 | + * @return string |
| 61 | + */ |
| 62 | + protected function getStub() |
| 63 | + { |
| 64 | + $config = $this->laravel['config']; |
| 65 | + |
| 66 | + return $config->get('datatables-buttons.stub') |
| 67 | + ? base_path() . $config->get('datatables-buttons.stub') . '/html.stub' |
| 68 | + : __DIR__ . '/stubs/html.stub'; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Parse the name and format according to the root namespace. |
| 73 | + * |
| 74 | + * @param string $name |
| 75 | + * @return string |
| 76 | + */ |
| 77 | + protected function qualifyClass($name) |
| 78 | + { |
| 79 | + $rootNamespace = $this->laravel->getNamespace(); |
| 80 | + |
| 81 | + if (Str::startsWith($name, $rootNamespace)) { |
| 82 | + return $name; |
| 83 | + } |
| 84 | + |
| 85 | + if (Str::contains($name, '/')) { |
| 86 | + $name = str_replace('/', '\\', $name); |
| 87 | + } |
| 88 | + |
| 89 | + if (! Str::contains(Str::lower($name), 'datatable')) { |
| 90 | + $name .= 'DataTableHtml'; |
| 91 | + } |
| 92 | + |
| 93 | + return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name; |
| 94 | + } |
| 95 | +} |
0 commit comments