Skip to content

Commit ac23f7a

Browse files
committed
Clean up EloquentBuffer
1 parent 81e0dc6 commit ac23f7a

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/Adapter/EloquentBuffer.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,26 @@ abstract class EloquentBuffer
2323
{
2424
private static $buffer = [];
2525

26-
public static function add(Model $model, string $relation): void
26+
public static function add(Model $model, string $relationName): void
2727
{
28-
static::$buffer[get_class($model)][$relation][] = $model;
28+
static::$buffer[get_class($model)][$relationName][] = $model;
2929
}
3030

31-
public static function load(Model $model, string $relation, Relationship $relationship, Context $context): void
31+
public static function load(Model $model, string $relationName, Relationship $relationship, Context $context): void
3232
{
33-
if (! $models = static::$buffer[get_class($model)][$relation] ?? null) {
33+
if (! $models = static::$buffer[get_class($model)][$relationName] ?? null) {
3434
return;
3535
}
3636

3737
Collection::make($models)->loadMissing([
38-
$relation => function ($query) use ($model, $relation, $relationship, $context) {
39-
// As we're loading the relationship, we need to scope the query
40-
// using the scopes defined in the related API resources. We
38+
$relationName => function ($relation) use ($model, $relationName, $relationship, $context) {
39+
$query = $relation->getQuery();
40+
41+
// When loading the relationship, we need to scope the query
42+
// using the scopes defined in the related API resource – there
43+
// may be multiple if this is a polymorphic relationship. We
4144
// start by getting the resource types this relationship
42-
// could contain.
45+
// could possibly contain.
4346
$resourceTypes = $context->getApi()->getResourceTypes();
4447

4548
if ($type = $relationship->getType()) {
@@ -50,33 +53,34 @@ public static function load(Model $model, string $relation, Relationship $relati
5053
}
5154
}
5255

56+
// Now, construct a map of model class names -> scoping
57+
// functions. This will be provided to the MorphTo::constrain
58+
// method in order to apply type-specific scoping.
5359
$constrain = [];
5460

5561
foreach ($resourceTypes as $resourceType) {
5662
if ($model = $resourceType->getAdapter()->model()) {
5763
$constrain[get_class($model)] = function ($query) use ($resourceType, $context) {
58-
run_callbacks(
59-
$resourceType->getSchema()->getListeners('scope'),
60-
[$query, $context]
61-
);
64+
$resourceType->applyScopes($query, $context);
6265
};
6366
}
6467
}
6568

66-
if ($query instanceof MorphTo) {
67-
$query->constrain($constrain);
69+
if ($relation instanceof MorphTo) {
70+
$relation->constrain($constrain);
6871
} else {
69-
reset($constrain)($query->getQuery());
72+
reset($constrain)($query);
7073
}
7174

72-
// Also apply relationship scopes to the query.
75+
// Also apply any local scopes that have been defined on this
76+
// relationship.
7377
run_callbacks(
7478
$relationship->getListeners('scope'),
75-
[$query->getQuery(), $context]
79+
[$query, $context]
7680
);
7781
}
7882
]);
7983

80-
static::$buffer[get_class($model)][$relation] = [];
84+
static::$buffer[get_class($model)][$relationName] = [];
8185
}
8286
}

0 commit comments

Comments
 (0)