CreateCrud.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 {name}';
  9. protected $description = 'Create CRUD operations including Model, Repository, Service, Controller, DTO, Request, Resource, and Collection';
  10. protected $files;
  11. public function __construct(Filesystem $files)
  12. {
  13. parent::__construct();
  14. $this->files = $files;
  15. }
  16. public function handle(): void
  17. {
  18. $name = Str::studly($this->argument(key: 'name'));
  19. $this->createModel(name: $name);
  20. $this->createRepositoryInterface(name: $name);
  21. $this->createRepository(name: $name);
  22. $this->createService(name: $name);
  23. $this->createController(name: $name);
  24. $this->createDTO(name: $name);
  25. $this->createRequest(name: $name);
  26. $this->createResource(name: $name);
  27. $this->updateAppServiceProvider(name: $name);
  28. $this->createRoute(name: $name);
  29. $this->createMigration(name: $name);
  30. $this->info(string: 'CRUD operations created successfully!');
  31. }
  32. protected function createModel($name): void
  33. {
  34. $tableNameSnakeCase = Str::snake(Str::plural($name));
  35. $modelTemplate = str_replace(
  36. search: ['{{modelName}}', '{{tableNameSnakeCase}}'],
  37. replace: [$name, $tableNameSnakeCase],
  38. subject: $this->getStub(type: 'Model')
  39. );
  40. $this->put(path: "app/Models/{$name}.php", contents: $modelTemplate);
  41. }
  42. protected function createRepositoryInterface($name): void
  43. {
  44. $interfaceTemplate = str_replace(
  45. search: ['{{modelName}}'],
  46. replace: [$name],
  47. subject: $this->getStub(type: 'RepositoryInterface')
  48. );
  49. $this->put(path: "app/Repositories/{$name}RepositoryInterface.php", contents: $interfaceTemplate);
  50. }
  51. protected function createRepository($name): void
  52. {
  53. $repositoryTemplate = str_replace(
  54. search: ['{{modelName}}'],
  55. replace: [$name],
  56. subject: $this->getStub(type: 'Repository')
  57. );
  58. $this->put(path: "app/Repositories/{$name}Repository.php", contents: $repositoryTemplate);
  59. }
  60. protected function createService($name): void
  61. {
  62. $serviceTemplate = str_replace(
  63. search: ['{{modelName}}'],
  64. replace: [$name],
  65. subject: $this->getStub(type: 'Service')
  66. );
  67. $this->put(path: "app/Services/{$name}Service.php", contents: $serviceTemplate);
  68. }
  69. protected function createController($name): void
  70. {
  71. $controllerTemplate = str_replace(
  72. search: ['{{modelName}}'],
  73. replace: [$name],
  74. subject: $this->getStub(type: 'Controller')
  75. );
  76. $this->put(path: "app/Http/Controllers/{$name}Controller.php", contents: $controllerTemplate);
  77. }
  78. protected function createDTO($name): void
  79. {
  80. $dtoTemplate = str_replace(
  81. search: ['{{modelName}}'],
  82. replace: [$name],
  83. subject: $this->getStub(type: 'DTO')
  84. );
  85. $this->put(path: "app/DTO/{$name}DTO.php", contents: $dtoTemplate);
  86. }
  87. protected function createRequest($name): void
  88. {
  89. $requestTemplate = str_replace(
  90. search: ['{{modelName}}'],
  91. replace: [$name],
  92. subject: $this->getStub(type: 'Request')
  93. );
  94. $this->put(path: "app/Http/Requests/{$name}Request.php", contents: $requestTemplate);
  95. }
  96. protected function createResource($name): void
  97. {
  98. $resourceTemplate = str_replace(
  99. search: ['{{modelName}}'],
  100. replace: [$name],
  101. subject: $this->getStub(type: 'Resource')
  102. );
  103. $this->put(path: "app/Http/Resources/{$name}Resource.php", contents: $resourceTemplate);
  104. }
  105. protected function updateAppServiceProvider($name): void
  106. {
  107. $providerPath = app_path(path: 'Providers/AppServiceProvider.php');
  108. $content = $this->files->get(path: $providerPath);
  109. // Add binding
  110. $binding = " {$name}RepositoryInterface::class => {$name}Repository::class,";
  111. $content = preg_replace(
  112. pattern: '/(public \$bindings = \[)/',
  113. replacement: "$1\n$binding",
  114. subject: $content
  115. );
  116. // Add use statement
  117. $useStatement = "use App\Repositories\\{$name}RepositoryInterface;\nuse App\Repositories\\{$name}Repository;";
  118. $content = preg_replace(
  119. pattern: '/(use Illuminate\\\Support\\\ServiceProvider;)/',
  120. replacement: "$1\n\n{$useStatement}",
  121. subject: $content
  122. );
  123. $this->files->put(path: $providerPath, contents: $content);
  124. }
  125. protected function createRoute($name): void
  126. {
  127. $routeTemplate = str_replace(
  128. search: [
  129. '{{modelName}}',
  130. '{{modelNameSnakeCase}}'
  131. ],
  132. replace: [
  133. $name,
  134. Str::snake($name)
  135. ],
  136. subject: $this->getStub(type: 'Route')
  137. );
  138. $routePath = base_path("routes/authRoutes/" . strtolower($name) . ".php");
  139. if (!$this->files->isDirectory(base_path('routes/authRoutes'))) {
  140. $this->files->makeDirectory(base_path('routes/authRoutes'), 0755, true);
  141. }
  142. $this->put(path: $routePath, contents: $routeTemplate);
  143. }
  144. protected function createMigration($name): void
  145. {
  146. $tableNameSnakeCase = Str::snake(Str::plural($name));
  147. $migrationTemplate = str_replace(
  148. search: ['{{tableNameSnakeCase}}'],
  149. replace: [$tableNameSnakeCase],
  150. subject: $this->getStub(type: 'Migration')
  151. );
  152. $timestamp = date('Y_m_d_His');
  153. $filename = $timestamp . "_create_{$tableNameSnakeCase}_table.php";
  154. $this->put(
  155. path: database_path("migrations/{$filename}"),
  156. contents: $migrationTemplate
  157. );
  158. }
  159. protected function getStub($type): string
  160. {
  161. return $this->files->get(path: storage_path(path: "stubs/{$type}.stub"));
  162. }
  163. protected function put($path, $contents): void
  164. {
  165. $this->files->put(path: $path, contents: $contents);
  166. }
  167. }