<?php

namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    /**
     * @throws \Exception
     */
    protected function prepareContainer(ContainerBuilder $container): void
    {
        $this->addMithoConfig($container);
        parent::prepareContainer($container);
    }

    /**
     * @throws \Exception
     */
    private function addMithoConfig($container): void
    {
        $loader = new YamlFileLoader($container, new FileLocator($this->getProjectDir()));
        $loader->load('config/mitho/settings.yaml');
        $this->flattenParameter('mitho', $container);
    }

    private function flattenParameter(string $name, ContainerBuilder $container): void
    {
        $value = $container->getParameter($name);

        if (!is_array($value)) {
            return;
        }

        foreach ($value as $key => $innerValue) {
            $innerName = $name . '.' . $key;
            $container->setParameter($innerName, $innerValue);
            $this->flattenParameter($innerName, $container);
        }
    }

}
