Skip to content

Commit

Permalink
add option to generate controllers only
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Oct 27, 2023
1 parent 9f309ca commit 40fcb53
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
22 changes: 16 additions & 6 deletions src/Console/TomatoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ class TomatoGenerator extends Command
*
* @var string
*/
protected $name = 'tomato:generate';
protected $signature = 'tomato:generate
{table=0}
{module=0}
{--builder}
{--only-controllers}
';

/**
* The console command description.
Expand All @@ -44,7 +49,7 @@ public function handle(): void
return $item->{'Tables_in_'.config('database.connections.mysql.database')};
})->toArray();

$tableName = search(
$tableName = $this->argument('table') && $this->argument('table') != "0" ? $this->argument('table') : search(
label: 'Please input your table name you went to create CRUD?',
options: fn (string $value) => strlen($value) > 0
? collect($tables)->filter(function ($item, $key) use ($value){
Expand All @@ -63,14 +68,14 @@ public function handle(): void
}

//Check if user need to use HMVC
$isModule = confirm('Do you went to use HMVC module?');
$isModule = $this->argument('module') && $this->argument('module') != "0" ?: confirm('Do you went to use HMVC module?');
$moduleName = false;
if ($isModule){
if (class_exists(\Nwidart\Modules\Facades\Module::class)){
$modules = \Nwidart\Modules\Facades\Module::toCollection()->map(function ($item){
return $item->getName();
});
$moduleName = suggest(
$moduleName = $this->argument('module') && $this->argument('module') != "0" ?: suggest(
label:'Please input your module name?',
placeholder:'Translations',
options: fn (string $value) => strlen($value) > 0
Expand Down Expand Up @@ -104,7 +109,7 @@ public function handle(): void
}
}

$isBuilder = select(
$isBuilder = $this->option('builder') && $this->option('builder') != "0" ? $this->option('builder') : select(
label: 'Do you went to use Form Builder?',
options: ["form", "file"],
default: "file",
Expand All @@ -114,9 +119,14 @@ public function handle(): void
}
);

$onlyController = $this->option('only-controllers') && $this->option('only-controllers') != "0" ? $this->option('only-controllers') : confirm(
label: 'Do you went to generate controllers Only?',
);


//Generate CRUD Service
try {
$resourceGenerator = new CRUDGenerator(tableName:$tableName,moduleName:$moduleName,isBuilder: $isBuilder);
$resourceGenerator = new CRUDGenerator(tableName:$tableName,moduleName:$moduleName,isBuilder: $isBuilder, isOnlyController: $onlyController);
$resourceGenerator->generate();
info('CRUD Has Been Generated Success');
} catch (\Exception $e) {
Expand Down
35 changes: 21 additions & 14 deletions src/Services/Generator/CRUDGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class CRUDGenerator
public function __construct(
private string $tableName,
private string | bool | null $moduleName,
private string $isBuilder
private string $isBuilder,
private bool $isOnlyController
){
$connectionParams = [
'dbname' => config('database.connections.mysql.database'),
Expand All @@ -81,21 +82,27 @@ public function __construct(
*/
public function generate(): void
{
$this->generateFolders();
$this->generateModel();
$this->generateTable();
($this->isBuilder == 'form')?$this->generateControllerForBuilder():$this->generateController();
$this->generateRoutes();
$this->generateIndexView();
if ($this->isBuilder == 'form'){
$this->generateFormView();
$this->generateFormBuilderClass();
if($this->isOnlyController){
($this->isBuilder == 'form')?$this->generateControllerForBuilder():$this->generateController(true);
}
else {
$this->generateFolders();
$this->generateModel();
$this->generateTable();
($this->isBuilder == 'form')?$this->generateControllerForBuilder():$this->generateController();
$this->generateRoutes();
$this->generateIndexView();
if ($this->isBuilder == 'form'){
$this->generateFormView();
$this->generateFormBuilderClass();

}else{
$this->generateCreateView();
$this->generateEditView();
}else{
$this->generateCreateView();
$this->generateEditView();
}
$this->generateShowView();
}
$this->generateShowView();

}

}
9 changes: 8 additions & 1 deletion src/Services/Generator/Concerns/GenerateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

namespace TomatoPHP\TomatoPHP\Services\Generator\Concerns;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

trait GenerateController
{
private function generateController()
private function generateController(bool $isForce = false)
{
$filePath = $this->moduleName ? module_path($this->moduleName) ."/Http/Controllers/{$this->modelName}Controller.php" : app_path("Http/Controllers/Admin/{$this->modelName}Controller.php");
if($isForce){
if(File::exists($filePath)){
File::delete($filePath);
}
}
$this->generateStubs(
$this->stubPath . "controller.stub",
$this->moduleName ? module_path($this->moduleName) ."/Http/Controllers/{$this->modelName}Controller.php" : app_path("Http/Controllers/Admin/{$this->modelName}Controller.php"),
Expand Down

0 comments on commit 40fcb53

Please sign in to comment.