2
2
3
3
namespace Blueprint \Generators ;
4
4
5
+ use Blueprint \Blueprint ;
5
6
use Blueprint \Contracts \Generator ;
6
7
use Blueprint \Models \Column ;
7
8
use Blueprint \Models \Model ;
8
9
use Blueprint \Tree ;
9
- use Illuminate \Support \Facades \App ;
10
10
use Illuminate \Support \Str ;
11
11
use Shift \Faker \Registry as FakerRegistry ;
12
12
@@ -28,16 +28,15 @@ public function output(Tree $tree): array
28
28
{
29
29
$ output = [];
30
30
31
-
32
- if ($ this ->isLaravel8Up ()) {
31
+ if (Blueprint::isLaravel8OrHigher ()) {
33
32
$ stub = $ this ->files ->stub ('factory.stub ' );
34
33
} else {
35
34
$ stub = $ this ->files ->stub ('factory.closure.stub ' );
36
35
}
37
36
38
37
/** @var \Blueprint\Models\Model $model */
39
38
foreach ($ tree ->models () as $ model ) {
40
- if (! $ this -> isLaravel8Up ()) {
39
+ if (! Blueprint:: isLaravel8OrHigher ()) {
41
40
$ this ->addImport ($ model , 'Faker\Generator as Faker ' );
42
41
}
43
42
$ this ->addImport ($ model , $ model ->fullyQualifiedClassName ());
@@ -75,7 +74,7 @@ protected function populateStub(string $stub, Model $model)
75
74
{
76
75
$ stub = str_replace ('{{ model }} ' , $ model ->name (), $ stub );
77
76
$ stub = str_replace ('// ' , $ this ->buildDefinition ($ model ), $ stub );
78
- if ($ this -> isLaravel8Up ()) {
77
+ if (Blueprint:: isLaravel8OrHigher ()) {
79
78
$ stub = str_replace ('use {{ namespacedModel }}; ' , $ this ->buildImports ($ model ), $ stub );
80
79
} else {
81
80
$ stub = str_replace ('use Faker\Generator as Faker; ' .PHP_EOL .'use {{ namespacedModel }}; ' , $ this ->buildImports ($ model ), $ stub );
@@ -121,11 +120,11 @@ protected function buildDefinition(Model $model)
121
120
122
121
$ class = Str::studly (Str::singular ($ table ));
123
122
124
- if ($ this -> isLaravel8Up ()) {
123
+ if (Blueprint:: isLaravel8OrHigher ()) {
125
124
$ this ->addImport ($ model , $ model ->fullyQualifiedNamespace ().'\\' .$ class );
126
125
}
127
126
if ($ key === 'id ' ) {
128
- if ($ this -> isLaravel8Up ()) {
127
+ if (Blueprint:: isLaravel8OrHigher ()) {
129
128
$ definition .= str_repeat (self ::INDENT , 3 )."' {$ column ->name ()}' => " ;
130
129
$ definition .= sprintf ('%s::factory() ' , $ class );
131
130
$ definition .= ', ' .PHP_EOL ;
@@ -135,7 +134,7 @@ protected function buildDefinition(Model $model)
135
134
$ definition .= ', ' .PHP_EOL ;
136
135
}
137
136
} else {
138
- if ($ this -> isLaravel8Up ()) {
137
+ if (Blueprint:: isLaravel8OrHigher ()) {
139
138
$ definition .= str_repeat (self ::INDENT , 3 )."' {$ column ->name ()}' => " ;
140
139
$ definition .= sprintf ('%s::factory()->create()->%s ' , $ class , $ key );
141
140
$ definition .= ', ' .PHP_EOL ;
@@ -151,7 +150,7 @@ protected function buildDefinition(Model $model)
151
150
$ name = Str::beforeLast ($ column ->name (), '_id ' );
152
151
$ class = Str::studly ($ column ->attributes ()[0 ] ?? $ name );
153
152
154
- if ($ this -> isLaravel8Up ()) {
153
+ if (Blueprint:: isLaravel8OrHigher ()) {
155
154
$ this ->addImport ($ model , $ model ->fullyQualifiedNamespace ().'\\' .$ class );
156
155
$ definition .= str_repeat (self ::INDENT , 3 )."' {$ column ->name ()}' => " ;
157
156
$ definition .= sprintf ('%s::factory() ' , $ class );
@@ -162,7 +161,7 @@ protected function buildDefinition(Model $model)
162
161
$ definition .= ', ' .PHP_EOL ;
163
162
} elseif (in_array ($ column ->dataType (), ['enum ' , 'set ' ]) && ! empty ($ column ->attributes ())) {
164
163
$ faker = FakerRegistry::fakerData ($ column ->name ()) ?? FakerRegistry::fakerDataType ($ column ->dataType ());
165
- if ($ this -> isLaravel8Up ()) {
164
+ if (Blueprint:: isLaravel8OrHigher ()) {
166
165
$ definition .= str_repeat (self ::INDENT , 3 )."' {$ column ->name ()}' => " ;
167
166
$ definition .= '$this->faker-> ' .$ faker ;
168
167
} else {
@@ -177,7 +176,7 @@ protected function buildDefinition(Model $model)
177
176
);
178
177
} elseif (in_array ($ column ->dataType (), ['decimal ' , 'double ' , 'float ' ])) {
179
178
$ faker = FakerRegistry::fakerData ($ column ->name ()) ?? FakerRegistry::fakerDataType ($ column ->dataType ());
180
- if ($ this -> isLaravel8Up ()) {
179
+ if (Blueprint:: isLaravel8OrHigher ()) {
181
180
$ definition .= str_repeat (self ::INDENT , 3 )."' {$ column ->name ()}' => " ;
182
181
$ definition .= '$this->faker-> ' .$ faker ;
183
182
} else {
@@ -196,7 +195,7 @@ protected function buildDefinition(Model $model)
196
195
);
197
196
} elseif (in_array ($ column ->dataType (), ['json ' , 'jsonb ' ])) {
198
197
$ default = $ column ->defaultValue () ?? "'{}' " ;
199
- if ($ this -> isLaravel8Up ()) {
198
+ if (Blueprint:: isLaravel8OrHigher ()) {
200
199
$ definition .= str_repeat (self ::INDENT , 3 )."' {$ column ->name ()}' => {$ default }, " .PHP_EOL ;
201
200
} else {
202
201
$ definition .= str_repeat (self ::INDENT , 2 )."' {$ column ->name ()}' => {$ default }, " .PHP_EOL ;
@@ -205,15 +204,15 @@ protected function buildDefinition(Model $model)
205
204
if ($ column ->isNullable ()) {
206
205
continue ;
207
206
}
208
- if ($ this -> isLaravel8Up ()) {
207
+ if (Blueprint:: isLaravel8OrHigher ()) {
209
208
$ definition .= sprintf ('%s%s => $this->faker->%s,%s ' , str_repeat (self ::INDENT , 3 ), "' {$ column ->name ()}_id' " , FakerRegistry::fakerDataType ('id ' ), PHP_EOL );
210
209
$ definition .= sprintf ('%s%s => $this->faker->%s,%s ' , str_repeat (self ::INDENT , 3 ), "' {$ column ->name ()}_type' " , FakerRegistry::fakerDataType ('string ' ), PHP_EOL );
211
210
} else {
212
211
$ definition .= sprintf ('%s%s => $faker->%s,%s ' , str_repeat (self ::INDENT , 2 ), "' {$ column ->name ()}_id' " , FakerRegistry::fakerDataType ('id ' ), PHP_EOL );
213
212
$ definition .= sprintf ('%s%s => $faker->%s,%s ' , str_repeat (self ::INDENT , 2 ), "' {$ column ->name ()}_type' " , FakerRegistry::fakerDataType ('string ' ), PHP_EOL );
214
213
}
215
214
} elseif ($ column ->dataType () === 'rememberToken ' ) {
216
- if ($ this -> isLaravel8Up ()) {
215
+ if (Blueprint:: isLaravel8OrHigher ()) {
217
216
$ definition .= str_repeat (self ::INDENT , 3 )."' {$ column ->name ()}' => " ;
218
217
} else {
219
218
$ this ->addImport ($ model , 'Illuminate\Support\Str ' );
@@ -222,7 +221,7 @@ protected function buildDefinition(Model $model)
222
221
$ definition .= 'Str::random(10) ' ;
223
222
$ definition .= ', ' .PHP_EOL ;
224
223
} else {
225
- if ($ this -> isLaravel8Up ()) {
224
+ if (Blueprint:: isLaravel8OrHigher ()) {
226
225
$ definition .= str_repeat (self ::INDENT , 3 )."' {$ column ->name ()}' => " ;
227
226
} else {
228
227
$ definition .= str_repeat (self ::INDENT , 2 )."' {$ column ->name ()}' => " ;
@@ -238,7 +237,7 @@ protected function buildDefinition(Model $model)
238
237
$ faker = 'word ' ;
239
238
}
240
239
241
- if ($ this -> isLaravel8Up ()) {
240
+ if (Blueprint:: isLaravel8OrHigher ()) {
242
241
$ definition .= '$this->faker-> ' .$ faker ;
243
242
} else {
244
243
$ definition .= '$faker-> ' .$ faker ;
@@ -275,9 +274,4 @@ private function fillableColumns(array $columns): array
275
274
return ! in_array ('nullable ' , $ column ->modifiers ());
276
275
});
277
276
}
278
-
279
- protected function isLaravel8Up ()
280
- {
281
- return version_compare (App::version (), '8.0.0 ' , '>= ' );
282
- }
283
277
}
0 commit comments