diff --git a/src/Module/Loader/ModuleFile/ModuleFileLoader.php b/src/Module/Loader/ModuleFile/ModuleFileLoader.php index eaf342f8..fd95b6c9 100644 --- a/src/Module/Loader/ModuleFile/ModuleFileLoader.php +++ b/src/Module/Loader/ModuleFile/ModuleFileLoader.php @@ -38,26 +38,35 @@ final class ModuleFileLoader implements LoaderInterface { + /** @var array> */ + private array $cache = []; + public function resolveTypeOfImport(ImportNode $importNode): TypeInterface { $pathToImportFrom = $importNode->source->path->resolveRelationTo( Path::fromString($importNode->path) ); - $source = Source::fromFile($pathToImportFrom->value); - $tokenizer = Tokenizer::fromSource($source); - $module = ModuleNode::fromTokens($tokenizer->getIterator()); - $export = $module->exports->get($importNode->name->value); - - if ($export === null) { - throw new \Exception( - '@TODO: Module "' . $importNode->path . '" has no exported member "' . $importNode->name->value . '".' - ); + + if (!isset($this->cache[$pathToImportFrom->value])) { + $source = Source::fromFile($pathToImportFrom->value); + $tokenizer = Tokenizer::fromSource($source); + $module = ModuleNode::fromTokens($tokenizer->getIterator()); + + foreach ($module->exports->items as $export) { + $this->cache[$pathToImportFrom->value][$importNode->name->value] = match ($export->declaration::class) { + ComponentDeclarationNode::class => ComponentType::fromComponentDeclarationNode($export->declaration), + EnumDeclarationNode::class => EnumType::fromEnumDeclarationNode($export->declaration), + StructDeclarationNode::class => StructType::fromStructDeclarationNode($export->declaration) + }; + } } - return match ($export->declaration::class) { - ComponentDeclarationNode::class => ComponentType::fromComponentDeclarationNode($export->declaration), - EnumDeclarationNode::class => EnumType::fromEnumDeclarationNode($export->declaration), - StructDeclarationNode::class => StructType::fromStructDeclarationNode($export->declaration) - }; + if ($type = $this->cache[$pathToImportFrom->value][$importNode->name->value] ?? null) { + return $type; + } + + throw new \Exception( + '@TODO: Module "' . $importNode->path . '" has no exported member "' . $importNode->name->value . '".' + ); } }