Skip to content

Commit d79509d

Browse files
committed
formatting and fixing
1 parent 36163b9 commit d79509d

File tree

3 files changed

+78
-31
lines changed

3 files changed

+78
-31
lines changed

src/Illuminate/Database/Eloquent/Factory.php

+29-11
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ class Factory implements ArrayAccess
2323
protected $states = [];
2424

2525
/**
26-
* The registered callbacks.
26+
* The registered after making callbacks.
2727
*
2828
* @var array
2929
*/
30-
protected $after = [];
30+
protected $afterMaking = [];
31+
32+
/**
33+
* The registered after creating callbacks.
34+
*
35+
* @var array
36+
*/
37+
protected $afterCreating = [];
3138

3239
/**
3340
* The Faker instance for the builder.
@@ -105,21 +112,29 @@ public function state($class, $state, $attributes)
105112
}
106113

107114
/**
108-
* Define a callback to run after an action.
115+
* Define a callback to run after making a model.
109116
*
110117
* @param string $class
111-
* @param string $action
112118
* @param callable $callback
113119
* @return $this
114120
*/
115-
public function after($class, $action, $callback = null)
121+
public function afterMaking($class, $callback)
116122
{
117-
if (is_callable($action) && $callback === null) {
118-
$callback = $action;
119-
$action = 'create';
120-
}
123+
$this->afterMaking[$class][] = $callback;
121124

122-
$this->after[$class][$action] = $callback;
125+
return $this;
126+
}
127+
128+
/**
129+
* Define a callback to run after creating a model.
130+
*
131+
* @param string $class
132+
* @param callable $callback
133+
* @return $this
134+
*/
135+
public function afterCreating($class, $callback)
136+
{
137+
$this->afterCreating[$class][] = $callback;
123138

124139
return $this;
125140
}
@@ -211,7 +226,10 @@ public function raw($class, array $attributes = [], $name = 'default')
211226
*/
212227
public function of($class, $name = 'default')
213228
{
214-
return new FactoryBuilder($class, $name, $this->definitions, $this->states, $this->after, $this->faker);
229+
return new FactoryBuilder(
230+
$class, $name, $this->definitions, $this->states,
231+
$this->afterMaking, $this->afterCreating, $this->faker
232+
);
215233
}
216234

217235
/**

src/Illuminate/Database/Eloquent/FactoryBuilder.php

+47-18
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/Integration/Database/EloquentFactoryBuilderTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function getEnvironmentSetUp($app)
4646
];
4747
});
4848

49-
$factory->after(FactoryBuildableUser::class, 'make', function (FactoryBuildableUser $user, Generator $faker) {
49+
$factory->afterMaking(FactoryBuildableUser::class, function (FactoryBuildableUser $user, Generator $faker) {
5050
$profile = factory(FactoryBuildableProfile::class)->make(['user_id' => $user->id]);
5151
$user->setRelation('profile', $profile);
5252
});
@@ -60,7 +60,7 @@ protected function getEnvironmentSetUp($app)
6060
];
6161
});
6262

63-
$factory->after(FactoryBuildableTeam::class, function (FactoryBuildableTeam $team, Generator $faker) {
63+
$factory->afterCreating(FactoryBuildableTeam::class, function (FactoryBuildableTeam $team, Generator $faker) {
6464
$team->users()->attach($team->owner);
6565
});
6666

0 commit comments

Comments
 (0)