Skip to content

Commit f839c68

Browse files
committed
1 parent 17786ca commit f839c68

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/Illuminate/Database/Eloquent/Factories/Factory.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ abstract class Factory
114114
public static $namespace = 'Database\\Factories\\';
115115

116116
/**
117-
* The default model name resolver.
117+
* The default model name resolvers.
118118
*
119-
* @var callable(self): class-string<TModel>
119+
* @var array<class-string, callable(self): class-string<TModel>>
120120
*/
121-
protected static $modelNameResolver;
121+
protected static $modelNameResolvers = [];
122122

123123
/**
124124
* The factory name resolver.
@@ -274,7 +274,7 @@ public function createMany(int|iterable|null $records = null)
274274
*/
275275
public function createManyQuietly(int|iterable|null $records = null)
276276
{
277-
return Model::withoutEvents(fn () => $this->createMany($records));
277+
return Model::withoutEvents(fn() => $this->createMany($records));
278278
}
279279

280280
/**
@@ -314,7 +314,7 @@ public function create($attributes = [], ?Model $parent = null)
314314
*/
315315
public function createQuietly($attributes = [], ?Model $parent = null)
316316
{
317-
return Model::withoutEvents(fn () => $this->create($attributes, $parent));
317+
return Model::withoutEvents(fn() => $this->create($attributes, $parent));
318318
}
319319

320320
/**
@@ -326,7 +326,7 @@ public function createQuietly($attributes = [], ?Model $parent = null)
326326
*/
327327
public function lazy(array $attributes = [], ?Model $parent = null)
328328
{
329-
return fn () => $this->create($attributes, $parent);
329+
return fn() => $this->create($attributes, $parent);
330330
}
331331

332332
/**
@@ -469,7 +469,7 @@ protected function getRawAttributes(?Model $parent)
469469
protected function parentResolvers()
470470
{
471471
return $this->for
472-
->map(fn (BelongsToRelationship $for) => $for->recycle($this->recycle)->attributesFor($this->newModel()))
472+
->map(fn(BelongsToRelationship $for) => $for->recycle($this->recycle)->attributesFor($this->newModel()))
473473
->collapse()
474474
->all();
475475
}
@@ -519,7 +519,7 @@ public function state($state)
519519
{
520520
return $this->newInstance([
521521
'states' => $this->states->concat([
522-
is_callable($state) ? $state : fn () => $state,
522+
is_callable($state) ? $state : fn() => $state,
523523
]),
524524
]);
525525
}
@@ -580,7 +580,8 @@ public function has(self $factory, $relationship = null)
580580
{
581581
return $this->newInstance([
582582
'has' => $this->has->concat([new Relationship(
583-
$factory, $relationship ?? $this->guessRelationship($factory->modelName())
583+
$factory,
584+
$relationship ?? $this->guessRelationship($factory->modelName())
584585
)]),
585586
]);
586587
}
@@ -653,7 +654,7 @@ public function recycle($model)
653654
->merge(
654655
Collection::wrap($model instanceof Model ? func_get_args() : $model)
655656
->flatten()
656-
)->groupBy(fn ($model) => get_class($model)),
657+
)->groupBy(fn($model) => get_class($model)),
657658
]);
658659
}
659660

@@ -810,18 +811,20 @@ public function modelName()
810811
return $this->model;
811812
}
812813

813-
$resolver = static::$modelNameResolver ?? function (self $factory) {
814+
$resolver = static::$modelNameResolvers[static::class] ?? function (self $factory) {
814815
$namespacedFactoryBasename = Str::replaceLast(
815-
'Factory', '', Str::replaceFirst(static::$namespace, '', get_class($factory))
816+
'Factory',
817+
'',
818+
Str::replaceFirst(static::$namespace, '', get_class($factory))
816819
);
817820

818821
$factoryBasename = Str::replaceLast('Factory', '', class_basename($factory));
819822

820823
$appNamespace = static::appNamespace();
821824

822-
return class_exists($appNamespace.'Models\\'.$namespacedFactoryBasename)
823-
? $appNamespace.'Models\\'.$namespacedFactoryBasename
824-
: $appNamespace.$factoryBasename;
825+
return class_exists($appNamespace . 'Models\\' . $namespacedFactoryBasename)
826+
? $appNamespace . 'Models\\' . $namespacedFactoryBasename
827+
: $appNamespace . $factoryBasename;
825828
};
826829

827830
return $resolver($this);
@@ -835,7 +838,7 @@ public function modelName()
835838
*/
836839
public static function guessModelNamesUsing(callable $callback)
837840
{
838-
static::$modelNameResolver = $callback;
841+
static::$modelNameResolvers[static::class] = $callback;
839842
}
840843

841844
/**
@@ -898,11 +901,11 @@ public static function resolveFactoryName(string $modelName)
898901
$resolver = static::$factoryNameResolver ?? function (string $modelName) {
899902
$appNamespace = static::appNamespace();
900903

901-
$modelName = Str::startsWith($modelName, $appNamespace.'Models\\')
902-
? Str::after($modelName, $appNamespace.'Models\\')
904+
$modelName = Str::startsWith($modelName, $appNamespace . 'Models\\')
905+
? Str::after($modelName, $appNamespace . 'Models\\')
903906
: Str::after($modelName, $appNamespace);
904907

905-
return static::$namespace.$modelName.'Factory';
908+
return static::$namespace . $modelName . 'Factory';
906909
};
907910

908911
return $resolver($modelName);

0 commit comments

Comments
 (0)