Skip to content

Commit

Permalink
fix: cache with native functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Aug 21, 2023
1 parent 90b9715 commit 527ff87
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Repositories/ObjectsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class ObjectsRepository
/**
* Holds the Objects Descriptions of the previous resolved prefixes.
*
* @var array<string, array<int, ObjectDescription>>
* @var array<string, array{0?: array<int, ObjectDescription|FunctionDescription>, 1?: array<int, ObjectDescription|FunctionDescription>}>
*/
private array $cachedObjectsPerPrefix = [];

Expand Down Expand Up @@ -85,17 +85,21 @@ public function allByNamespace(string $namespace, bool $onlyUserDefinedUses = tr

foreach ($directoriesByNamespace as $prefix => $directories) {
if (array_key_exists($prefix, $this->cachedObjectsPerPrefix)) {
$objects = [...$objects, ...$this->cachedObjectsPerPrefix[$prefix]];
if (array_key_exists((int) $onlyUserDefinedUses, $this->cachedObjectsPerPrefix[$prefix])) {
$objects = [...$objects, ...$this->cachedObjectsPerPrefix[$prefix][(int) $onlyUserDefinedUses]];

continue;
continue;
}
} else {
$this->cachedObjectsPerPrefix[$prefix] = [];
}

$objectsPerPrefix = array_values(array_filter(array_reduce($directories, fn (array $files, string $fileOrDirectory): array => array_merge($files, array_values(array_map(
static fn (SplFileInfo $file): ?ObjectDescription => ObjectDescriptionFactory::make($file->getPathname(), $onlyUserDefinedUses),
is_dir($fileOrDirectory) ? iterator_to_array(Finder::create()->files()->in($fileOrDirectory)->name('*.php')) : [new SplFileInfo($fileOrDirectory)],
))), [])));

$objects = [...$objects, ...$this->cachedObjectsPerPrefix[$prefix] = $objectsPerPrefix];
$objects = [...$objects, ...$this->cachedObjectsPerPrefix[$prefix][(int) $onlyUserDefinedUses] = $objectsPerPrefix];
}

return [...$objects, ...array_map(
Expand Down

0 comments on commit 527ff87

Please sign in to comment.