<?php

namespace App\DependencyInjection;

use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;
use Symfony\Component\HttpKernel\KernelInterface;

final class JsonEnvVarLoader implements EnvVarLoaderInterface
{
    private const string ENV_VARS_FILE = 'env.json';

    public function __construct(
        private readonly KernelInterface $kernel
    )
    {
    }

    public function loadEnvVars(): array
    {
        $projectDir = $this->kernel->getProjectDir();

        $fileName = $projectDir . DIRECTORY_SEPARATOR . self::ENV_VARS_FILE;
        if (!is_file($fileName)) {
            throw new \RuntimeException('File not found: '.$fileName);
        }

        $content = json_decode(file_get_contents($fileName), true);

        return $content['vars'];
    }
}