<?php
use DI\ContainerBuilder;
use Incoviba\Common\Alias\Application;

include_once 'composer.php';

function buildApp(): Application
{
    $builder = new ContainerBuilder();
    $folders = [
        'settings',
        'setups'
    ];
    foreach ($folders as $folder_name) {
        $folder = implode(DIRECTORY_SEPARATOR, [__DIR__, $folder_name]);
        if (!file_exists($folder)) {
            continue;
        }
        $files = new FilesystemIterator($folder);
        foreach ($files as $file) {
            if ($file->isDir()) {
                continue;
            }
            $builder->addDefinitions($file->getRealPath());
        }
    }
    $app = new Application($builder->build());

    $folder = implode(DIRECTORY_SEPARATOR, [__DIR__, 'middlewares']);
    if (file_exists($folder)) {
        $files = new FilesystemIterator($folder);
        foreach ($files as $file) {
            if ($file->isDir()) {
                continue;
            }
            include_once $file->getRealPath();
        }
    }

    return $app;
}
return buildApp();
