<?php

namespace AutoAdmin\Bundle\Content\EventListener;

use AutoAdmin\Bundle\Content\Configuration\ConfigManager;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Twig\Environment;

class TwigContentConfigListener
{
    /**
     * @var Environment
     */
    private $twig;
    /**
     * @var ConfigManager
     */
    private $configManager;

    public function __construct(Environment $twig, ConfigManager $configManager)
    {
        $this->twig = $twig;
        $this->configManager = $configManager;
    }

    public function onKernelController(ControllerEvent $event): void
    {
        if ($event->getRequestType() === HttpKernelInterface::MAIN_REQUEST) {
            $this->twig->addGlobal('aa_content_config', $this->configManager->getConfig());
        }
    }
}
