1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Generators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Facades\Schema; |
7
|
|
|
use Illuminate\Support\Str; |
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
|
|
|
{--builder : Extract html() to a Builder class.} |
23
|
|
|
{--dom= : The dom of the DataTable.} |
24
|
|
|
{--buttons= : The buttons of the DataTable.} |
25
|
|
|
{--columns= : The columns of the DataTable.}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Create a new DataTable service class.'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The type of class being generated. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $type = 'DataTable'; |
40
|
|
|
|
41
|
|
|
public function handle() |
42
|
|
|
{ |
43
|
|
|
parent::handle(); |
44
|
|
|
|
45
|
|
|
if ($this->option('builder')) { |
46
|
|
|
$this->call('datatables:html', [ |
47
|
|
|
'name' => $this->getNameInput(), |
48
|
|
|
'--columns' => $this->option('columns') ?: $this->laravel['config']->get( |
49
|
|
|
'datatables-buttons.generator.columns', |
50
|
|
|
'id,add your columns,created_at,updated_at' |
51
|
|
|
), |
52
|
|
|
'--buttons' => $this->option('buttons') ?: $this->laravel['config']->get( |
53
|
|
|
'datatables-buttons.generator.buttons', |
54
|
|
|
'create,export,print,reset,reload' |
55
|
|
|
), |
56
|
|
|
'--dom' => $this->option('dom') ?: $this->laravel['config']->get( |
57
|
|
|
'datatables-buttons.generator.dom', |
58
|
|
|
'Bfrtip' |
59
|
|
|
), |
60
|
|
|
'--table' => $this->option('table'), |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Build the class with the given name. |
67
|
|
|
* |
68
|
|
|
* @param string $name |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
protected function buildClass($name) |
72
|
|
|
{ |
73
|
|
|
$stub = parent::buildClass($name); |
74
|
|
|
|
75
|
|
|
$this->replaceModelImport($stub) |
76
|
|
|
->replaceModel($stub) |
77
|
|
|
->replaceBuilder($stub) |
78
|
|
|
->replaceColumns($stub) |
79
|
|
|
->replaceButtons($stub) |
80
|
|
|
->replaceDOM($stub) |
81
|
|
|
->replaceTableId($stub) |
82
|
|
|
->replaceAction($stub) |
83
|
|
|
->replaceFilename($stub); |
84
|
|
|
|
85
|
|
|
return $stub; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Replace the filename. |
90
|
|
|
* |
91
|
|
|
* @param string $stub |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
protected function replaceFilename(&$stub) |
95
|
|
|
{ |
96
|
|
|
$stub = str_replace( |
97
|
|
|
'DummyFilename', preg_replace('#datatable$#i', '', $this->getNameInput()), $stub |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Replace the action. |
105
|
|
|
* |
106
|
|
|
* @param string $stub |
107
|
|
|
* @return \Yajra\DataTables\Generators\DataTablesMakeCommand |
108
|
|
|
*/ |
109
|
|
|
protected function replaceAction(&$stub) |
110
|
|
|
{ |
111
|
|
|
$stub = str_replace( |
112
|
|
|
'DummyAction', $this->getAction(), $stub |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the action view to be used. |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
protected function getAction() |
124
|
|
|
{ |
125
|
|
|
return $this->option('action') ?: Str::lower($this->getNameInput()) . '.action'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Replace columns. |
130
|
|
|
* |
131
|
|
|
* @param string $stub |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
protected function replaceTableId(&$stub) |
135
|
|
|
{ |
136
|
|
|
$stub = str_replace( |
137
|
|
|
'DummyTableId', Str::lower($this->getNameInput()) . '-table', $stub |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Replace dom. |
145
|
|
|
* |
146
|
|
|
* @param string $stub |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
protected function replaceDOM(&$stub) |
150
|
|
|
{ |
151
|
|
|
$stub = str_replace( |
152
|
|
|
'DummyDOM', |
153
|
|
|
$this->option('dom') ?: $this->laravel['config']->get('datatables-buttons.generator.dom', 'Bfrtip'), |
154
|
|
|
$stub |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Replace buttons. |
162
|
|
|
* |
163
|
|
|
* @param string $stub |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
|
protected function replaceButtons(&$stub) |
167
|
|
|
{ |
168
|
|
|
$stub = str_replace( |
169
|
|
|
'DummyButtons', $this->getButtons(), $stub |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get the columns to be used. |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
protected function getButtons() |
181
|
|
|
{ |
182
|
|
|
if ($this->option('buttons') != '') { |
183
|
|
|
return $this->parseButtons($this->option('buttons')); |
184
|
|
|
} else { |
185
|
|
|
return $this->parseButtons( |
186
|
|
|
$this->laravel['config']->get( |
187
|
|
|
'datatables-buttons.generator.buttons', |
188
|
|
|
'create,export,print,reset,reload' |
189
|
|
|
) |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Parse array from definition. |
196
|
|
|
* |
197
|
|
|
* @param string $definition |
198
|
|
|
* @param int $indentation |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
protected function parseButtons($definition, $indentation = 24) |
202
|
|
|
{ |
203
|
|
|
$columns = explode(',', $definition); |
204
|
|
|
$stub = ''; |
205
|
|
|
foreach ($columns as $key => $column) { |
206
|
|
|
$indent = ''; |
207
|
|
|
$separator = ','; |
208
|
|
|
|
209
|
|
View Code Duplication |
if ($key < count($columns) - 1) { |
|
|
|
|
210
|
|
|
$indent = PHP_EOL . str_repeat(' ', $indentation); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if ($key == count($columns) - 1) { |
214
|
|
|
$separator = ''; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$stub .= "Button::make('{$column}')" . $separator . $indent; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $stub; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Replace columns. |
225
|
|
|
* |
226
|
|
|
* @param string $stub |
227
|
|
|
* @return $this |
228
|
|
|
*/ |
229
|
|
|
protected function replaceColumns(&$stub) |
230
|
|
|
{ |
231
|
|
|
$stub = str_replace( |
232
|
|
|
'DummyColumns', $this->getColumns(), $stub |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get the columns to be used. |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
|
|
protected function getColumns() |
244
|
|
|
{ |
245
|
|
|
if ($this->option('table')) { |
246
|
|
|
return $this->parseColumns(Schema::getColumnListing($this->option('table'))); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
if ($this->option('columns') != '') { |
250
|
|
|
return $this->parseColumns($this->option('columns')); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $this->parseColumns( |
254
|
|
|
$this->laravel['config']->get( |
255
|
|
|
'datatables-buttons.generator.columns', |
256
|
|
|
'id,add your columns,created_at,updated_at' |
257
|
|
|
) |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Parse array from definition. |
263
|
|
|
* |
264
|
|
|
* @param string $definition |
265
|
|
|
* @param int $indentation |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
protected function parseColumns($definition, $indentation = 12) |
269
|
|
|
{ |
270
|
|
|
$columns = is_array($definition) ? $definition : explode(',', $definition); |
271
|
|
|
$stub = ''; |
272
|
|
|
foreach ($columns as $key => $column) { |
273
|
|
|
$stub .= "Column::make('{$column}'),"; |
274
|
|
|
|
275
|
|
View Code Duplication |
if ($key < count($columns) - 1) { |
|
|
|
|
276
|
|
|
$stub .= PHP_EOL . str_repeat(' ', $indentation); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $stub; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Replace builder name. |
285
|
|
|
* |
286
|
|
|
* @param string $stub |
287
|
|
|
* @return \Yajra\DataTables\Generators\DataTablesMakeCommand |
288
|
|
|
*/ |
289
|
|
|
protected function replaceBuilder(&$stub) |
290
|
|
|
{ |
291
|
|
|
$name = $this->qualifyClass($this->getNameInput()); |
292
|
|
|
$class = str_replace($this->getNamespace($name) . '\\', '', $name); |
293
|
|
|
|
294
|
|
|
$stub = str_replace('DummyBuilder', $class . 'Html', $stub); |
295
|
|
|
|
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Parse the name and format according to the root namespace. |
301
|
|
|
* |
302
|
|
|
* @param string $name |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
|
View Code Duplication |
protected function qualifyClass($name) |
|
|
|
|
306
|
|
|
{ |
307
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
308
|
|
|
|
309
|
|
|
if (Str::startsWith($name, $rootNamespace)) { |
310
|
|
|
return $name; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
if (Str::contains($name, '/')) { |
314
|
|
|
$name = str_replace('/', '\\', $name); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
if (! Str::contains(Str::lower($name), 'datatable')) { |
318
|
|
|
$name .= 'DataTable'; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Get the default namespace for the class. |
326
|
|
|
* |
327
|
|
|
* @param string $rootNamespace |
328
|
|
|
* @return string |
329
|
|
|
*/ |
330
|
|
|
protected function getDefaultNamespace($rootNamespace) |
331
|
|
|
{ |
332
|
|
|
return $rootNamespace . '\\' . $this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables'); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Replace model name. |
337
|
|
|
* |
338
|
|
|
* @param string $stub |
339
|
|
|
* @return \Yajra\DataTables\Generators\DataTablesMakeCommand |
340
|
|
|
*/ |
341
|
|
|
protected function replaceModel(&$stub) |
342
|
|
|
{ |
343
|
|
|
$model = explode('\\', $this->getModel()); |
344
|
|
|
$model = array_pop($model); |
345
|
|
|
$stub = str_replace('ModelName', $model, $stub); |
346
|
|
|
|
347
|
|
|
return $this; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Get model name to use. |
352
|
|
|
*/ |
353
|
|
|
protected function getModel() |
354
|
|
|
{ |
355
|
|
|
$name = $this->getNameInput(); |
356
|
|
|
$rootNamespace = $this->laravel->getNamespace(); |
357
|
|
|
$model = $this->option('model') == '' || $this->option('model-namespace'); |
358
|
|
|
$modelNamespace = $this->option('model-namespace') ? $this->option('model-namespace') : $this->laravel['config']->get('datatables-buttons.namespace.model'); |
359
|
|
|
|
360
|
|
|
if ($this->option('model') != '') { |
361
|
|
|
return $this->option('model'); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
return $model |
365
|
|
|
? $rootNamespace . '\\' . ($modelNamespace ? $modelNamespace . '\\' : '') . Str::singular($name) |
366
|
|
|
: $rootNamespace . '\\User'; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Replace model import. |
371
|
|
|
* |
372
|
|
|
* @param string $stub |
373
|
|
|
* @return $this |
374
|
|
|
*/ |
375
|
|
|
protected function replaceModelImport(&$stub) |
376
|
|
|
{ |
377
|
|
|
$stub = str_replace( |
378
|
|
|
'DummyModel', str_replace('\\\\', '\\', $this->getModel()), $stub |
379
|
|
|
); |
380
|
|
|
|
381
|
|
|
return $this; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Get the stub file for the generator. |
386
|
|
|
* |
387
|
|
|
* @return string |
388
|
|
|
*/ |
389
|
|
|
protected function getStub() |
390
|
|
|
{ |
391
|
|
|
$config = $this->laravel['config']; |
392
|
|
|
$stub = 'datatables.stub'; |
393
|
|
|
|
394
|
|
|
if ($this->option('builder')) { |
395
|
|
|
$stub = 'builder.stub'; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return $config->get('datatables-buttons.stub') |
399
|
|
|
? base_path() . $config->get('datatables-buttons.stub') . "/{$stub}" |
400
|
|
|
: __DIR__ . "/stubs/{$stub}"; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
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.