CreateCrud.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. //
  26. public function handle(): void
  27. {
  28. $name = Str::studly($this->argument('name'));
  29. $hasSpecificOption = $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. //
  84. protected function createController(string $name): void
  85. {
  86. $controllerTemplate = str_replace(
  87. ['{{modelName}}'],
  88. [$name],
  89. $this->getStub('Controller'),
  90. );
  91. $this->put(
  92. "app/Http/Controllers/{$name}Controller.php",
  93. $controllerTemplate,
  94. );
  95. $this->info(
  96. "✓ Created Controller: app/Http/Controllers/{$name}Controller.php",
  97. );
  98. }
  99. protected function createMigration(string $name): void
  100. {
  101. $tableName = $this->getTableName($name);
  102. $migrationTemplate = str_replace(
  103. ['{{tableNameSnakeCase}}'],
  104. [$tableName],
  105. $this->getStub('Migration'),
  106. );
  107. $timestamp = date('Y_m_d_His');
  108. $filename = $timestamp."_create_{$tableName}_table.php";
  109. $this->put(database_path("migrations/{$filename}"), $migrationTemplate);
  110. $this->info("✓ Created Migration: database/migrations/{$filename}");
  111. }
  112. protected function createModel(string $name): void
  113. {
  114. $modelTemplate = str_replace(
  115. ['{{modelName}}', '{{tableNameSnakeCase}}'],
  116. [$name, $this->getTableName($name)],
  117. $this->getStub('Model'),
  118. );
  119. $this->put("app/Models/{$name}.php", $modelTemplate);
  120. $this->info("✓ Created Model: app/Models/{$name}.php");
  121. }
  122. protected function createRequest(string $name): void
  123. {
  124. $requestTemplate = str_replace(
  125. ['{{modelName}}'],
  126. [$name],
  127. $this->getStub('Request'),
  128. );
  129. $this->put("app/Http/Requests/{$name}Request.php", $requestTemplate);
  130. $this->info("✓ Created Request: app/Http/Requests/{$name}Request.php");
  131. }
  132. protected function createResource(string $name): void
  133. {
  134. $resourceTemplate = str_replace(
  135. ['{{modelName}}'],
  136. [$name],
  137. $this->getStub('Resource'),
  138. );
  139. $this->put("app/Http/Resources/{$name}Resource.php", $resourceTemplate);
  140. $this->info(
  141. "✓ Created Resource: app/Http/Resources/{$name}Resource.php",
  142. );
  143. }
  144. protected function createRoute(string $name): void
  145. {
  146. $routeTemplate = str_replace(
  147. ['{{modelName}}', '{{modelNameSnakeCase}}'],
  148. [$name, Str::snake($name, '-')],
  149. $this->getStub('Route'),
  150. );
  151. $routePath = base_path(
  152. 'routes/authRoutes/'.Str::snake($name).'.php',
  153. );
  154. if (! $this->files->isDirectory(base_path('routes/authRoutes'))) {
  155. $this->files->makeDirectory(
  156. base_path('routes/authRoutes'),
  157. 0755,
  158. true,
  159. );
  160. }
  161. $this->put($routePath, $routeTemplate);
  162. $this->info(
  163. '✓ Created Route: routes/authRoutes/'.Str::snake($name).'.php',
  164. );
  165. }
  166. protected function createService(string $name): void
  167. {
  168. $serviceTemplate = str_replace(
  169. ['{{modelName}}'],
  170. [$name],
  171. $this->getStub('Service'),
  172. );
  173. $this->put("app/Services/{$name}Service.php", $serviceTemplate);
  174. $this->info("✓ Created Service: app/Services/{$name}Service.php");
  175. }
  176. //
  177. protected function getStub(string $type): string
  178. {
  179. return $this->files->get(storage_path("stubs/{$type}.stub"));
  180. }
  181. protected function getTableName(string $name): string
  182. {
  183. return Str::snake(Str::plural($name));
  184. }
  185. protected function put(string $path, string $contents): void
  186. {
  187. $this->files->put($path, $contents);
  188. }
  189. }