1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Generators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Console\GeneratorCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
|
9
|
|
|
class DataTablesMakeCommand extends GeneratorCommand |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name and signature of the console command. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $signature = 'datatables:make |
17
|
|
|
{name : The name of the datatable.} |
18
|
|
|
{--model= : The name of the model to be used.} |
19
|
|
|
{--model-namespace= : The namespace of the model to be used.} |
20
|
|
|
{--action= : The path of the action view.} |
21
|
|
|
{--table= : Scaffold columns from the table.} |
22
|
|
|
{--dom= : The dom of the datatable.} |
23
|
|
|
{--buttons= : The buttons of the datatable.} |
24
|
|
|
{--columns= : The columns of the datatable.}'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The console command description. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $description = 'Create a new dataTable service class.'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The type of class being generated. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $type = 'DataTable'; |
39
|
|
|
|
40
|
|
|
public function handle() |
41
|
|
|
{ |
42
|
|
|
parent::handle(); |
43
|
|
|
|
44
|
|
|
$this->call('datatables:html', [ |
45
|
|
|
'name' => $this->getNameInput(), |
46
|
|
|
'--columns' => $this->option('columns') ?: $this->laravel['config']->get( |
47
|
|
|
'datatables-buttons.generator.columns', |
48
|
|
|
'id,add your columns,created_at,updated_at' |
49
|
|
|
), |
50
|
|
|
'--buttons' => $this->option('buttons') ?: $this->laravel['config']->get( |
51
|
|
|
'datatables-buttons.generator.buttons', |
52
|
|
|
'create,export,print,reset,reload' |
53
|
|
|
), |
54
|
|
|
'--dom' => $this->option('dom') ?: $this->laravel['config']->get( |
55
|
|
|
'datatables-buttons.generator.dom', |
56
|
|
|
'Bfrtip' |
57
|
|
|
), |
58
|
|
|
'--table' => $this->option('table'), |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Build the class with the given name. |
64
|
|
|
* |
65
|
|
|
* @param string $name |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
protected function buildClass($name) |
69
|
|
|
{ |
70
|
|
|
$stub = parent::buildClass($name); |
71
|
|
|
|
72
|
|
|
return $this->replaceModelImport($stub) |
73
|
|
|
->replaceModel($stub) |
74
|
|
|
->replaceBuilder($stub) |
75
|
|
|
->replaceAction($stub) |
76
|
|
|
->replaceFilename($stub); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Replace the filename. |
81
|
|
|
* |
82
|
|
|
* @param string $stub |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
protected function replaceFilename(&$stub) |
86
|
|
|
{ |
87
|
|
|
$stub = str_replace( |
88
|
|
|
'DummyFilename', preg_replace('#datatable$#i', '', $this->getNameInput()), $stub |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return $stub; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Replace the action. |
96
|
|
|
* |
97
|
|
|
* @param string $stub |
98
|
|
|
* @return \Yajra\DataTables\Generators\DataTablesMakeCommand |
99
|
|
|
*/ |
100
|
|
|
protected function replaceAction(&$stub) |
101
|
|
|
{ |
102
|
|
|
$stub = str_replace( |
103
|
|
|
'DummyAction', $this->getAction(), $stub |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set the action view to be used. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
protected function getAction() |
115
|
|
|
{ |
116
|
|
|
return $this->option('action') ? $this->option('action') : Str::lower($this->getNameInput()) . '.action'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Replace builder name. |
121
|
|
|
* |
122
|
|
|
* @param string $stub |
123
|
|
|
* @return \Yajra\DataTables\Generators\DataTablesMakeCommand |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
protected function replaceBuilder(&$stub) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$name = $this->qualifyClass($this->getNameInput()); |
128
|
|
|
$class = str_replace($this->getNamespace($name) . '\\', '', $name); |
129
|
|
|
|
130
|
|
|
$stub = str_replace('DummyBuilder', $class . 'Html', $stub); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Parse the name and format according to the root namespace. |
137
|
|
|
* |
138
|
|
|
* @param string $name |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
protected function qualifyClass($name) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
144
|
|
|
|
145
|
|
|
if (Str::startsWith($name, $rootNamespace)) { |
146
|
|
|
return $name; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (Str::contains($name, '/')) { |
150
|
|
|
$name = str_replace('/', '\\', $name); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (! Str::contains(Str::lower($name), 'datatable')) { |
154
|
|
|
$name .= 'DataTable'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the default namespace for the class. |
162
|
|
|
* |
163
|
|
|
* @param string $rootNamespace |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
protected function getDefaultNamespace($rootNamespace) |
167
|
|
|
{ |
168
|
|
|
return $rootNamespace . '\\' . $this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Replace model name. |
173
|
|
|
* |
174
|
|
|
* @param string $stub |
175
|
|
|
* @return \Yajra\DataTables\Generators\DataTablesMakeCommand |
176
|
|
|
*/ |
177
|
|
|
protected function replaceModel(&$stub) |
178
|
|
|
{ |
179
|
|
|
$model = explode('\\', $this->getModel()); |
180
|
|
|
$model = array_pop($model); |
181
|
|
|
$stub = str_replace('ModelName', $model, $stub); |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get model name to use. |
188
|
|
|
*/ |
189
|
|
|
protected function getModel() |
190
|
|
|
{ |
191
|
|
|
$name = $this->getNameInput(); |
192
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
193
|
|
|
$model = $this->option('model') == '' || $this->option('model-namespace'); |
194
|
|
|
$modelNamespace = $this->option('model-namespace') ? $this->option('model-namespace') : $this->laravel['config']->get('datatables-buttons.namespace.model'); |
195
|
|
|
|
196
|
|
|
if ($this->option('model') != '') { |
197
|
|
|
return $this->option('model'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $model |
201
|
|
|
? $rootNamespace . '\\' . ($modelNamespace ? $modelNamespace . '\\' : '') . Str::singular($name) |
202
|
|
|
: $rootNamespace . '\\User'; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Replace model import. |
207
|
|
|
* |
208
|
|
|
* @param string $stub |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
|
|
protected function replaceModelImport(&$stub) |
212
|
|
|
{ |
213
|
|
|
$stub = str_replace( |
214
|
|
|
'DummyModel', str_replace('\\\\', '\\', $this->getModel()), $stub |
215
|
|
|
); |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Get the stub file for the generator. |
222
|
|
|
* |
223
|
|
|
* @return string |
224
|
|
|
*/ |
225
|
|
View Code Duplication |
protected function getStub() |
|
|
|
|
226
|
|
|
{ |
227
|
|
|
$config = $this->laravel['config']; |
228
|
|
|
|
229
|
|
|
return $config->get('datatables-buttons.stub') |
230
|
|
|
? base_path() . $config->get('datatables-buttons.stub') . '/datatables.stub' |
231
|
|
|
: __DIR__ . '/stubs/datatables.stub'; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get the console command options. |
236
|
|
|
* |
237
|
|
|
* @return array |
238
|
|
|
*/ |
239
|
|
View Code Duplication |
protected function getOptions() |
|
|
|
|
240
|
|
|
{ |
241
|
|
|
return [ |
242
|
|
|
['model', null, InputOption::VALUE_NONE, 'Use the provided name as the model.', null], |
243
|
|
|
['action', null, InputOption::VALUE_OPTIONAL, 'Path to action column template.', null], |
244
|
|
|
['columns', null, InputOption::VALUE_OPTIONAL, 'Use the provided columns.', null], |
245
|
|
|
]; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.