forked from chaosarium/lwt
-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathrouter.php
More file actions
73 lines (67 loc) · 2.22 KB
/
router.php
File metadata and controls
73 lines (67 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php declare(strict_types=1);
/**
* PHP Built-in Server Router
*
* This script enables clean URLs and legacy URL redirects when using
* PHP's built-in web server (php -S localhost:8000 router.php)
*
* PHP version 8.1
*
* @category Server
* @package Lwt
* @author HugoFara <hugo.farajallah@protonmailhotmail.com>
* @license Unlicense <http://unlicense.org/>
* @link https://hugofara.github.io/lwt/developer/api
* @since 3.0.0
*
* Usage: php -S localhost:8000 router.php
*/
$uri = $_SERVER['REQUEST_URI'] ?? '/';
$path = parse_url($uri, PHP_URL_PATH);
if ($path === false || $path === null) {
$path = '/';
}
// Serve documentation files directly (VitePress output in /docs)
if (preg_match('#^/docs(/|$)#', $path)) {
$docPath = __DIR__ . $path;
// If path ends with / or has no extension, try index.html
if (is_dir($docPath)) {
$docPath = rtrim($docPath, '/') . '/index.html';
} elseif (!pathinfo($path, PATHINFO_EXTENSION)) {
$docPath .= '.html';
}
if (file_exists($docPath)) {
// Set appropriate content type
$ext = pathinfo($docPath, PATHINFO_EXTENSION);
$mimeTypes = [
'html' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'svg' => 'image/svg+xml',
'ico' => 'image/x-icon',
'woff' => 'font/woff',
'woff2' => 'font/woff2',
];
$contentType = $mimeTypes[$ext] ?? 'application/octet-stream';
header("Content-Type: $contentType");
readfile($docPath);
return true;
}
}
// Serve static files directly
$staticExtensions = [
'css', 'js', 'png', 'jpg', 'jpeg', 'gif',
'ico', 'svg', 'woff', 'woff2', 'ttf', 'eot', 'map', 'html'
];
$ext = pathinfo($path, PATHINFO_EXTENSION);
if (in_array(strtolower($ext), $staticExtensions) && file_exists(__DIR__ . $path)) {
return false; // Let PHP's built-in server handle static files
}
// Route everything through index.php
$_SERVER['SCRIPT_NAME'] = '/index.php';
require __DIR__ . '/index.php';