<?php

/**
 * Copyright © Resurs Bank AB. All rights reserved.
 * See LICENSE for license details.
 */

namespace Resursbank\MerchantApi\EventListener\KernelRequest;

use Symfony\Component\HttpKernel\Event\RequestEvent;

/**
 * Start our autoloader at the start of the request. Note that this is required
 * for our services to function properly, otherwise Symfony won't be able to
 * find the classes specified within our YAML files. Please note that this is
 * required alongside the hook defined in the main class, otherwise our
 * autoloader won't always be available.
 */
class Autoloader
{
    /**
     * We have several classes we need to execute on kernel request, to register
     * twig filters, initiate ecom etc.
     *
     * These classes have DI requirements, and we need to ensure all our classes
     * are loaded before these execute.
     *
     * This is why we split it into several classes, and also why we perform our
     * action here in the construct method. The DI container will generate all
     * objects, then execute their event functions one ata a time.
     *
     * This means that, if one event has a DI requirement, that class must have
     * been loaded _before_ that object is initiated.
     *
     * Since we can list of services by priority, we know that this class will
     * be initiated before the others, hence, be placing our autoloader in the
     * constructor we guarantee that required classes will exist for other
     * classes that have a dependency on them.
     */
    public function __construct()
    {
        require_once _PS_MODULE_DIR_ . '/resursbank/src/Service/Autoloader.php';
    }

    public function exec(RequestEvent $event): void
    {
        // We are required to have this method, but we don't need to do anything
        // here since out actual code executes in the constructor.
    }
}
