Permalink
Browse files

formatting and fixing

  • Loading branch information...
taylorotwell committed Mar 12, 2018
1 parent 36163b9 commit d79509dfb82a8518ca0a0ccb9d4986cfa632b1ab
@@ -23,11 +23,18 @@ class Factory implements ArrayAccess
protected $states = [];
/**
* The registered callbacks.
* The registered after making callbacks.
*
* @var array
*/
protected $after = [];
protected $afterMaking = [];
/**
* The registered after creating callbacks.
*
* @var array
*/
protected $afterCreating = [];
/**
* The Faker instance for the builder.
@@ -105,21 +112,29 @@ public function state($class, $state, $attributes)
}
/**
* Define a callback to run after an action.
* Define a callback to run after making a model.
*
* @param string $class
* @param string $action
* @param callable $callback
* @return $this
*/
public function after($class, $action, $callback = null)
public function afterMaking($class, $callback)
{
if (is_callable($action) && $callback === null) {
$callback = $action;
$action = 'create';
}
$this->afterMaking[$class][] = $callback;
$this->after[$class][$action] = $callback;
return $this;
}
/**
* Define a callback to run after creating a model.
*
* @param string $class
* @param callable $callback
* @return $this
*/
public function afterCreating($class, $callback)
{
$this->afterCreating[$class][] = $callback;
return $this;
}
@@ -211,7 +226,10 @@ public function raw($class, array $attributes = [], $name = 'default')
*/
public function of($class, $name = 'default')
{
return new FactoryBuilder($class, $name, $this->definitions, $this->states, $this->after, $this->faker);
return new FactoryBuilder(
$class, $name, $this->definitions, $this->states,
$this->afterMaking, $this->afterCreating, $this->faker
);
}
/**
@@ -46,11 +46,18 @@ class FactoryBuilder
protected $states;
/**
* The model callbacks.
* The model after making callbacks.
*
* @var array
*/
protected $after = [];
protected $afterMaking = [];
/**
* The model after creating callbacks.
*
* @var array
*/
protected $afterCreating = [];
/**
* The states to apply.
@@ -80,17 +87,21 @@ class FactoryBuilder
* @param string $name
* @param array $definitions
* @param array $states
* @param array $afterMaking
* @param array $afterCreating
* @param \Faker\Generator $faker
* @return void
*/
public function __construct($class, $name, array $definitions, array $states, array $after, Faker $faker)
public function __construct($class, $name, array $definitions, array $states,
array $afterMaking, array $afterCreating, Faker $faker)
{
$this->name = $name;
$this->class = $class;
$this->faker = $faker;
$this->states = $states;
$this->definitions = $definitions;
$this->after = $after;
$this->afterMaking = $afterMaking;
$this->afterCreating = $afterCreating;
}
/**
@@ -157,10 +168,12 @@ public function create(array $attributes = [])
if ($results instanceof Model) {
$this->store(collect([$results]));
$this->applyAfter(collect([$results]), 'create');
$this->callAfterCreating(collect([$results]));
} else {
$this->store($results);
$this->applyAfter($results, 'create');
$this->callAfterCreating($results);
}
return $results;
@@ -192,10 +205,9 @@ protected function store($results)
public function make(array $attributes = [])
{
if ($this->amount === null) {
$instance = $this->makeInstance($attributes);
$this->applyAfter(collect([$instance]), 'make');
return $instance;
return tap($this->makeInstance($attributes), function ($instance) {
$this->callAfterMaking(collect([$instance]));
});
}
if ($this->amount < 1) {
@@ -205,7 +217,8 @@ public function make(array $attributes = [])
$instances = (new $this->class)->newCollection(array_map(function () use ($attributes) {
return $this->makeInstance($attributes);
}, range(1, $this->amount)));
$this->applyAfter($instances, 'make');
$this->callAfterMaking($instances);
return $instances;
}
@@ -346,20 +359,36 @@ protected function expandAttributes(array $attributes)
}
/**
* Run after callback on a collection of models.
* Run after making callbacks on a collection of models.
*
* @param \Illuminate\Support\Collection $models
* @param string $action
* @return void
*/
public function applyAfter($models, $action)
public function callAfterMaking($models)
{
$models->each(function ($model) use ($action) {
if (! isset($this->after[$this->class][$action])) {
return;
$models->each(function ($model) {
if (isset($this->afterMaking[$this->class])) {
foreach ($this->afterMaking[$this->class] as $callback) {
$callback($model, $this->faker);
}
}
});
}
call_user_func_array($this->after[$this->class][$action], [$model, $this->faker]);
/**
* Run after creating callbacks on a collection of models.
*
* @param \Illuminate\Support\Collection $models
* @return void
*/
public function callAfterCreating($models)
{
$models->each(function ($model) {
if (isset($this->afterCreating[$this->class])) {
foreach ($this->afterCreating[$this->class] as $callback) {
$callback($model, $this->faker);
}
}
});
}
}
@@ -46,7 +46,7 @@ protected function getEnvironmentSetUp($app)
];
});
$factory->after(FactoryBuildableUser::class, 'make', function (FactoryBuildableUser $user, Generator $faker) {
$factory->afterMaking(FactoryBuildableUser::class, function (FactoryBuildableUser $user, Generator $faker) {
$profile = factory(FactoryBuildableProfile::class)->make(['user_id' => $user->id]);
$user->setRelation('profile', $profile);
});
@@ -60,7 +60,7 @@ protected function getEnvironmentSetUp($app)
];
});
$factory->after(FactoryBuildableTeam::class, function (FactoryBuildableTeam $team, Generator $faker) {
$factory->afterCreating(FactoryBuildableTeam::class, function (FactoryBuildableTeam $team, Generator $faker) {
$team->users()->attach($team->owner);
});

0 comments on commit d79509d

Please sign in to comment.