CreateCrud.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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()
  16. {
  17. $name = $this->argument('name');
  18. $this->createModel($name);
  19. $this->createRepositoryInterface($name);
  20. $this->createRepository($name);
  21. $this->createService($name);
  22. $this->createController($name);
  23. $this->createDto($name);
  24. $this->createRequest($name);
  25. $this->createResource($name);
  26. $this->createCollection($name);
  27. $this->updateAppServiceProvider($name);
  28. $this->info('CRUD operations created successfully!');
  29. }
  30. protected function createModel($name)
  31. {
  32. $modelTemplate = str_replace(
  33. ['{{modelName}}'],
  34. [$name],
  35. $this->getStub('Model')
  36. );
  37. $this->put("app/Models/{$name}.php", $modelTemplate);
  38. }
  39. protected function createRepositoryInterface($name)
  40. {
  41. $interfaceTemplate = str_replace(
  42. ['{{modelName}}'],
  43. [$name],
  44. $this->getStub('RepositoryInterface')
  45. );
  46. $this->put("app/Repositories/{$name}RepositoryInterface.php", $interfaceTemplate);
  47. }
  48. protected function createRepository($name)
  49. {
  50. $repositoryTemplate = str_replace(
  51. ['{{modelName}}'],
  52. [$name],
  53. $this->getStub('Repository')
  54. );
  55. $this->put("app/Repositories/{$name}Repository.php", $repositoryTemplate);
  56. }
  57. protected function createService($name)
  58. {
  59. $serviceTemplate = str_replace(
  60. ['{{modelName}}'],
  61. [$name],
  62. $this->getStub('Service')
  63. );
  64. $this->put("app/Services/{$name}Service.php", $serviceTemplate);
  65. }
  66. protected function createController($name)
  67. {
  68. $controllerTemplate = str_replace(
  69. ['{{modelName}}'],
  70. [$name],
  71. $this->getStub('Controller')
  72. );
  73. $this->put("app/Http/Controllers/{$name}Controller.php", $controllerTemplate);
  74. }
  75. protected function createDto($name)
  76. {
  77. $dtoTemplate = str_replace(
  78. ['{{modelName}}'],
  79. [$name],
  80. $this->getStub('Dto')
  81. );
  82. $this->put("app/DataTransferObjects/{$name}Dto.php", $dtoTemplate);
  83. }
  84. protected function createRequest($name)
  85. {
  86. $requestTemplate = str_replace(
  87. ['{{modelName}}'],
  88. [$name],
  89. $this->getStub('Request')
  90. );
  91. $this->put("app/Http/Requests/{$name}Request.php", $requestTemplate);
  92. }
  93. protected function createResource($name)
  94. {
  95. $resourceTemplate = str_replace(
  96. ['{{modelName}}'],
  97. [$name],
  98. $this->getStub('Resource')
  99. );
  100. $this->put("app/Http/Resources/{$name}Resource.php", $resourceTemplate);
  101. }
  102. protected function createCollection($name)
  103. {
  104. $collectionTemplate = str_replace(
  105. ['{{modelName}}'],
  106. [$name],
  107. $this->getStub('Collection')
  108. );
  109. $this->put("app/Http/Resources/{$name}Collection.php", $collectionTemplate);
  110. }
  111. protected function updateAppServiceProvider($name)
  112. {
  113. $providerPath = app_path('Providers/AppServiceProvider.php');
  114. $content = $this->files->get($providerPath);
  115. // Add binding
  116. $binding = " {$name}RepositoryInterface::class => {$name}Repository::class,";
  117. $content = preg_replace(
  118. '/(public \$bindings = \[)/',
  119. "$1\n$binding",
  120. $content
  121. );
  122. // Add use statement
  123. $useStatement = "use App\Repositories\\{$name}RepositoryInterface;\nuse App\Repositories\\{$name}Repository;";
  124. $content = preg_replace(
  125. '/(use Illuminate\\\Support\\\ServiceProvider;)/',
  126. "$1\n\n{$useStatement}",
  127. $content
  128. );
  129. $this->files->put($providerPath, $content);
  130. }
  131. protected function getStub($type)
  132. {
  133. return $this->files->get(storage_path("stubs/{$type}.stub"));
  134. }
  135. protected function put($path, $contents)
  136. {
  137. $this->files->put($path, $contents);
  138. }
  139. }