Skip to content

Commit

Permalink
Latest performance fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jan 8, 2024
1 parent 310d711 commit e1d19de
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/Resolvers/TransformedDataCollectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,26 @@ public function execute(
? $items->getWrap()
: new Wrap(WrapType::UseGlobal);

$nestedContext = $context->wrapExecutionType->shouldExecute()
? (clone $context)->setWrapExecutionType(WrapExecutionType::TemporarilyDisabled)
$executeWrap = $context->wrapExecutionType->shouldExecute();

$nestedContext = $executeWrap
? $context->setWrapExecutionType(WrapExecutionType::TemporarilyDisabled)
: $context;

if ($items instanceof DataCollection) {
return $this->transformItems($items->items(), $wrap, $context, $nestedContext);
return $this->transformItems($items->items(), $wrap, $executeWrap, $nestedContext);
}

if ($items instanceof Enumerable || is_array($items)) {
return $this->transformItems($items, $wrap, $context, $nestedContext);
return $this->transformItems($items, $wrap, $executeWrap, $nestedContext);
}

if ($items instanceof PaginatedDataCollection || $items instanceof CursorPaginatedDataCollection) {
return $this->transformPaginator($items->items(), $wrap, $context, $nestedContext);
return $this->transformPaginator($items->items(), $wrap, $nestedContext);
}

if ($items instanceof Paginator || $items instanceof CursorPaginator) {
return $this->transformPaginator($items, $wrap, $context, $nestedContext);
return $this->transformPaginator($items, $wrap, $nestedContext);
}

throw new Exception("Cannot transform collection");
Expand All @@ -61,7 +63,7 @@ public function execute(
protected function transformItems(
Enumerable|array $items,
Wrap $wrap,
TransformationContext $context,
bool $executeWrap,
TransformationContext $nestedContext,
): array {
$collection = [];
Expand All @@ -70,20 +72,19 @@ protected function transformItems(
$collection[$key] = $this->transformationClosure($nestedContext)($value);
}

return $context->wrapExecutionType->shouldExecute()
return $executeWrap
? $wrap->wrap($collection)
: $collection;
}

protected function transformPaginator(
Paginator|CursorPaginator $paginator,
Wrap $wrap,
TransformationContext $context,
TransformationContext $nestedContext,
): array {
$paginator = $paginator->through(fn (BaseData $data) => $this->transformationClosure($nestedContext)($data));

if ($context->transformValues === false) {
if ($nestedContext->transformValues === false) {
return $paginator->all();
}

Expand All @@ -102,14 +103,14 @@ protected function transformPaginator(
}

protected function transformationClosure(
TransformationContext $context,
TransformationContext $nestedContext,
): Closure {
return function (BaseData $data) use ($context) {
if (! $data instanceof TransformableData || ! $context->transformValues) {
return function (BaseData $data) use ($nestedContext) {
if (! $data instanceof TransformableData || ! $nestedContext->transformValues) {
return $data;
}

return $data->transform(clone $context);
return $data->transform(clone $nestedContext);
};
}
}

0 comments on commit e1d19de

Please sign in to comment.