Skip to content

Commit

Permalink
fix(doctrine): memory leak when using the iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Dec 18, 2024
1 parent e0da756 commit 5a2f521
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Doctrine/Odm/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,23 @@ final class Paginator implements \IteratorAggregate, PaginatorInterface, HasNext

private readonly int $totalItems;

private readonly int $count;

public function __construct(private readonly Iterator $mongoDbOdmIterator, private readonly UnitOfWork $unitOfWork, private readonly string $resourceClass, private readonly array $pipeline)

Check failure on line 43 in src/Doctrine/Odm/Paginator.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property ApiPlatform\Doctrine\Odm\Paginator::$mongoDbOdmIterator is never read, only written.
{
$array = $mongoDbOdmIterator->toArray();
$resultsFacetInfo = $this->getFacetInfo('results');
$this->getFacetInfo('count');
$this->iterator = new \ArrayIterator(array_map(fn ($result): object => $this->unitOfWork->getOrCreateDocument($this->resourceClass, $result), $array[0]['results']));

/*
* Since the {@see \MongoDB\Driver\Cursor} class does not expose information about
* skip/limit parameters of the query, the values set in the facet stage are used instead.
*/
$this->firstResult = $this->getStageInfo($resultsFacetInfo, '$skip');
$this->maxResults = $this->hasLimitZeroStage($resultsFacetInfo) ? 0 : $this->getStageInfo($resultsFacetInfo, '$limit');
$this->totalItems = $mongoDbOdmIterator->toArray()[0]['count'][0]['count'] ?? 0;
$this->totalItems = $array[0]['count'][0]['count'] ?? 0;
$this->count = is_countable($array[0]['results']) ? \count($array[0]['results']) : 0;
}

/**
Expand Down Expand Up @@ -97,15 +102,15 @@ public function getTotalItems(): float
*/
public function getIterator(): \Traversable
{
return $this->iterator ?? $this->iterator = new \ArrayIterator(array_map(fn ($result): object => $this->unitOfWork->getOrCreateDocument($this->resourceClass, $result), $this->mongoDbOdmIterator->toArray()[0]['results']));
return $this->iterator;
}

/**
* {@inheritdoc}
*/
public function count(): int
{
return is_countable($this->mongoDbOdmIterator->toArray()[0]['results']) ? \count($this->mongoDbOdmIterator->toArray()[0]['results']) : 0;
return $this->count;
}

/**
Expand Down

0 comments on commit 5a2f521

Please sign in to comment.