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

require_once 'composer.php';

$builder = new Builder();

$folders = [
  'base',
  'env'
];

if (isset($__environment)) {
  $folders []= $__environment;
}

$files = [
  'config',
  'setup'
];

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();
$container->set('env', $__environment);
$app = Bridge::create($container);
$app->setBasePath($container->get('base_url'));

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

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