6
6
use Illuminate \Console \Command ;
7
7
use Illuminate \Filesystem \Filesystem ;
8
8
use Illuminate \Support \Arr ;
9
- use Illuminate \Support \Facades \DB ;
10
9
use Illuminate \Support \Facades \Schema ;
11
10
use Illuminate \Support \Str ;
12
11
use Symfony \Component \Console \Input \InputArgument ;
@@ -64,7 +63,7 @@ abstract class GeneratorCommand extends Command
64
63
*
65
64
* @var string
66
65
*/
67
- protected $ modelNamespace = 'App ' ;
66
+ protected $ modelNamespace = 'App\Models ' ;
68
67
69
68
/**
70
69
* Controller Namespace.
@@ -90,7 +89,7 @@ abstract class GeneratorCommand extends Command
90
89
/**
91
90
* Create a new controller creator command instance.
92
91
*
93
- * @param \Illuminate\Filesystem\Filesystem $files
92
+ * @param \Illuminate\Filesystem\Filesystem $files
94
93
*
95
94
* @return void
96
95
*/
@@ -129,13 +128,13 @@ abstract protected function buildViews();
129
128
/**
130
129
* Build the directory if necessary.
131
130
*
132
- * @param string $path
131
+ * @param string $path
133
132
*
134
133
* @return string
135
134
*/
136
135
protected function makeDirectory ($ path )
137
136
{
138
- if (!$ this ->files ->isDirectory (dirname ($ path ))) {
137
+ if (! $ this ->files ->isDirectory (dirname ($ path ))) {
139
138
$ this ->files ->makeDirectory (dirname ($ path ), 0777 , true , true );
140
139
}
141
140
@@ -156,8 +155,8 @@ protected function write($path, $content)
156
155
/**
157
156
* Get the stub file.
158
157
*
159
- * @param string $type
160
- * @param boolean $content
158
+ * @param string $type
159
+ * @param boolean $content
161
160
*
162
161
* @return string
163
162
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
@@ -167,12 +166,12 @@ protected function getStub($type, $content = true)
167
166
{
168
167
$ stub_path = config ('crud.stub_path ' , 'default ' );
169
168
if ($ stub_path == 'default ' ) {
170
- $ stub_path = __DIR__ . '/../stubs/ ' ;
169
+ $ stub_path = __DIR__ . '/../stubs/ ' ;
171
170
}
172
171
173
- $ path = Str::finish ($ stub_path , '/ ' ) . "{$ type }.stub " ;
172
+ $ path = Str::finish ($ stub_path , '/ ' ). "{$ type }.stub " ;
174
173
175
- if (!$ content ) {
174
+ if (! $ content ) {
176
175
return $ path ;
177
176
}
178
177
@@ -201,7 +200,7 @@ private function _getSpace($no = 1)
201
200
*/
202
201
protected function _getControllerPath ($ name )
203
202
{
204
- return app_path ($ this ->_getNamespacePath ($ this ->controllerNamespace ) . "{$ name }Controller.php " );
203
+ return app_path ($ this ->_getNamespacePath ($ this ->controllerNamespace ). "{$ name }Controller.php " );
205
204
}
206
205
207
206
/**
@@ -211,7 +210,7 @@ protected function _getControllerPath($name)
211
210
*/
212
211
protected function _getModelPath ($ name )
213
212
{
214
- return $ this ->makeDirectory (app_path ($ this ->_getNamespacePath ($ this ->modelNamespace ) . "{$ name }.php " ));
213
+ return $ this ->makeDirectory (app_path ($ this ->_getNamespacePath ($ this ->modelNamespace ). "{$ name }.php " ));
215
214
}
216
215
217
216
/**
@@ -276,7 +275,7 @@ protected function buildReplacements()
276
275
*
277
276
* @param $title
278
277
* @param $column
279
- * @param string $type
278
+ * @param string $type
280
279
*
281
280
* @return mixed
282
281
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
@@ -308,7 +307,7 @@ protected function getHead($title)
308
307
return str_replace (
309
308
array_keys ($ replace ),
310
309
array_values ($ replace ),
311
- $ this ->_getSpace (10 ) . '<th>{{title}}</th> ' . "\n"
310
+ $ this ->_getSpace (10 ). '<th>{{title}}</th> ' . "\n"
312
311
);
313
312
}
314
313
@@ -326,7 +325,7 @@ protected function getBody($column)
326
325
return str_replace (
327
326
array_keys ($ replace ),
328
327
array_values ($ replace ),
329
- $ this ->_getSpace (11 ) . '<td>{{ ${{modelNameLowerCase}}->{{column}} }}</td> ' . "\n"
328
+ $ this ->_getSpace (11 ). '<td>{{ ${{modelNameLowerCase}}->{{column}} }}</td> ' . "\n"
330
329
);
331
330
}
332
331
@@ -337,7 +336,7 @@ protected function getBody($column)
337
336
*/
338
337
protected function buildLayout (): void
339
338
{
340
- if (!(view ()->exists ($ this ->layout ))) {
339
+ if (! (view ()->exists ($ this ->layout ))) {
341
340
342
341
$ this ->info ('Creating Layout ... ' );
343
342
@@ -357,7 +356,7 @@ protected function buildLayout(): void
357
356
protected function getColumns ()
358
357
{
359
358
if (empty ($ this ->tableColumns )) {
360
- $ this ->tableColumns = DB :: select ( ' SHOW COLUMNS FROM ' . $ this ->table );
359
+ $ this ->tableColumns = Schema:: getColumns ( $ this ->table );
361
360
}
362
361
363
362
return $ this ->tableColumns ;
@@ -372,11 +371,11 @@ protected function getFilteredColumns()
372
371
$ columns = [];
373
372
374
373
foreach ($ this ->getColumns () as $ column ) {
375
- $ columns [] = $ column-> Field ;
374
+ $ columns [] = $ column[ ' name ' ] ;
376
375
}
377
376
378
377
return array_filter ($ columns , function ($ value ) use ($ unwanted ) {
379
- return !in_array ($ value , $ unwanted );
378
+ return ! in_array ($ value , $ unwanted );
380
379
});
381
380
}
382
381
@@ -391,14 +390,26 @@ protected function modelReplacements()
391
390
$ rulesArray = [];
392
391
$ softDeletesNamespace = $ softDeletes = '' ;
393
392
394
- foreach ($ this ->getColumns () as $ value ) {
395
- $ properties .= "\n * @property $ $ value ->Field " ;
393
+ foreach ($ this ->getColumns () as $ column ) {
394
+ $ properties .= "\n * @property \${$ column ['name ' ]}" ;
395
+
396
+ if (! $ column ['nullable ' ]) {
397
+ $ rulesArray [$ column ['name ' ]] = ['required ' ];
398
+ }
399
+
400
+ if ($ column ['type_name ' ] == 'bool ' ) {
401
+ $ rulesArray [$ column ['name ' ]][] = 'boolean ' ;
402
+ }
403
+
404
+ if ($ column ['type_name ' ] == 'uuid ' ) {
405
+ $ rulesArray [$ column ['name ' ]][] = 'uuid ' ;
406
+ }
396
407
397
- if ($ value -> Null == 'NO ' ) {
398
- $ rulesArray [$ value -> Field ] = 'required ' ;
408
+ if ($ column [ ' type_name ' ] == 'text ' || $ column [ ' type_name ' ] == ' varchar ' ) {
409
+ $ rulesArray [$ column [ ' name ' ]][] = 'string ' ;
399
410
}
400
411
401
- if ($ value -> Field == 'deleted_at ' ) {
412
+ if ($ column [ ' name ' ] == 'deleted_at ' ) {
402
413
$ softDeletesNamespace = "use Illuminate\Database\Eloquent\SoftDeletes; \n" ;
403
414
$ softDeletes = "use SoftDeletes; \n" ;
404
415
}
@@ -410,7 +421,7 @@ protected function modelReplacements()
410
421
$ rulesArray = Arr::except ($ rulesArray , $ this ->unwantedColumns );
411
422
// Make rulesArray
412
423
foreach ($ rulesArray as $ col => $ rule ) {
413
- $ rules .= "\n\t\t' {$ col }' => ' { $ rule} ', " ;
424
+ $ rules .= "\n\t\t' {$ col }' => ' " . implode ( ' | ' , $ rule). " ', " ;
414
425
}
415
426
416
427
return $ rules ;
@@ -423,7 +434,7 @@ protected function modelReplacements()
423
434
424
435
// Add quotes to the unwanted columns for fillable
425
436
array_walk ($ filterColumns , function (&$ value ) {
426
- $ value = "' " . $ value . "' " ;
437
+ $ value = "' " . $ value. "' " ;
427
438
});
428
439
429
440
// CSV format
@@ -432,7 +443,7 @@ protected function modelReplacements()
432
443
433
444
$ properties .= "\n * " ;
434
445
435
- list ( $ relations , $ properties) = (new ModelGenerator ($ this ->table , $ properties , $ this ->modelNamespace ))->getEloquentRelations ();
446
+ [ $ relations , $ properties] = (new ModelGenerator ($ this ->table , $ properties , $ this ->modelNamespace ))->getEloquentRelations ();
436
447
437
448
return [
438
449
'{{fillable}} ' => $ fillable (),
@@ -463,7 +474,7 @@ protected function buildOptions()
463
474
{
464
475
$ route = $ this ->option ('route ' );
465
476
466
- if (!empty ($ route )) {
477
+ if (! empty ($ route )) {
467
478
$ this ->options ['route ' ] = $ route ;
468
479
}
469
480
0 commit comments