<?php
/*
 * Router script for PHP built-in server.
 * Mimics Apache's mod_rewrite to direct requests to index.php
 */

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// If the file exists in public/, serve it directly
if ($uri !== '/' && file_exists(__DIR__ . '/public' . $uri)) {
    $file = __DIR__ . '/public' . $uri;
    $mime = function_exists('mime_content_type') ? mime_content_type($file) : null;
    if ($mime) {
        header('Content-Type: ' . $mime);
    }
    header('Content-Length: ' . filesize($file));
    readfile($file);
    return true;
}

// Otherwise, load the framework
$_SERVER['SCRIPT_NAME'] = '/index.php';
require_once __DIR__ . '/public/index.php';
