CreateCrud.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Filesystem\Filesystem;
  5. class CreateCrud extends Command
  6. {
  7. protected $signature = 'create:crud {name}';
  8. protected $description = 'Create CRUD operations including Model, Repository, Service, Controller, DTO, Request, Resource, and Collection';
  9. protected $files;
  10. public function __construct(Filesystem $files)
  11. {
  12. parent::__construct();
  13. $this->files = $files;
  14. }
  15. public function handle(): void
  16. {
  17. $name = $this->argument(key: 'name');
  18. $this->createModel(name: $name);
  19. $this->createRepositoryInterface(name: $name);
  20. $this->createRepository(name: $name);
  21. $this->createService(name: $name);
  22. $this->createController(name: $name);
  23. $this->createDTO(name: $name);
  24. $this->createRequest(name: $name);
  25. $this->createResource(name: $name);
  26. $this->updateAppServiceProvider(name: $name);
  27. $this->info(string: 'CRUD operations created successfully!');
  28. }
  29. protected function createModel($name): void
  30. {
  31. $modelTemplate = str_replace(
  32. search: ['{{modelName}}'],
  33. replace: [$name],
  34. subject: $this->getStub(type: 'Model')
  35. );
  36. $this->put(path: "app/Models/{$name}.php", contents: $modelTemplate);
  37. }
  38. protected function createRepositoryInterface($name): void
  39. {
  40. $interfaceTemplate = str_replace(
  41. search: ['{{modelName}}'],
  42. replace: [$name],
  43. subject: $this->getStub(type: 'RepositoryInterface')
  44. );
  45. $this->put(path: "app/Repositories/{$name}RepositoryInterface.php", contents: $interfaceTemplate);
  46. }
  47. protected function createRepository($name): void
  48. {
  49. $repositoryTemplate = str_replace(
  50. search: ['{{modelName}}'],
  51. replace: [$name],
  52. subject: $this->getStub(type: 'Repository')
  53. );
  54. $this->put(path: "app/Repositories/{$name}Repository.php", contents: $repositoryTemplate);
  55. }
  56. protected function createService($name): void
  57. {
  58. $serviceTemplate = str_replace(
  59. search: ['{{modelName}}'],
  60. replace: [$name],
  61. subject: $this->getStub(type: 'Service')
  62. );
  63. $this->put(path: "app/Services/{$name}Service.php", contents: $serviceTemplate);
  64. }
  65. protected function createController($name): void
  66. {
  67. $controllerTemplate = str_replace(
  68. search: ['{{modelName}}'],
  69. replace: [$name],
  70. subject: $this->getStub(type: 'Controller')
  71. );
  72. $this->put(path: "app/Http/Controllers/{$name}Controller.php", contents: $controllerTemplate);
  73. }
  74. protected function createDTO($name): void
  75. {
  76. $dtoTemplate = str_replace(
  77. search: ['{{modelName}}'],
  78. replace: [$name],
  79. subject: $this->getStub(type: 'DTO')
  80. );
  81. $this->put(path: "app/DTO/{$name}DTO.php", contents: $dtoTemplate);
  82. }
  83. protected function createRequest($name): void
  84. {
  85. $requestTemplate = str_replace(
  86. search: ['{{modelName}}'],
  87. replace: [$name],
  88. subject: $this->getStub(type: 'Request')
  89. );
  90. $this->put(path: "app/Http/Requests/{$name}Request.php", contents: $requestTemplate);
  91. }
  92. protected function createResource($name): void
  93. {
  94. $resourceTemplate = str_replace(
  95. search: ['{{modelName}}'],
  96. replace: [$name],
  97. subject: $this->getStub(type: 'Resource')
  98. );
  99. $this->put(path: "app/Http/Resources/{$name}Resource.php", contents: $resourceTemplate);
  100. }
  101. protected function updateAppServiceProvider($name): void
  102. {
  103. $providerPath = app_path(path: 'Providers/AppServiceProvider.php');
  104. $content = $this->files->get(path: $providerPath);
  105. // Add binding
  106. $binding = " {$name}RepositoryInterface::class => {$name}Repository::class,";
  107. $content = preg_replace(
  108. pattern: '/(public \$bindings = \[)/',
  109. replacement: "$1\n$binding",
  110. subject: $content
  111. );
  112. // Add use statement
  113. $useStatement = "use App\Repositories\\{$name}RepositoryInterface;\nuse App\Repositories\\{$name}Repository;";
  114. $content = preg_replace(
  115. pattern: '/(use Illuminate\\\Support\\\ServiceProvider;)/',
  116. replacement: "$1\n\n{$useStatement}",
  117. subject: $content
  118. );
  119. $this->files->put(path: $providerPath, contents: $content);
  120. }
  121. protected function getStub($type): string
  122. {
  123. return $this->files->get(path: storage_path(path: "stubs/{$type}.stub"));
  124. }
  125. protected function put($path, $contents): void
  126. {
  127. $this->files->put(path: $path, contents: $contents);
  128. }
  129. }