CreateCrud.php 6.8 KB

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