ef24df55fa5e4bf71f850270c44661750cac479d
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CuyZ\Valinor\Definition\Repository\Cache\Compiler;
6
7 use CuyZ\Valinor\Cache\Compiled\CacheCompiler;
8 use CuyZ\Valinor\Definition\ClassDefinition;
9 use CuyZ\Valinor\Definition\MethodDefinition;
10 use CuyZ\Valinor\Definition\PropertyDefinition;
11
12 use function array_map;
13 use function assert;
14 use function implode;
15 use function iterator_to_array;
16
17 /** @internal */
18 final class ClassDefinitionCompiler implements CacheCompiler
19 {
20 private TypeCompiler $typeCompiler;
21
22 private AttributesCompiler $attributesCompiler;
23
24 private MethodDefinitionCompiler $methodCompiler;
25
26 private PropertyDefinitionCompiler $propertyCompiler;
27
28 public function __construct()
29 {
30 $this->typeCompiler = new TypeCompiler();
31 $this->attributesCompiler = new AttributesCompiler();
32
33 $this->methodCompiler = new MethodDefinitionCompiler($this->typeCompiler, $this->attributesCompiler);
34 $this->propertyCompiler = new PropertyDefinitionCompiler($this->typeCompiler, $this->attributesCompiler);
35 }
36
37 public function compile(mixed $value): string
38 {
39 assert($value instanceof ClassDefinition);
40
41 $type = $this->typeCompiler->compile($value->type());
42
43 $properties = array_map(
44 fn (PropertyDefinition $property) => $this->propertyCompiler->compile($property),
45 iterator_to_array($value->properties())
46 );
47
48 $properties = implode(', ', $properties);
49
50 $methods = array_map(
51 fn (MethodDefinition $method) => $this->methodCompiler->compile($method),
52 iterator_to_array($value->methods())
53 );
54
55 $methods = implode(', ', $methods);
56 $attributes = $this->attributesCompiler->compile($value->attributes());
57
58 $isFinal = var_export($value->isFinal(), true);
59 $isAbstract = var_export($value->isAbstract(), true);
60
61 return <<<PHP
62 new \CuyZ\Valinor\Definition\ClassDefinition(
63 $type,
64 $attributes,
65 new \CuyZ\Valinor\Definition\Properties($properties),
66 new \CuyZ\Valinor\Definition\Methods($methods),
67 $isFinal,
68 $isAbstract,
69 )
70 PHP;
71 }
72 }