<?php
use DI\ContainerBuilder as Builder;
use DI\Bridge\Slim\Bridge;

include_once 'composer.php';

$folders = [
  'env',
  'common',
  $__environment
];
$files = [
  'config',
  'setups'
];
$builder = new Builder();
foreach ($files as $file) {
  foreach ($folders as $folder) {
    $filename = implode(DIRECTORY_SEPARATOR, [
      __DIR__,
      $folder,
      $file . '.php'
    ]);
    if (!file_exists($filename)) {
      continue;
    }
    $builder->addDefinitions($filename);
  }
}

$container = $builder->build();
$app = Bridge::create($container);
$app->setBasePath($app->getContainer()->get('base_url'));
$app->addRoutingMiddleware();

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

$app->addErrorMiddleware(true, true, true);

include_once 'database.php';

include_once 'router.php';
