|
39 | 39 |
|
40 | 40 | final class ModuleFileLoader implements LoaderInterface |
41 | 41 | { |
| 42 | + /** @var array<string, array<string, TypeInterface>> */ |
| 43 | + private array $cache = []; |
| 44 | + |
42 | 45 | public function resolveTypeOfImport(ImportNode $importNode): TypeInterface |
43 | 46 | { |
44 | 47 | $pathToImportFrom = $importNode->source->path->resolveRelationTo( |
45 | 48 | Path::fromString($importNode->path) |
46 | 49 | ); |
47 | | - $source = Source::fromFile($pathToImportFrom->value); |
48 | | - $tokenizer = Tokenizer::fromSource($source); |
49 | | - $module = ModuleNode::fromTokens($tokenizer->getIterator()); |
50 | | - $export = $module->exports->get($importNode->name->value); |
51 | 50 |
|
52 | | - if ($export === null) { |
53 | | - throw new \Exception( |
54 | | - '@TODO: Module "' . $importNode->path . '" has no exported member "' . $importNode->name->value . '".' |
55 | | - ); |
| 51 | + if (!isset($this->cache[$pathToImportFrom->value])) { |
| 52 | + $source = Source::fromFile($pathToImportFrom->value); |
| 53 | + $tokenizer = Tokenizer::fromSource($source); |
| 54 | + $module = ModuleNode::fromTokens($tokenizer->getIterator()); |
| 55 | + $moduleId = ModuleId::fromSource($source); |
| 56 | + |
| 57 | + foreach ($module->exports->items as $export) { |
| 58 | + $this->cache[$pathToImportFrom->value][$importNode->name->value] = match ($export->declaration::class) { |
| 59 | + ComponentDeclarationNode::class => ComponentType::fromComponentDeclarationNode($export->declaration), |
| 60 | + EnumDeclarationNode::class => EnumStaticType::fromModuleIdAndDeclaration( |
| 61 | + $moduleId, |
| 62 | + $export->declaration, |
| 63 | + ), |
| 64 | + StructDeclarationNode::class => StructType::fromStructDeclarationNode($export->declaration) |
| 65 | + }; |
| 66 | + } |
56 | 67 | } |
57 | 68 |
|
58 | | - $moduleId = ModuleId::fromSource($source); |
| 69 | + if ($type = $this->cache[$pathToImportFrom->value][$importNode->name->value] ?? null) { |
| 70 | + return $type; |
| 71 | + } |
59 | 72 |
|
60 | | - return match ($export->declaration::class) { |
61 | | - ComponentDeclarationNode::class => ComponentType::fromComponentDeclarationNode($export->declaration), |
62 | | - EnumDeclarationNode::class => EnumStaticType::fromModuleIdAndDeclaration( |
63 | | - $moduleId, |
64 | | - $export->declaration, |
65 | | - ), |
66 | | - StructDeclarationNode::class => StructType::fromStructDeclarationNode($export->declaration) |
67 | | - }; |
| 73 | + throw new \Exception( |
| 74 | + '@TODO: Module "' . $importNode->path . '" has no exported member "' . $importNode->name->value . '".' |
| 75 | + ); |
68 | 76 | } |
69 | 77 | } |
0 commit comments