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 13, 2024
1 parent 8edf9f6 commit 081f9a8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
14 changes: 10 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::before($name, '\\');
} else {
$modelName = Str::studly(class_basename($name));
$modelNamespace = 'App\\Models';
}

// Configuration-based paths
$repositoryPath = config('repository.path', app_path('Repositories'));
Expand Down Expand Up @@ -60,7 +66,7 @@ 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);

Expand Down
17 changes: 17 additions & 0 deletions tests/MakeRepositoryCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,21 @@ public function testRepositoryStubGeneration(): void
$this->assertFileExists($file);
}
}

public function testRepositoryStubGenerationWithFQCN(): void
{
$fqcn = 'App\Models\Special\\'.$this->model;

$this->artisan('make:repository', ['name' => $fqcn]);

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

0 comments on commit 081f9a8

Please sign in to comment.