| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- declare(strict_types=1);
- /**
- * Glacier 单一入口(前端控制器)。
- * 所有动态请求都经此进入 —— URL 与物理路径完全解耦,见 docs/architecture.md §3.1。
- */
- define('APP_ROOT', dirname(__DIR__));
- require APP_ROOT . '/vendor/autoload.php';
- use Glacier\Core\Config;
- use Glacier\Core\Logger;
- use Glacier\Core\Response;
- use Glacier\Core\Router;
- date_default_timezone_set('Asia/Shanghai'); // 架构约定 UTC+8
- $config = require APP_ROOT . '/app/config/env.php';
- Config::load($config);
- Logger::init(Config::get('storage.logs', APP_ROOT . '/storage/logs'));
- try {
- // 解析请求路径(去 query,规范化尾部斜杠)
- $path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';
- if (strlen($path) > 1) {
- $path = rtrim($path, '/');
- }
- $method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
- Logger::info('request', ['method' => $method, 'path' => $path]);
- // 注册路由(声明式路由表)
- $router = new Router();
- foreach ((array) require APP_ROOT . '/app/config/routes.php' as $route) {
- $router->add($route['method'], $route['pattern'], $route['handler'], $route['middleware'] ?? []);
- }
- $match = $router->match($path, $method);
- if ($match === null) {
- Logger::warning('route not found', ['method' => $method, 'path' => $path]);
- Response::html(
- '<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><title>404</title>'
- . '<style>body{font-family:system-ui;text-align:center;padding-top:80px;color:#666}'
- . 'h1{font-size:48px;color:#c33;margin-bottom:8px}</style></head>'
- . '<body><h1>404</h1><p>页面不存在。</p></body></html>',
- 404
- );
- }
- call_user_func($match['handler'], $match['params']);
- } catch (\Throwable $e) {
- Logger::error('uncaught exception', [
- 'message' => $e->getMessage(),
- 'file' => $e->getFile(),
- 'line' => $e->getLine(),
- ]);
- $detail = Config::get('app.debug', false)
- ? '<pre>' . e($e->getMessage()) . PHP_EOL . e($e->getFile() . ':' . $e->getLine()) . '</pre>'
- : '<p>服务器开小差了,请稍后再试。</p>';
- Response::html(
- '<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><title>500</title>'
- . '<style>body{font-family:system-ui;text-align:center;padding-top:80px;color:#666}'
- . 'h1{font-size:48px;color:#c33}</style></head>'
- . '<body><h1>500</h1>' . $detail . '</body></html>',
- 500
- );
- }
|