| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace App\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Filesystem\Filesystem;
- class CreateCrud extends Command
- {
- protected $signature = 'create:crud {name}';
- protected $description = 'Create CRUD operations including Model, Repository, Service, Controller, DTO, Request, Resource, and Collection';
- protected $files;
- public function __construct(Filesystem $files)
- {
- parent::__construct();
- $this->files = $files;
- }
- public function handle(): void
- {
- $name = $this->argument(key: 'name');
- $this->createModel(name: $name);
- $this->createRepositoryInterface(name: $name);
- $this->createRepository(name: $name);
- $this->createService(name: $name);
- $this->createController(name: $name);
- $this->createDTO(name: $name);
- $this->createRequest(name: $name);
- $this->createResource(name: $name);
- $this->updateAppServiceProvider(name: $name);
- $this->info(string: 'CRUD operations created successfully!');
- }
- protected function createModel($name): void
- {
- $modelTemplate = str_replace(
- search: ['{{modelName}}'],
- replace: [$name],
- subject: $this->getStub(type: 'Model')
- );
- $this->put(path: "app/Models/{$name}.php", contents: $modelTemplate);
- }
- protected function createRepositoryInterface($name): void
- {
- $interfaceTemplate = str_replace(
- search: ['{{modelName}}'],
- replace: [$name],
- subject: $this->getStub(type: 'RepositoryInterface')
- );
- $this->put(path: "app/Repositories/{$name}RepositoryInterface.php", contents: $interfaceTemplate);
- }
- protected function createRepository($name): void
- {
- $repositoryTemplate = str_replace(
- search: ['{{modelName}}'],
- replace: [$name],
- subject: $this->getStub(type: 'Repository')
- );
- $this->put(path: "app/Repositories/{$name}Repository.php", contents: $repositoryTemplate);
- }
- protected function createService($name): void
- {
- $serviceTemplate = str_replace(
- search: ['{{modelName}}'],
- replace: [$name],
- subject: $this->getStub(type: 'Service')
- );
- $this->put(path: "app/Services/{$name}Service.php", contents: $serviceTemplate);
- }
- protected function createController($name): void
- {
- $controllerTemplate = str_replace(
- search: ['{{modelName}}'],
- replace: [$name],
- subject: $this->getStub(type: 'Controller')
- );
- $this->put(path: "app/Http/Controllers/{$name}Controller.php", contents: $controllerTemplate);
- }
- protected function createDTO($name): void
- {
- $dtoTemplate = str_replace(
- search: ['{{modelName}}'],
- replace: [$name],
- subject: $this->getStub(type: 'DTO')
- );
- $this->put(path: "app/DTO/{$name}DTO.php", contents: $dtoTemplate);
- }
- protected function createRequest($name): void
- {
- $requestTemplate = str_replace(
- search: ['{{modelName}}'],
- replace: [$name],
- subject: $this->getStub(type: 'Request')
- );
- $this->put(path: "app/Http/Requests/{$name}Request.php", contents: $requestTemplate);
- }
- protected function createResource($name): void
- {
- $resourceTemplate = str_replace(
- search: ['{{modelName}}'],
- replace: [$name],
- subject: $this->getStub(type: 'Resource')
- );
- $this->put(path: "app/Http/Resources/{$name}Resource.php", contents: $resourceTemplate);
- }
- protected function updateAppServiceProvider($name): void
- {
- $providerPath = app_path(path: 'Providers/AppServiceProvider.php');
- $content = $this->files->get(path: $providerPath);
- // Add binding
- $binding = " {$name}RepositoryInterface::class => {$name}Repository::class,";
- $content = preg_replace(
- pattern: '/(public \$bindings = \[)/',
- replacement: "$1\n$binding",
- subject: $content
- );
- // Add use statement
- $useStatement = "use App\Repositories\\{$name}RepositoryInterface;\nuse App\Repositories\\{$name}Repository;";
- $content = preg_replace(
- pattern: '/(use Illuminate\\\Support\\\ServiceProvider;)/',
- replacement: "$1\n\n{$useStatement}",
- subject: $content
- );
- $this->files->put(path: $providerPath, contents: $content);
- }
- protected function getStub($type): string
- {
- return $this->files->get(path: storage_path(path: "stubs/{$type}.stub"));
- }
- protected function put($path, $contents): void
- {
- $this->files->put(path: $path, contents: $contents);
- }
- }
|