@@ -14,6 +14,8 @@ class FactoryGenerator implements Generator
14
14
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
15
15
private $ files ;
16
16
17
+ private $ imports = [];
18
+
17
19
public function __construct ($ files )
18
20
{
19
21
$ this ->files = $ files ;
@@ -27,6 +29,9 @@ public function output(array $tree): array
27
29
28
30
/** @var \Blueprint\Models\Model $model */
29
31
foreach ($ tree ['models ' ] as $ model ) {
32
+ $ this ->addImport ($ model , 'Faker\Generator as Faker ' );
33
+ $ this ->addImport ($ model , $ model ->fullyQualifiedClassName ());
34
+
30
35
$ path = $ this ->getPath ($ model );
31
36
32
37
if (!$ this ->files ->exists (dirname ($ path ))) {
@@ -53,9 +58,9 @@ protected function getPath(Model $model)
53
58
54
59
protected function populateStub (string $ stub , Model $ model )
55
60
{
56
- $ stub = str_replace ('DummyModel ' , $ model ->fullyQualifiedClassName (), $ stub );
57
61
$ stub = str_replace ('DummyClass ' , $ model ->name (), $ stub );
58
62
$ stub = str_replace ('// definition... ' , $ this ->buildDefinition ($ model ), $ stub );
63
+ $ stub = str_replace ('// imports... ' , $ this ->buildImports ($ model ), $ stub );
59
64
60
65
return $ stub ;
61
66
}
@@ -144,6 +149,11 @@ protected function buildDefinition(Model $model)
144
149
}
145
150
$ definition .= sprintf ('%s%s => $faker->%s,%s ' , self ::INDENT , "' {$ column ->name ()}_id' " , self ::fakerDataType ('id ' ), PHP_EOL );
146
151
$ definition .= sprintf ('%s%s => $faker->%s,%s ' , self ::INDENT , "' {$ column ->name ()}_type' " , self ::fakerDataType ('string ' ), PHP_EOL );
152
+ } elseif ($ column ->dataType () === 'rememberToken ' ) {
153
+ $ this ->addImport ($ model , 'Illuminate\Support\Str ' );
154
+ $ definition .= self ::INDENT . "' {$ column ->name ()}' => " ;
155
+ $ definition .= 'Str::random(10) ' ;
156
+ $ definition .= ', ' . PHP_EOL ;
147
157
} else {
148
158
$ definition .= self ::INDENT . "' {$ column ->name ()}' => " ;
149
159
$ faker = self ::fakerData ($ column ->name ()) ?? self ::fakerDataType ($ column ->dataType ());
@@ -155,6 +165,21 @@ protected function buildDefinition(Model $model)
155
165
return trim ($ definition );
156
166
}
157
167
168
+ private function addImport (Model $ model , $ class )
169
+ {
170
+ $ this ->imports [$ model ->name ()][] = $ class ;
171
+ }
172
+
173
+ private function buildImports (Model $ model )
174
+ {
175
+ $ imports = array_unique ($ this ->imports [$ model ->name ()]);
176
+ sort ($ imports );
177
+
178
+ return implode (PHP_EOL , array_map (function ($ class ) {
179
+ return 'use ' . $ class . '; ' ;
180
+ }, $ imports ));
181
+ }
182
+
158
183
private function fillableColumns (array $ columns ): array
159
184
{
160
185
if (config ('blueprint.fake_nullables ' )) {
0 commit comments