@@ -46,11 +46,18 @@ class FactoryBuilder
46
46
protected $ states ;
47
47
48
48
/**
49
- * The model callbacks.
49
+ * The model after making callbacks.
50
50
*
51
51
* @var array
52
52
*/
53
- protected $ after = [];
53
+ protected $ afterMaking = [];
54
+
55
+ /**
56
+ * The model after creating callbacks.
57
+ *
58
+ * @var array
59
+ */
60
+ protected $ afterCreating = [];
54
61
55
62
/**
56
63
* The states to apply.
@@ -80,17 +87,21 @@ class FactoryBuilder
80
87
* @param string $name
81
88
* @param array $definitions
82
89
* @param array $states
90
+ * @param array $afterMaking
91
+ * @param array $afterCreating
83
92
* @param \Faker\Generator $faker
84
93
* @return void
85
94
*/
86
- public function __construct ($ class , $ name , array $ definitions , array $ states , array $ after , Faker $ faker )
95
+ public function __construct ($ class , $ name , array $ definitions , array $ states ,
96
+ array $ afterMaking , array $ afterCreating , Faker $ faker )
87
97
{
88
98
$ this ->name = $ name ;
89
99
$ this ->class = $ class ;
90
100
$ this ->faker = $ faker ;
91
101
$ this ->states = $ states ;
92
102
$ this ->definitions = $ definitions ;
93
- $ this ->after = $ after ;
103
+ $ this ->afterMaking = $ afterMaking ;
104
+ $ this ->afterCreating = $ afterCreating ;
94
105
}
95
106
96
107
/**
@@ -157,10 +168,12 @@ public function create(array $attributes = [])
157
168
158
169
if ($ results instanceof Model) {
159
170
$ this ->store (collect ([$ results ]));
160
- $ this ->applyAfter (collect ([$ results ]), 'create ' );
171
+
172
+ $ this ->callAfterCreating (collect ([$ results ]));
161
173
} else {
162
174
$ this ->store ($ results );
163
- $ this ->applyAfter ($ results , 'create ' );
175
+
176
+ $ this ->callAfterCreating ($ results );
164
177
}
165
178
166
179
return $ results ;
@@ -192,10 +205,9 @@ protected function store($results)
192
205
public function make (array $ attributes = [])
193
206
{
194
207
if ($ this ->amount === null ) {
195
- $ instance = $ this ->makeInstance ($ attributes );
196
- $ this ->applyAfter (collect ([$ instance ]), 'make ' );
197
-
198
- return $ instance ;
208
+ return tap ($ this ->makeInstance ($ attributes ), function ($ instance ) {
209
+ $ this ->callAfterMaking (collect ([$ instance ]));
210
+ });
199
211
}
200
212
201
213
if ($ this ->amount < 1 ) {
@@ -205,7 +217,8 @@ public function make(array $attributes = [])
205
217
$ instances = (new $ this ->class )->newCollection (array_map (function () use ($ attributes ) {
206
218
return $ this ->makeInstance ($ attributes );
207
219
}, range (1 , $ this ->amount )));
208
- $ this ->applyAfter ($ instances , 'make ' );
220
+
221
+ $ this ->callAfterMaking ($ instances );
209
222
210
223
return $ instances ;
211
224
}
@@ -346,20 +359,36 @@ protected function expandAttributes(array $attributes)
346
359
}
347
360
348
361
/**
349
- * Run after callback on a collection of models.
362
+ * Run after making callbacks on a collection of models.
350
363
*
351
364
* @param \Illuminate\Support\Collection $models
352
- * @param string $action
353
365
* @return void
354
366
*/
355
- public function applyAfter ($ models, $ action )
367
+ public function callAfterMaking ($ models )
356
368
{
357
- $ models ->each (function ($ model ) use ($ action ) {
358
- if (! isset ($ this ->after [$ this ->class ][$ action ])) {
359
- return ;
369
+ $ models ->each (function ($ model ) {
370
+ if (isset ($ this ->afterMaking [$ this ->class ])) {
371
+ foreach ($ this ->afterMaking [$ this ->class ] as $ callback ) {
372
+ $ callback ($ model , $ this ->faker );
373
+ }
360
374
}
375
+ });
376
+ }
361
377
362
- call_user_func_array ($ this ->after [$ this ->class ][$ action ], [$ model , $ this ->faker ]);
378
+ /**
379
+ * Run after creating callbacks on a collection of models.
380
+ *
381
+ * @param \Illuminate\Support\Collection $models
382
+ * @return void
383
+ */
384
+ public function callAfterCreating ($ models )
385
+ {
386
+ $ models ->each (function ($ model ) {
387
+ if (isset ($ this ->afterCreating [$ this ->class ])) {
388
+ foreach ($ this ->afterCreating [$ this ->class ] as $ callback ) {
389
+ $ callback ($ model , $ this ->faker );
390
+ }
391
+ }
363
392
});
364
393
}
365
394
}
0 commit comments