Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: TASK: cache ModuleFileLoader::resolveTypeOfImport #13

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions src/Module/Loader/ModuleFile/ModuleFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,35 @@

final class ModuleFileLoader implements LoaderInterface
{
/** @var array<string, array<string, TypeInterface>> */
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 . '".'
);
}
}