<?php
// Enable Errors
ini_set("display_errors", 1); // DISABLE FOR RELEASE
ini_set("default_charset", "utf-8");
ini_set("session.cookie_httponly", 1);
ini_set("session.cookie_secure", 1);
ini_set("session.use_only_cookies", 1);
error_reporting(E_ALL);
// Load composer Autoloader
require_once __DIR__ . "/../vendor/autoload.php";
// Helpers
require_once __DIR__ . "/../utils/helpers.php";
// Namespaces
use Pecee\SimpleRouter\SimpleRouter;
use Pecee\SimpleRouter\Handlers\EventHandler;
use Pecee\SimpleRouter\Event\EventArgument;
use Pecee\SimpleRouter\Route\ILoadableRoute;
use Pecee\SimpleRouter\Route\IGroupRoute;
use Khofmann\Config\Config;
// Router base path
$basePath = Config::getBasePath() . "api";
// Router event handler for prepending base path
$eventHandler = new EventHandler();
$eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function (EventArgument $event) use ($basePath) {
  $route = $event->route;
  // Skip routes added by group as these will inherit the url
  if ($event->isSubRoute) {
    return;
  }
  // Prepend based on what type of Route
  switch (true) {
    case $route instanceof ILoadableRoute:
      $route->prependUrl($basePath);
      break;
    case $route instanceof IGroupRoute:
      $route->prependPrefix($basePath);
      break;
  }
});
// Add the handler
SimpleRouter::addEventHandler($eventHandler);
// Load external routes file
require_once __DIR__ . "/../routes/routes.php";
// Start the routing
SimpleRouter::start();
