diff --git a/src/Commands/MakeRepository.php b/src/Commands/MakeRepository.php index 81563f4..980d133 100644 --- a/src/Commands/MakeRepository.php +++ b/src/Commands/MakeRepository.php @@ -10,7 +10,7 @@ class MakeRepository extends Command { - protected $signature = 'make:repository {name : The name of the model}'; + protected $signature = 'make:repository {name : The (fully qualified) name of the model}'; protected $description = 'Creates a new repository, interface, and filter for the specified model'; @@ -22,8 +22,14 @@ public function __construct(private readonly Filesystem $filesystem) public function handle(): void { $name = $this->argument('name'); - $modelName = Str::studly(class_basename($name)); - $modelNamespace = 'App\\Models'; + + if (Str::contains($name, '\\')) { + $modelName = Str::afterLast($name, '\\'); + $modelNamespace = Str::beforeLast($name, '\\'); + } else { + $modelName = Str::studly(class_basename($name)); + $modelNamespace = app()->getNamespace().'Models'; + } // Configuration-based paths $repositoryPath = config('repository.path', app_path('Repositories')); @@ -60,9 +66,10 @@ protected function ensureDirectoryExists(string $path): void protected function pathToNamespace(string $path): string { - $appNamespace = 'App'; + $appNamespace = app()->getNamespace(); $relativePath = str_replace(app_path(), '', $path); $namespace = str_replace(['/', '\\'], '\\', $relativePath); + $namespace = ltrim($namespace, '\\'); return trim($appNamespace.$namespace, '\\'); } diff --git a/tests/MakeRepositoryCommandTest.php b/tests/MakeRepositoryCommandTest.php index bd3e23b..5afd507 100644 --- a/tests/MakeRepositoryCommandTest.php +++ b/tests/MakeRepositoryCommandTest.php @@ -39,4 +39,26 @@ public function testRepositoryStubGeneration(): void $this->assertFileExists($file); } } + + public function testRepositoryStubGenerationWithFQCN(): void + { + $fqcn = 'App\Models\Special\\'.$this->model; + + $this->artisan('make:repository', ['name' => $fqcn]); + + $expectedNamespace = 'namespace App\Repositories'; + + $expectedFiles = [ + "{$this->basePath}/{$this->model}Repository.php", + "{$this->basePath}/Contracts/{$this->model}RepositoryInterface.php", + "{$this->basePath}/Filters/{$this->model}Filter.php", + ]; + + foreach ($expectedFiles as $file) { + $this->assertFileExists($file); + + $content = file_get_contents($file); + $this->assertStringContainsString($expectedNamespace, $content); + } + } }