<?php

declare(strict_types=1);

use Core\Router;

require dirname(__DIR__) . '/bootstrap.php';

$router = new Router();
$routeRegistrar = require dirname(__DIR__) . '/routes/web.php';

if (is_callable($routeRegistrar)) {
    $routeRegistrar($router);
}

$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?: '/';
$scriptName = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
$basePath = rtrim($scriptName, '/');
if ($basePath !== '' && $basePath !== '/') {
    $uri = preg_replace('#^' . preg_quote($basePath, '#') . '#', '', $uri) ?: '/';
}

$router->dispatch($_SERVER['REQUEST_METHOD'], $uri);
