| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace App\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Filesystem\Filesystem;
- use Illuminate\Support\Str;
- 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 = Str::studly($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->createRoute(name: $name);
- $this->createMigration(name: $name);
- $this->info(string: 'CRUD operations created successfully!');
- }
- protected function createModel($name): void
- {
- $tableNameSnakeCase = Str::snake(Str::plural($name));
- $modelTemplate = str_replace(
- search: ['{{modelName}}', '{{tableNameSnakeCase}}'],
- replace: [$name, $tableNameSnakeCase],
- 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 createRoute($name): void
- {
- $routeTemplate = str_replace(
- search: [
- '{{modelName}}',
- '{{modelNameSnakeCase}}'
- ],
- replace: [
- $name,
- Str::snake($name, '-')
- ],
- subject: $this->getStub(type: 'Route')
- );
- $routePath = base_path("routes/authRoutes/" . Str::snake($name) . ".php");
- if (!$this->files->isDirectory(base_path('routes/authRoutes'))) {
- $this->files->makeDirectory(base_path('routes/authRoutes'), 0755, true);
- }
- $this->put(path: $routePath, contents: $routeTemplate);
- }
- protected function createMigration($name): void
- {
- $tableNameSnakeCase = Str::snake(Str::plural($name));
- $migrationTemplate = str_replace(
- search: ['{{tableNameSnakeCase}}'],
- replace: [$tableNameSnakeCase],
- subject: $this->getStub(type: 'Migration')
- );
- $timestamp = date('Y_m_d_His');
- $filename = $timestamp . "_create_{$tableNameSnakeCase}_table.php";
- $this->put(
- path: database_path("migrations/{$filename}"),
- contents: $migrationTemplate
- );
- }
- 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);
- }
- }
|