Clean up EloquentBuffer

This commit is contained in:
Toby Zerner 2021-08-29 15:53:45 +10:00
parent 81e0dc63b7
commit ac23f7a70a
1 changed files with 22 additions and 18 deletions

View File

@ -23,23 +23,26 @@ abstract class EloquentBuffer
{ {
private static $buffer = []; private static $buffer = [];
public static function add(Model $model, string $relation): void public static function add(Model $model, string $relationName): void
{ {
static::$buffer[get_class($model)][$relation][] = $model; static::$buffer[get_class($model)][$relationName][] = $model;
} }
public static function load(Model $model, string $relation, Relationship $relationship, Context $context): void public static function load(Model $model, string $relationName, Relationship $relationship, Context $context): void
{ {
if (! $models = static::$buffer[get_class($model)][$relation] ?? null) { if (! $models = static::$buffer[get_class($model)][$relationName] ?? null) {
return; return;
} }
Collection::make($models)->loadMissing([ Collection::make($models)->loadMissing([
$relation => function ($query) use ($model, $relation, $relationship, $context) { $relationName => function ($relation) use ($model, $relationName, $relationship, $context) {
// As we're loading the relationship, we need to scope the query $query = $relation->getQuery();
// using the scopes defined in the related API resources. We
// When loading the relationship, we need to scope the query
// using the scopes defined in the related API resource there
// may be multiple if this is a polymorphic relationship. We
// start by getting the resource types this relationship // start by getting the resource types this relationship
// could contain. // could possibly contain.
$resourceTypes = $context->getApi()->getResourceTypes(); $resourceTypes = $context->getApi()->getResourceTypes();
if ($type = $relationship->getType()) { if ($type = $relationship->getType()) {
@ -50,33 +53,34 @@ abstract class EloquentBuffer
} }
} }
// Now, construct a map of model class names -> scoping
// functions. This will be provided to the MorphTo::constrain
// method in order to apply type-specific scoping.
$constrain = []; $constrain = [];
foreach ($resourceTypes as $resourceType) { foreach ($resourceTypes as $resourceType) {
if ($model = $resourceType->getAdapter()->model()) { if ($model = $resourceType->getAdapter()->model()) {
$constrain[get_class($model)] = function ($query) use ($resourceType, $context) { $constrain[get_class($model)] = function ($query) use ($resourceType, $context) {
run_callbacks( $resourceType->applyScopes($query, $context);
$resourceType->getSchema()->getListeners('scope'),
[$query, $context]
);
}; };
} }
} }
if ($query instanceof MorphTo) { if ($relation instanceof MorphTo) {
$query->constrain($constrain); $relation->constrain($constrain);
} else { } else {
reset($constrain)($query->getQuery()); reset($constrain)($query);
} }
// Also apply relationship scopes to the query. // Also apply any local scopes that have been defined on this
// relationship.
run_callbacks( run_callbacks(
$relationship->getListeners('scope'), $relationship->getListeners('scope'),
[$query->getQuery(), $context] [$query, $context]
); );
} }
]); ]);
static::$buffer[get_class($model)][$relation] = []; static::$buffer[get_class($model)][$relationName] = [];
} }
} }