Skip to content

Commit

Permalink
v1.5
Browse files Browse the repository at this point in the history
- Support all DBs that Laravel support (MySQL,PgSQL, MSSQL)
- Added more validation rules
- Default Model name updated to `App\Models`
- Bug fixes
  • Loading branch information
Awais committed Feb 28, 2024
1 parent a501350 commit 6e5841f
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 129 deletions.
67 changes: 39 additions & 28 deletions src/Commands/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -64,7 +63,7 @@ abstract class GeneratorCommand extends Command
*
* @var string
*/
protected $modelNamespace = 'App';
protected $modelNamespace = 'App\Models';

/**
* Controller Namespace.
Expand All @@ -90,7 +89,7 @@ abstract class GeneratorCommand extends Command
/**
* Create a new controller creator command instance.
*
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Illuminate\Filesystem\Filesystem $files
*
* @return void
*/
Expand Down Expand Up @@ -129,13 +128,13 @@ abstract protected function buildViews();
/**
* Build the directory if necessary.
*
* @param string $path
* @param string $path
*
* @return string
*/
protected function makeDirectory($path)
{
if (!$this->files->isDirectory(dirname($path))) {
if (! $this->files->isDirectory(dirname($path))) {
$this->files->makeDirectory(dirname($path), 0777, true, true);
}

Expand All @@ -156,8 +155,8 @@ protected function write($path, $content)
/**
* Get the stub file.
*
* @param string $type
* @param boolean $content
* @param string $type
* @param boolean $content
*
* @return string
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
Expand All @@ -167,12 +166,12 @@ protected function getStub($type, $content = true)
{
$stub_path = config('crud.stub_path', 'default');
if ($stub_path == 'default') {
$stub_path = __DIR__ . '/../stubs/';
$stub_path = __DIR__.'/../stubs/';
}

$path = Str::finish($stub_path, '/') . "{$type}.stub";
$path = Str::finish($stub_path, '/')."{$type}.stub";

if (!$content) {
if (! $content) {
return $path;
}

Expand Down Expand Up @@ -201,7 +200,7 @@ private function _getSpace($no = 1)
*/
protected function _getControllerPath($name)
{
return app_path($this->_getNamespacePath($this->controllerNamespace) . "{$name}Controller.php");
return app_path($this->_getNamespacePath($this->controllerNamespace)."{$name}Controller.php");
}

/**
Expand All @@ -211,7 +210,7 @@ protected function _getControllerPath($name)
*/
protected function _getModelPath($name)
{
return $this->makeDirectory(app_path($this->_getNamespacePath($this->modelNamespace) . "{$name}.php"));
return $this->makeDirectory(app_path($this->_getNamespacePath($this->modelNamespace)."{$name}.php"));
}

/**
Expand Down Expand Up @@ -276,7 +275,7 @@ protected function buildReplacements()
*
* @param $title
* @param $column
* @param string $type
* @param string $type
*
* @return mixed
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
Expand Down Expand Up @@ -308,7 +307,7 @@ protected function getHead($title)
return str_replace(
array_keys($replace),
array_values($replace),
$this->_getSpace(10) . '<th>{{title}}</th>' . "\n"
$this->_getSpace(10).'<th>{{title}}</th>'."\n"
);
}

Expand All @@ -326,7 +325,7 @@ protected function getBody($column)
return str_replace(
array_keys($replace),
array_values($replace),
$this->_getSpace(11) . '<td>{{ ${{modelNameLowerCase}}->{{column}} }}</td>' . "\n"
$this->_getSpace(11).'<td>{{ ${{modelNameLowerCase}}->{{column}} }}</td>'."\n"
);
}

Expand All @@ -337,7 +336,7 @@ protected function getBody($column)
*/
protected function buildLayout(): void
{
if (!(view()->exists($this->layout))) {
if (! (view()->exists($this->layout))) {

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

Expand All @@ -357,7 +356,7 @@ protected function buildLayout(): void
protected function getColumns()
{
if (empty($this->tableColumns)) {
$this->tableColumns = DB::select('SHOW COLUMNS FROM ' . $this->table);
$this->tableColumns = Schema::getColumns($this->table);
}

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

foreach ($this->getColumns() as $column) {
$columns[] = $column->Field;
$columns[] = $column['name'];
}

return array_filter($columns, function ($value) use ($unwanted) {
return !in_array($value, $unwanted);
return ! in_array($value, $unwanted);
});
}

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

foreach ($this->getColumns() as $value) {
$properties .= "\n * @property $$value->Field";
foreach ($this->getColumns() as $column) {
$properties .= "\n * @property \${$column['name']}";

if (! $column['nullable']) {
$rulesArray[$column['name']] = ['required'];
}

if ($column['type_name'] == 'bool') {
$rulesArray[$column['name']][] = 'boolean';
}

if ($column['type_name'] == 'uuid') {
$rulesArray[$column['name']][] = 'uuid';
}

if ($value->Null == 'NO') {
$rulesArray[$value->Field] = 'required';
if ($column['type_name'] == 'text' || $column['type_name'] == 'varchar') {
$rulesArray[$column['name']][] = 'string';
}

if ($value->Field == 'deleted_at') {
if ($column['name'] == 'deleted_at') {
$softDeletesNamespace = "use Illuminate\Database\Eloquent\SoftDeletes;\n";
$softDeletes = "use SoftDeletes;\n";
}
Expand All @@ -410,7 +421,7 @@ protected function modelReplacements()
$rulesArray = Arr::except($rulesArray, $this->unwantedColumns);
// Make rulesArray
foreach ($rulesArray as $col => $rule) {
$rules .= "\n\t\t'{$col}' => '{$rule}',";
$rules .= "\n\t\t'{$col}' => '".implode('|', $rule)."',";
}

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

// Add quotes to the unwanted columns for fillable
array_walk($filterColumns, function (&$value) {
$value = "'" . $value . "'";
$value = "'".$value."'";
});

// CSV format
Expand All @@ -432,7 +443,7 @@ protected function modelReplacements()

$properties .= "\n *";

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

return [
'{{fillable}}' => $fillable(),
Expand Down Expand Up @@ -463,7 +474,7 @@ protected function buildOptions()
{
$route = $this->option('route');

if (!empty($route)) {
if (! empty($route)) {
$this->options['route'] = $route;
}

Expand Down
Loading

0 comments on commit 6e5841f

Please sign in to comment.