3 declare(strict_types=1);
5 namespace CuyZ\Valinor\Definition\Repository\Cache\Compiler;
7 use CuyZ\Valinor\Cache\Compiled\CacheCompiler;
8 use CuyZ\Valinor\Definition\FunctionDefinition;
9 use CuyZ\Valinor\Definition\ParameterDefinition;
11 use function var_export;
14 final class FunctionDefinitionCompiler implements CacheCompiler
16 private TypeCompiler $typeCompiler;
18 private AttributesCompiler $attributesCompiler;
20 private ParameterDefinitionCompiler $parameterCompiler;
22 public function __construct()
24 $this->typeCompiler = new TypeCompiler();
25 $this->attributesCompiler = new AttributesCompiler();
27 $this->parameterCompiler = new ParameterDefinitionCompiler($this->typeCompiler, new AttributesCompiler());
30 public function compile(mixed $value): string
32 assert($value instanceof FunctionDefinition);
34 $parameters = array_map(
35 fn (ParameterDefinition $parameter) => $this->parameterCompiler->compile($parameter),
36 iterator_to_array($value->parameters())
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());
48 new \CuyZ\Valinor\Definition\FunctionDefinition(
50 '{$value->signature()}',
56 new \CuyZ\Valinor\Definition\Parameters($parameters),