2025-12-31 06:46:30 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2025-12-31 06:47:28 +00:00
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2025-12-31 06:46:30 +00:00
|
|
|
|
use Kiri\Router\Blade\BladeFactory;
|
|
|
|
|
|
use Kiri\Router\Blade\BladeHelper;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 渲染 Blade 视图
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $path 视图路径(支持 . 分隔,如 'user.profile')
|
|
|
|
|
|
* @param array $data 视图数据
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return ResponseInterface
|
|
|
|
|
|
*/
|
|
|
|
|
|
function View(string $path, array $data = []): ResponseInterface
|
|
|
|
|
|
{
|
|
|
|
|
|
$response = \response();
|
|
|
|
|
|
$response->withAddedHeader('Content-Type', 'text/html; charset=utf-8');
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 获取视图路径和缓存路径
|
|
|
|
|
|
$viewPath = APP_PATH . 'resources/view';
|
|
|
|
|
|
$cachePath = storage(null, 'view/cache');
|
|
|
|
|
|
|
|
|
|
|
|
// 创建或获取 BladeFactory 实例
|
|
|
|
|
|
$factory = BladeHelper::getFactory();
|
|
|
|
|
|
if ($factory->getViewPath() !== $viewPath) {
|
|
|
|
|
|
$factory = new BladeFactory($viewPath, $cachePath);
|
|
|
|
|
|
BladeHelper::setFactory($factory);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 渲染视图
|
|
|
|
|
|
return $response->html($factory->render($path, $data));
|
|
|
|
|
|
} catch (\Exception $throwable) {
|
|
|
|
|
|
\Kiri::getLogger()->json_log($throwable);
|
|
|
|
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
|
|
|
|
|
|
|
|
extract(['errorData' => $throwable], EXTR_SKIP);
|
|
|
|
|
|
include __DIR__.'/template/error.php';
|
|
|
|
|
|
|
|
|
|
|
|
$message = ob_get_clean();
|
|
|
|
|
|
|
|
|
|
|
|
return $response->html($message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|