diff --git a/src/LayerResolver/Resolvers/NamespaceLayerResolver.php b/src/LayerResolver/Resolvers/NamespaceLayerResolver.php index de123b42..f407a378 100644 --- a/src/LayerResolver/Resolvers/NamespaceLayerResolver.php +++ b/src/LayerResolver/Resolvers/NamespaceLayerResolver.php @@ -24,13 +24,27 @@ */ final readonly class NamespaceLayerResolver implements LayerResolverInterface { + /** @var array> */ + private array $normalisedLayers; + /** * @param array> $layers Map of layer name → path prefixes */ public function __construct( - private array $layers, - private string $basePath, + array $layers, + string $basePath, ) { + $normalisedLayers = []; + + foreach ($layers as $layerName => $layerPaths) { + foreach ((array) $layerPaths as $layerPath) { + $normalisedLayers[$layerName][] = $this->normalisePath( + $basePath . DIRECTORY_SEPARATOR . trim($layerPath, '/') + ); + } + } + + $this->normalisedLayers = $normalisedLayers; } public function resolve(string $className, string $filePath): ?string @@ -39,14 +53,10 @@ public function resolve(string $className, string $filePath): ?string $matchedLayer = null; $matchedLength = -1; - foreach ($this->layers as $layerName => $layerPaths) { - foreach ((array) $layerPaths as $layerPath) { - $normalisedLayer = $this->normalisePath( - $this->basePath . DIRECTORY_SEPARATOR . trim($layerPath, '/') - ); - - if (str_starts_with($normalised, $normalisedLayer)) { - $length = strlen($normalisedLayer); + foreach ($this->normalisedLayers as $layerName => $layerPaths) { + foreach ($layerPaths as $layerPath) { + if (str_starts_with($normalised, $layerPath)) { + $length = strlen($layerPath); if ($length > $matchedLength) { $matchedLayer = $layerName; @@ -67,13 +77,9 @@ public function resolveAll(string $className, string $filePath): array $normalised = $this->normalisePath($filePath); $matched = []; - foreach ($this->layers as $layerName => $layerPaths) { - foreach ((array) $layerPaths as $layerPath) { - $normalisedLayer = $this->normalisePath( - $this->basePath . DIRECTORY_SEPARATOR . trim($layerPath, '/') - ); - - if (str_starts_with($normalised, $normalisedLayer)) { + foreach ($this->normalisedLayers as $layerName => $layerPaths) { + foreach ($layerPaths as $layerPath) { + if (str_starts_with($normalised, $layerPath)) { $matched[] = $layerName; break; }