File tree Expand file tree Collapse file tree 3 files changed +45
-0
lines changed
src/Illuminate/Foundation/Console
tests/Integration/Generators Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -205,6 +205,43 @@ protected function getDefaultNamespace($rootNamespace)
205
205
return is_dir (app_path ('Models ' )) ? $ rootNamespace .'\\Models ' : $ rootNamespace ;
206
206
}
207
207
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
+
208
245
/**
209
246
* Get the console command options.
210
247
*
Original file line number Diff line number Diff line change @@ -7,5 +7,6 @@ use Illuminate\Database\Eloquent\Model;
7
7
8
8
class {{ class }} extends Model
9
9
{
10
+ {{ factoryDocBlock }}
10
11
use HasFactory;
11
12
}
Original file line number Diff line number Diff line change @@ -25,6 +25,11 @@ public function testItCanGenerateModelFile()
25
25
'class Foo extends Model ' ,
26
26
], 'app/Models/Foo.php ' );
27
27
28
+ $ this ->assertFileDoesNotContains ([
29
+ '{{ factoryDocBlock }} ' ,
30
+ '/** @use HasFactory<\Database\Factories\FooFactory> */ ' ,
31
+ ], 'app/Models/Foo.php ' );
32
+
28
33
$ this ->assertFilenameNotExists ('app/Http/Controllers/FooController.php ' );
29
34
$ this ->assertFilenameNotExists ('database/factories/FooFactory.php ' );
30
35
$ this ->assertFilenameNotExists ('database/seeders/FooSeeder.php ' );
@@ -96,6 +101,8 @@ public function testItCanGenerateModelFileWithFactoryOption()
96
101
'namespace App\Models; ' ,
97
102
'use Illuminate\Database\Eloquent\Model; ' ,
98
103
'class Foo extends Model ' ,
104
+ '/** @use HasFactory<\Database\Factories\FooFactory> */ ' ,
105
+ 'use HasFactory; ' ,
99
106
], 'app/Models/Foo.php ' );
100
107
101
108
$ this ->assertFilenameNotExists ('app/Http/Controllers/FooController.php ' );
You can’t perform that action at this time.
0 commit comments