f885653eccb8bda7a26e29d1347c0c915dd9f129
[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\FunctionDefinition;
9 use CuyZ\Valinor\Definition\ParameterDefinition;
10
11 use function var_export;
12
13 /** @internal */
14 final class FunctionDefinitionCompiler implements CacheCompiler
15 {
16 private TypeCompiler $typeCompiler;
17
18 private AttributesCompiler $attributesCompiler;
19
20 private ParameterDefinitionCompiler $parameterCompiler;
21
22 public function __construct()
23 {
24 $this->typeCompiler = new TypeCompiler();
25 $this->attributesCompiler = new AttributesCompiler();
26
27 $this->parameterCompiler = new ParameterDefinitionCompiler($this->typeCompiler, new AttributesCompiler());
28 }
29
30 public function compile(mixed $value): string
31 {
32 assert($value instanceof FunctionDefinition);
33
34 $parameters = array_map(
35 fn (ParameterDefinition $parameter) => $this->parameterCompiler->compile($parameter),
36 iterator_to_array($value->parameters())
37 );
38
39 $attributes = $this->attributesCompiler->compile($value->attributes());
40 $fileName = var_export($value->fileName(), true);
41 $class = var_export($value->class(), true);
42 $isStatic = var_export($value->isStatic(), true);
43 $isClosure = var_export($value->isClosure(), true);
44 $parameters = implode(', ', $parameters);
45 $returnType = $this->typeCompiler->compile($value->returnType());
46
47 return <<<PHP
48 new \CuyZ\Valinor\Definition\FunctionDefinition(
49 '{$value->name()}',
50 '{$value->signature()}',
51 $attributes,
52 $fileName,
53 $class,
54 $isStatic,
55 $isClosure,
56 new \CuyZ\Valinor\Definition\Parameters($parameters),
57 $returnType
58 )
59 PHP;
60 }
61 }