<?php
use DI\Bridge\Slim\Bridge;

include_once 'composer.php';

$container_builder = new DI\ContainerBuilder();

$folders = [
  'common'
];
if (isset($_ENV)) {
  $folders []= $_ENV;
}

$file = 'config.php';
foreach ($folders as $folder) {
  $filename = implode(DIRECTORY_SEPARATOR, [__DIR__, $folder, $file]);
  if (file_exists($filename)) {
    $container_builder->addDefinitions($filename);
  }
}

$file = 'setup.php';
foreach ($folders as $folder) {
  $filename = implode(DIRECTORY_SEPARATOR, [__DIR__, $folder, $file]);
  if (file_exists($filename)) {
    $container_builder->addDefinitions($filename);
  }
}

$container = $container_builder->build();
$app = Bridge::create($container);
$app->setBasePath($container->get('urls.base'));
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);

$file = 'middleware.php';
foreach ($folders as $folder) {
  $filename = implode(DIRECTORY_SEPARATOR, [__DIR__, $folder, $file]);
  if (file_exists($filename)) {
    include_once $filename;
  }
}

include_once implode(DIRECTORY_SEPARATOR, [$app->getContainer()->get('folders.routes'), 'router.php']);
