Skip to content

Commit

Permalink
Merge pull request #38 from nafiesl/uuid_primary
Browse files Browse the repository at this point in the history
Generate CRUD with UUID Primary Key
  • Loading branch information
nafiesl authored Apr 16, 2021
2 parents 51a2107 + f7474a9 commit 16ccf65
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/CrudApiMake.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class CrudApiMake extends GeneratorCommand
{--p|parent= : The generated API controller parent directory}
{--t|tests-only : Generate API CRUD testcases only}
{--r|form-requests : Generate CRUD with Form Request on create and update actions}
{--f|formfield : Generate CRUD with FormField facades}';
{--f|formfield : Generate CRUD with FormField facades}
{--uuid : Generate CRUD with UUID primary keys}';

/**
* The console command description.
Expand Down
3 changes: 2 additions & 1 deletion src/CrudMake.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class CrudMake extends GeneratorCommand
{--t|tests-only : Generate CRUD testcases only}
{--f|formfield : Generate CRUD with FormField facades}
{--r|form-requests : Generate CRUD with Form Request on create and update actions}
{--bs3 : Generate CRUD with Bootstrap 3 views}';
{--bs3 : Generate CRUD with Bootstrap 3 views}
{--uuid : Generate CRUD with UUID primary keys}';

/**
* The console command description.
Expand Down
3 changes: 2 additions & 1 deletion src/CrudSimpleMake.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class CrudSimpleMake extends GeneratorCommand
{--t|tests-only : Generate CRUD testcases only}
{--f|formfield : Generate CRUD with FormField facades}
{--r|form-requests : Generate CRUD with Form Request on create and update actions}
{--bs3 : Generate CRUD with Bootstrap 3 views}';
{--bs3 : Generate CRUD with Bootstrap 3 views}
{--uuid : Generate CRUD with UUID primary keys}';

/**
* The console command description.
Expand Down
12 changes: 12 additions & 0 deletions src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ public function getContent(string $stubName)
);
}

if ($this->command->option('uuid')) {
$string = "use Illuminate\Http\Request;\n";
$replacement = "use Illuminate\Http\Request;\nuse Ramsey\Uuid\Uuid;\n";
$controllerFileContent = str_replace($string, $replacement, $controllerFileContent);

$string = "\$new{$this->modelNames['model_name']}['creator_id'] = auth()->id();\n";
$replacement = "\$new{$this->modelNames['model_name']}['id'] = Uuid::uuid4()->toString();\n";
$replacement .= " \$new{$this->modelNames['model_name']}['creator_id'] = auth()->id();\n";

$controllerFileContent = str_replace($string, $replacement, $controllerFileContent);
}

return $controllerFileContent;
}
}
8 changes: 7 additions & 1 deletion src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public function generate(string $type = 'full')
*/
public function getContent(string $stubName)
{
return $this->replaceStubString($this->getStubFileContent($stubName));
$content = $this->replaceStubString($this->getStubFileContent($stubName));

if ($this->command->option('uuid')) {
$content = str_replace("\$table->bigIncrements('id')", "\$table->uuid('id')", $content);
}

return $content;
}
}
6 changes: 6 additions & 0 deletions src/Generators/ModelFactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public function getContent(string $stubName)
$modelFactoryFileContent = str_replace('App\User', $userModel, $modelFactoryFileContent);
}

if ($this->command->option('uuid')) {
$string = "'title' => \$this->faker->word,\n";
$replacement = "'id' => \$this->faker->uuid,\n 'title' => \$this->faker->word,\n";
$modelFactoryFileContent = str_replace($string, $replacement, $modelFactoryFileContent);
}

return $this->replaceStubString($modelFactoryFileContent);
}
}
8 changes: 8 additions & 0 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function getContent(string $stubName)
$modelFileContent = str_replace('App\User', $userModel, $modelFileContent);
}

if ($this->command->option('uuid')) {
$string = "protected \$fillable = ['title', 'description', 'creator_id'];\n";
$replacement = "public \$incrementing = false;\n\n";
$replacement .= " protected \$keyType = 'string';\n\n";
$replacement .= " protected \$fillable = ['id', 'title', 'description', 'creator_id'];\n";
$modelFileContent = str_replace($string, $replacement, $modelFileContent);
}

return $this->replaceStubString($modelFileContent);
}
}
Loading

0 comments on commit 16ccf65

Please sign in to comment.