<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\DI\Compiler;

final class CompiledDefinitionWriter
{
    public function write(CompilerContext $context, string $file): void
    {
        $dir = dirname($file);
        if (!is_dir($dir)) {
            mkdir($dir, 0775, true);
        }

        $data = [
            'generatedAt' => date('c'),
            'definitions' => array_map(
                static fn (CompiledDefinition $definition): array => $definition->toArray(),
                array_values($context->compiled())
            ),
            'warnings' => $context->warnings(),
            'metadata' => $context->metadata(),
        ];

        file_put_contents($file, "<?php\nreturn " . var_export($data, true) . ";\n");
    }
}
