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); } }