Skip to content

Commit 6e5841f

Browse files
author
Awais
committed
v1.5
- Support all DBs that Laravel support (MySQL,PgSQL, MSSQL) - Added more validation rules - Default Model name updated to `App\Models` - Bug fixes
1 parent a501350 commit 6e5841f

File tree

2 files changed

+126
-129
lines changed

2 files changed

+126
-129
lines changed

src/Commands/GeneratorCommand.php

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Console\Command;
77
use Illuminate\Filesystem\Filesystem;
88
use Illuminate\Support\Arr;
9-
use Illuminate\Support\Facades\DB;
109
use Illuminate\Support\Facades\Schema;
1110
use Illuminate\Support\Str;
1211
use Symfony\Component\Console\Input\InputArgument;
@@ -64,7 +63,7 @@ abstract class GeneratorCommand extends Command
6463
*
6564
* @var string
6665
*/
67-
protected $modelNamespace = 'App';
66+
protected $modelNamespace = 'App\Models';
6867

6968
/**
7069
* Controller Namespace.
@@ -90,7 +89,7 @@ abstract class GeneratorCommand extends Command
9089
/**
9190
* Create a new controller creator command instance.
9291
*
93-
* @param \Illuminate\Filesystem\Filesystem $files
92+
* @param \Illuminate\Filesystem\Filesystem $files
9493
*
9594
* @return void
9695
*/
@@ -129,13 +128,13 @@ abstract protected function buildViews();
129128
/**
130129
* Build the directory if necessary.
131130
*
132-
* @param string $path
131+
* @param string $path
133132
*
134133
* @return string
135134
*/
136135
protected function makeDirectory($path)
137136
{
138-
if (!$this->files->isDirectory(dirname($path))) {
137+
if (! $this->files->isDirectory(dirname($path))) {
139138
$this->files->makeDirectory(dirname($path), 0777, true, true);
140139
}
141140

@@ -156,8 +155,8 @@ protected function write($path, $content)
156155
/**
157156
* Get the stub file.
158157
*
159-
* @param string $type
160-
* @param boolean $content
158+
* @param string $type
159+
* @param boolean $content
161160
*
162161
* @return string
163162
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
@@ -167,12 +166,12 @@ protected function getStub($type, $content = true)
167166
{
168167
$stub_path = config('crud.stub_path', 'default');
169168
if ($stub_path == 'default') {
170-
$stub_path = __DIR__ . '/../stubs/';
169+
$stub_path = __DIR__.'/../stubs/';
171170
}
172171

173-
$path = Str::finish($stub_path, '/') . "{$type}.stub";
172+
$path = Str::finish($stub_path, '/')."{$type}.stub";
174173

175-
if (!$content) {
174+
if (! $content) {
176175
return $path;
177176
}
178177

@@ -201,7 +200,7 @@ private function _getSpace($no = 1)
201200
*/
202201
protected function _getControllerPath($name)
203202
{
204-
return app_path($this->_getNamespacePath($this->controllerNamespace) . "{$name}Controller.php");
203+
return app_path($this->_getNamespacePath($this->controllerNamespace)."{$name}Controller.php");
205204
}
206205

207206
/**
@@ -211,7 +210,7 @@ protected function _getControllerPath($name)
211210
*/
212211
protected function _getModelPath($name)
213212
{
214-
return $this->makeDirectory(app_path($this->_getNamespacePath($this->modelNamespace) . "{$name}.php"));
213+
return $this->makeDirectory(app_path($this->_getNamespacePath($this->modelNamespace)."{$name}.php"));
215214
}
216215

217216
/**
@@ -276,7 +275,7 @@ protected function buildReplacements()
276275
*
277276
* @param $title
278277
* @param $column
279-
* @param string $type
278+
* @param string $type
280279
*
281280
* @return mixed
282281
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
@@ -308,7 +307,7 @@ protected function getHead($title)
308307
return str_replace(
309308
array_keys($replace),
310309
array_values($replace),
311-
$this->_getSpace(10) . '<th>{{title}}</th>' . "\n"
310+
$this->_getSpace(10).'<th>{{title}}</th>'."\n"
312311
);
313312
}
314313

@@ -326,7 +325,7 @@ protected function getBody($column)
326325
return str_replace(
327326
array_keys($replace),
328327
array_values($replace),
329-
$this->_getSpace(11) . '<td>{{ ${{modelNameLowerCase}}->{{column}} }}</td>' . "\n"
328+
$this->_getSpace(11).'<td>{{ ${{modelNameLowerCase}}->{{column}} }}</td>'."\n"
330329
);
331330
}
332331

@@ -337,7 +336,7 @@ protected function getBody($column)
337336
*/
338337
protected function buildLayout(): void
339338
{
340-
if (!(view()->exists($this->layout))) {
339+
if (! (view()->exists($this->layout))) {
341340

342341
$this->info('Creating Layout ...');
343342

@@ -357,7 +356,7 @@ protected function buildLayout(): void
357356
protected function getColumns()
358357
{
359358
if (empty($this->tableColumns)) {
360-
$this->tableColumns = DB::select('SHOW COLUMNS FROM ' . $this->table);
359+
$this->tableColumns = Schema::getColumns($this->table);
361360
}
362361

363362
return $this->tableColumns;
@@ -372,11 +371,11 @@ protected function getFilteredColumns()
372371
$columns = [];
373372

374373
foreach ($this->getColumns() as $column) {
375-
$columns[] = $column->Field;
374+
$columns[] = $column['name'];
376375
}
377376

378377
return array_filter($columns, function ($value) use ($unwanted) {
379-
return !in_array($value, $unwanted);
378+
return ! in_array($value, $unwanted);
380379
});
381380
}
382381

@@ -391,14 +390,26 @@ protected function modelReplacements()
391390
$rulesArray = [];
392391
$softDeletesNamespace = $softDeletes = '';
393392

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+
}
396407

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';
399410
}
400411

401-
if ($value->Field == 'deleted_at') {
412+
if ($column['name'] == 'deleted_at') {
402413
$softDeletesNamespace = "use Illuminate\Database\Eloquent\SoftDeletes;\n";
403414
$softDeletes = "use SoftDeletes;\n";
404415
}
@@ -410,7 +421,7 @@ protected function modelReplacements()
410421
$rulesArray = Arr::except($rulesArray, $this->unwantedColumns);
411422
// Make rulesArray
412423
foreach ($rulesArray as $col => $rule) {
413-
$rules .= "\n\t\t'{$col}' => '{$rule}',";
424+
$rules .= "\n\t\t'{$col}' => '".implode('|', $rule)."',";
414425
}
415426

416427
return $rules;
@@ -423,7 +434,7 @@ protected function modelReplacements()
423434

424435
// Add quotes to the unwanted columns for fillable
425436
array_walk($filterColumns, function (&$value) {
426-
$value = "'" . $value . "'";
437+
$value = "'".$value."'";
427438
});
428439

429440
// CSV format
@@ -432,7 +443,7 @@ protected function modelReplacements()
432443

433444
$properties .= "\n *";
434445

435-
list($relations, $properties) = (new ModelGenerator($this->table, $properties, $this->modelNamespace))->getEloquentRelations();
446+
[$relations, $properties] = (new ModelGenerator($this->table, $properties, $this->modelNamespace))->getEloquentRelations();
436447

437448
return [
438449
'{{fillable}}' => $fillable(),
@@ -463,7 +474,7 @@ protected function buildOptions()
463474
{
464475
$route = $this->option('route');
465476

466-
if (!empty($route)) {
477+
if (! empty($route)) {
467478
$this->options['route'] = $route;
468479
}
469480

0 commit comments

Comments
 (0)