@@ -46,11 +46,18 @@ class FactoryBuilder
4646 protected $ states ;
4747
4848 /**
49- * The model callbacks.
49+ * The model after making callbacks.
5050 *
5151 * @var array
5252 */
53- protected $ after = [];
53+ protected $ afterMaking = [];
54+
55+ /**
56+ * The model after creating callbacks.
57+ *
58+ * @var array
59+ */
60+ protected $ afterCreating = [];
5461
5562 /**
5663 * The states to apply.
@@ -80,17 +87,21 @@ class FactoryBuilder
8087 * @param string $name
8188 * @param array $definitions
8289 * @param array $states
90+ * @param array $afterMaking
91+ * @param array $afterCreating
8392 * @param \Faker\Generator $faker
8493 * @return void
8594 */
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 )
8797 {
8898 $ this ->name = $ name ;
8999 $ this ->class = $ class ;
90100 $ this ->faker = $ faker ;
91101 $ this ->states = $ states ;
92102 $ this ->definitions = $ definitions ;
93- $ this ->after = $ after ;
103+ $ this ->afterMaking = $ afterMaking ;
104+ $ this ->afterCreating = $ afterCreating ;
94105 }
95106
96107 /**
@@ -157,10 +168,12 @@ public function create(array $attributes = [])
157168
158169 if ($ results instanceof Model) {
159170 $ this ->store (collect ([$ results ]));
160- $ this ->applyAfter (collect ([$ results ]), 'create ' );
171+
172+ $ this ->callAfterCreating (collect ([$ results ]));
161173 } else {
162174 $ this ->store ($ results );
163- $ this ->applyAfter ($ results , 'create ' );
175+
176+ $ this ->callAfterCreating ($ results );
164177 }
165178
166179 return $ results ;
@@ -192,10 +205,9 @@ protected function store($results)
192205 public function make (array $ attributes = [])
193206 {
194207 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+ });
199211 }
200212
201213 if ($ this ->amount < 1 ) {
@@ -205,7 +217,8 @@ public function make(array $attributes = [])
205217 $ instances = (new $ this ->class )->newCollection (array_map (function () use ($ attributes ) {
206218 return $ this ->makeInstance ($ attributes );
207219 }, range (1 , $ this ->amount )));
208- $ this ->applyAfter ($ instances , 'make ' );
220+
221+ $ this ->callAfterMaking ($ instances );
209222
210223 return $ instances ;
211224 }
@@ -346,20 +359,36 @@ protected function expandAttributes(array $attributes)
346359 }
347360
348361 /**
349- * Run after callback on a collection of models.
362+ * Run after making callbacks on a collection of models.
350363 *
351364 * @param \Illuminate\Support\Collection $models
352- * @param string $action
353365 * @return void
354366 */
355- public function applyAfter ($ models, $ action )
367+ public function callAfterMaking ($ models )
356368 {
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+ }
360374 }
375+ });
376+ }
361377
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+ }
363392 });
364393 }
365394}
0 commit comments