-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModuleFileLoader.php
More file actions
69 lines (61 loc) · 3 KB
/
ModuleFileLoader.php
File metadata and controls
69 lines (61 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* PackageFactory.ComponentEngine - Universal View Components for PHP
* Copyright (C) 2022 Contributors of PackageFactory.ComponentEngine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace PackageFactory\ComponentEngine\Module\Loader\ModuleFile;
use PackageFactory\ComponentEngine\Module\LoaderInterface;
use PackageFactory\ComponentEngine\Module\ModuleId;
use PackageFactory\ComponentEngine\Parser\Ast\ComponentDeclarationNode;
use PackageFactory\ComponentEngine\Parser\Ast\EnumDeclarationNode;
use PackageFactory\ComponentEngine\Parser\Ast\ImportNode;
use PackageFactory\ComponentEngine\Parser\Ast\ModuleNode;
use PackageFactory\ComponentEngine\Parser\Ast\StructDeclarationNode;
use PackageFactory\ComponentEngine\Parser\Source\Path;
use PackageFactory\ComponentEngine\Parser\Source\Source;
use PackageFactory\ComponentEngine\Parser\Tokenizer\Tokenizer;
use PackageFactory\ComponentEngine\TypeSystem\Type\ComponentType\ComponentType;
use PackageFactory\ComponentEngine\TypeSystem\Type\EnumType\EnumStaticType;
use PackageFactory\ComponentEngine\TypeSystem\Type\StructType\StructType;
use PackageFactory\ComponentEngine\TypeSystem\TypeInterface;
final class ModuleFileLoader implements LoaderInterface
{
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 . '".'
);
}
$moduleId = ModuleId::fromSource($source);
return match ($export->declaration::class) {
ComponentDeclarationNode::class => ComponentType::fromComponentDeclarationNode($export->declaration),
EnumDeclarationNode::class => EnumStaticType::fromModuleIdAndDeclaration(
$moduleId,
$export->declaration,
),
StructDeclarationNode::class => StructType::fromStructDeclarationNode($export->declaration)
};
}
}