CreateCrud.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace App\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Filesystem\Filesystem;
  5. use Illuminate\Support\Str;
  6. class CreateCrud extends Command
  7. {
  8. protected $signature = 'create:crud
  9. {name : The name of the model}
  10. {--m|model : Create only the Model}
  11. {--s|service : Create only the Service}
  12. {--c|controller : Create only the Controller}
  13. {--r|request : Create only the Request}
  14. {--e|resource : Create only the Resource}
  15. {--t|route : Create only the Route}
  16. {--g|migration : Create only the Migration}
  17. {--all : Create all files (default behavior)}';
  18. protected $description = "Create CRUD operations with typed Models (use options to create specific files)";
  19. protected $files;
  20. public function __construct(Filesystem $files)
  21. {
  22. parent::__construct();
  23. $this->files = $files;
  24. }
  25. public function handle(): void
  26. {
  27. $name = Str::studly($this->argument("name"));
  28. $hasSpecificOption =
  29. $this->option("model") ||
  30. $this->option("service") ||
  31. $this->option("controller") ||
  32. $this->option("request") ||
  33. $this->option("resource") ||
  34. $this->option("route") ||
  35. $this->option("migration");
  36. $createAll = !$hasSpecificOption || $this->option("all");
  37. $this->info("Creating CRUD for: {$name}");
  38. $this->newLine();
  39. if ($createAll || $this->option("model")) {
  40. $this->createModel($name);
  41. }
  42. if ($createAll || $this->option("service")) {
  43. $this->createService($name);
  44. }
  45. if ($createAll || $this->option("controller")) {
  46. $this->createController($name);
  47. }
  48. if ($createAll || $this->option("request")) {
  49. $this->createRequest($name);
  50. }
  51. if ($createAll || $this->option("resource")) {
  52. $this->createResource($name);
  53. }
  54. if ($createAll || $this->option("route")) {
  55. $this->createRoute($name);
  56. }
  57. if ($createAll || $this->option("migration")) {
  58. $this->createMigration($name);
  59. }
  60. $this->newLine();
  61. $this->info("✓ CRUD operations created successfully!");
  62. if ($createAll) {
  63. $this->newLine();
  64. $this->info("Next steps:");
  65. $this->info(
  66. "1. Update the migration: database/migrations/*_create_{$this->getTableName(
  67. $name,
  68. )}_table.php",
  69. );
  70. $this->info(
  71. "2. Update the model properties in: app/Models/{$name}.php",
  72. );
  73. $this->info(
  74. "3. Update validation rules in: app/Http/Requests/{$name}Request.php",
  75. );
  76. $this->info(
  77. "4. Update resource fields in: app/Http/Resources/{$name}Resource.php",
  78. );
  79. $this->info("5. Run: php artisan migrate");
  80. $this->info("6. Run: php artisan ide-helper:models -RW");
  81. }
  82. }
  83. protected function createModel(string $name): void
  84. {
  85. $modelTemplate = str_replace(
  86. ["{{modelName}}", "{{tableNameSnakeCase}}"],
  87. [$name, $this->getTableName($name)],
  88. $this->getStub("Model"),
  89. );
  90. $this->put("app/Models/{$name}.php", $modelTemplate);
  91. $this->info("✓ Created Model: app/Models/{$name}.php");
  92. }
  93. protected function createService(string $name): void
  94. {
  95. $serviceTemplate = str_replace(
  96. ["{{modelName}}"],
  97. [$name],
  98. $this->getStub("Service"),
  99. );
  100. $this->put("app/Services/{$name}Service.php", $serviceTemplate);
  101. $this->info("✓ Created Service: app/Services/{$name}Service.php");
  102. }
  103. protected function createController(string $name): void
  104. {
  105. $controllerTemplate = str_replace(
  106. ["{{modelName}}"],
  107. [$name],
  108. $this->getStub("Controller"),
  109. );
  110. $this->put(
  111. "app/Http/Controllers/{$name}Controller.php",
  112. $controllerTemplate,
  113. );
  114. $this->info(
  115. "✓ Created Controller: app/Http/Controllers/{$name}Controller.php",
  116. );
  117. }
  118. protected function createRequest(string $name): void
  119. {
  120. $requestTemplate = str_replace(
  121. ["{{modelName}}"],
  122. [$name],
  123. $this->getStub("Request"),
  124. );
  125. $this->put("app/Http/Requests/{$name}Request.php", $requestTemplate);
  126. $this->info("✓ Created Request: app/Http/Requests/{$name}Request.php");
  127. }
  128. protected function createResource(string $name): void
  129. {
  130. $resourceTemplate = str_replace(
  131. ["{{modelName}}"],
  132. [$name],
  133. $this->getStub("Resource"),
  134. );
  135. $this->put("app/Http/Resources/{$name}Resource.php", $resourceTemplate);
  136. $this->info(
  137. "✓ Created Resource: app/Http/Resources/{$name}Resource.php",
  138. );
  139. }
  140. protected function createRoute(string $name): void
  141. {
  142. $routeTemplate = str_replace(
  143. ["{{modelName}}", "{{modelNameSnakeCase}}"],
  144. [$name, Str::snake($name, "-")],
  145. $this->getStub("Route"),
  146. );
  147. $routePath = base_path(
  148. "routes/authRoutes/" . Str::snake($name) . ".php",
  149. );
  150. if (!$this->files->isDirectory(base_path("routes/authRoutes"))) {
  151. $this->files->makeDirectory(
  152. base_path("routes/authRoutes"),
  153. 0755,
  154. true,
  155. );
  156. }
  157. $this->put($routePath, $routeTemplate);
  158. $this->info(
  159. "✓ Created Route: routes/authRoutes/" . Str::snake($name) . ".php",
  160. );
  161. }
  162. protected function createMigration(string $name): void
  163. {
  164. $tableName = $this->getTableName($name);
  165. $migrationTemplate = str_replace(
  166. ["{{tableNameSnakeCase}}"],
  167. [$tableName],
  168. $this->getStub("Migration"),
  169. );
  170. $timestamp = date("Y_m_d_His");
  171. $filename = $timestamp . "_create_{$tableName}_table.php";
  172. $this->put(database_path("migrations/{$filename}"), $migrationTemplate);
  173. $this->info("✓ Created Migration: database/migrations/{$filename}");
  174. }
  175. protected function getTableName(string $name): string
  176. {
  177. return Str::snake(Str::plural($name));
  178. }
  179. protected function getStub(string $type): string
  180. {
  181. return $this->files->get(storage_path("stubs/{$type}.stub"));
  182. }
  183. protected function put(string $path, string $contents): void
  184. {
  185. $this->files->put($path, $contents);
  186. }
  187. }