Skip to content

Commit 22bc32f

Browse files
[11.x] Feat: factory generic in make:model command (#52855)
* 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 <[email protected]>
1 parent 1f7b884 commit 22bc32f

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/Illuminate/Foundation/Console/ModelMakeCommand.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,43 @@ protected function getDefaultNamespace($rootNamespace)
205205
return is_dir(app_path('Models')) ? $rootNamespace.'\\Models' : $rootNamespace;
206206
}
207207

208+
/**
209+
* Build the class with the given name.
210+
*
211+
* @param string $name
212+
* @return string
213+
*
214+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
215+
*/
216+
protected function buildClass($name)
217+
{
218+
$replace = [];
219+
220+
if ($this->option('factory')) {
221+
$replace['{{ factoryDocBlock }}'] = $this->buildFactoryReplacements();
222+
} else {
223+
$replace["\n {{ factoryDocBlock }}"] = '';
224+
}
225+
226+
return str_replace(
227+
array_keys($replace), array_values($replace), parent::buildClass($name)
228+
);
229+
}
230+
231+
/**
232+
* Build the replacements for a factory DocBlock.
233+
*
234+
* @return string
235+
*/
236+
protected function buildFactoryReplacements()
237+
{
238+
$factoryNamespace = '\\Database\\Factories\\'.Str::studly($this->argument('name')).'Factory';
239+
240+
return <<<EOT
241+
/** @use HasFactory<$factoryNamespace> */
242+
EOT;
243+
}
244+
208245
/**
209246
* Get the console command options.
210247
*

src/Illuminate/Foundation/Console/stubs/model.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ use Illuminate\Database\Eloquent\Model;
77

88
class {{ class }} extends Model
99
{
10+
{{ factoryDocBlock }}
1011
use HasFactory;
1112
}

tests/Integration/Generators/ModelMakeCommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public function testItCanGenerateModelFile()
2525
'class Foo extends Model',
2626
], 'app/Models/Foo.php');
2727

28+
$this->assertFileDoesNotContains([
29+
'{{ factoryDocBlock }}',
30+
'/** @use HasFactory<\Database\Factories\FooFactory> */',
31+
], 'app/Models/Foo.php');
32+
2833
$this->assertFilenameNotExists('app/Http/Controllers/FooController.php');
2934
$this->assertFilenameNotExists('database/factories/FooFactory.php');
3035
$this->assertFilenameNotExists('database/seeders/FooSeeder.php');
@@ -96,6 +101,8 @@ public function testItCanGenerateModelFileWithFactoryOption()
96101
'namespace App\Models;',
97102
'use Illuminate\Database\Eloquent\Model;',
98103
'class Foo extends Model',
104+
'/** @use HasFactory<\Database\Factories\FooFactory> */',
105+
'use HasFactory;',
99106
], 'app/Models/Foo.php');
100107

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

0 commit comments

Comments
 (0)