Skip to content

Commit

Permalink
[11.x] Feat: factory generic in make:model command (#52855)
Browse files Browse the repository at this point in the history
* Refactor model generation to include factory doc block

* Refactor model generation to include factory doc block in ModelMakeCommandTest

* Refactor model generation to include factory doc block in ModelMakeCommandTest

* lint

* formatting

* Update ModelMakeCommand.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
MrPunyapal and taylorotwell authored Oct 1, 2024
1 parent 1f7b884 commit 22bc32f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Illuminate/Foundation/Console/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,43 @@ protected function getDefaultNamespace($rootNamespace)
return is_dir(app_path('Models')) ? $rootNamespace.'\\Models' : $rootNamespace;
}

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function buildClass($name)
{
$replace = [];

if ($this->option('factory')) {
$replace['{{ factoryDocBlock }}'] = $this->buildFactoryReplacements();
} else {
$replace["\n {{ factoryDocBlock }}"] = '';
}

return str_replace(
array_keys($replace), array_values($replace), parent::buildClass($name)
);
}

/**
* Build the replacements for a factory DocBlock.
*
* @return string
*/
protected function buildFactoryReplacements()
{
$factoryNamespace = '\\Database\\Factories\\'.Str::studly($this->argument('name')).'Factory';

return <<<EOT
/** @use HasFactory<$factoryNamespace> */
EOT;
}

/**
* Get the console command options.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Foundation/Console/stubs/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ use Illuminate\Database\Eloquent\Model;

class {{ class }} extends Model
{
{{ factoryDocBlock }}
use HasFactory;
}
7 changes: 7 additions & 0 deletions tests/Integration/Generators/ModelMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function testItCanGenerateModelFile()
'class Foo extends Model',
], 'app/Models/Foo.php');

$this->assertFileDoesNotContains([
'{{ factoryDocBlock }}',
'/** @use HasFactory<\Database\Factories\FooFactory> */',
], 'app/Models/Foo.php');

$this->assertFilenameNotExists('app/Http/Controllers/FooController.php');
$this->assertFilenameNotExists('database/factories/FooFactory.php');
$this->assertFilenameNotExists('database/seeders/FooSeeder.php');
Expand Down Expand Up @@ -96,6 +101,8 @@ public function testItCanGenerateModelFileWithFactoryOption()
'namespace App\Models;',
'use Illuminate\Database\Eloquent\Model;',
'class Foo extends Model',
'/** @use HasFactory<\Database\Factories\FooFactory> */',
'use HasFactory;',
], 'app/Models/Foo.php');

$this->assertFilenameNotExists('app/Http/Controllers/FooController.php');
Expand Down

0 comments on commit 22bc32f

Please sign in to comment.