Skip to content

Commit

Permalink
fully qualified class names for the name argument support
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 committed Mar 16, 2024
1 parent 8edf9f6 commit 9db1955
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Commands/MakeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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'));
Expand Down Expand Up @@ -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, '\\');
}
Expand Down
22 changes: 22 additions & 0 deletions tests/MakeRepositoryCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 9db1955

Please sign in to comment.