From 527ff8705c1bbbf49e729d4dce9bdb00b07c0424 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 21 Aug 2023 09:38:17 +0100 Subject: [PATCH] fix: cache with native functions --- src/Repositories/ObjectsRepository.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Repositories/ObjectsRepository.php b/src/Repositories/ObjectsRepository.php index c604b44..1ab8218 100644 --- a/src/Repositories/ObjectsRepository.php +++ b/src/Repositories/ObjectsRepository.php @@ -26,7 +26,7 @@ final class ObjectsRepository /** * Holds the Objects Descriptions of the previous resolved prefixes. * - * @var array> + * @var array, 1?: array}> */ private array $cachedObjectsPerPrefix = []; @@ -85,9 +85,13 @@ 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( @@ -95,7 +99,7 @@ public function allByNamespace(string $namespace, bool $onlyUserDefinedUses = tr 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(