diff --git a/examples/test-blade.php b/examples/test-blade.php index 3c0e9fa..b984068 100644 --- a/examples/test-blade.php +++ b/examples/test-blade.php @@ -24,7 +24,7 @@ $data = [ 'name' => '张三', 'email' => 'zhangsan@example.com', 'age' => 28, - 'skills' => ['PHP', 'JavaScript', 'MySQL', 'Redis'], + 'skills' => ['PHP', 'JavaScript', 'MySQL', 'NoSql'], 'posts' => [ [ 'title' => 'Blade 模板引擎介绍', diff --git a/src/Base/ExceptionHandlerDispatcher.php b/src/Base/ExceptionHandlerDispatcher.php index 6a82038..5f4362b 100644 --- a/src/Base/ExceptionHandlerDispatcher.php +++ b/src/Base/ExceptionHandlerDispatcher.php @@ -26,7 +26,7 @@ class ExceptionHandlerDispatcher implements ExceptionHandlerInterface */ public function emit(Throwable $exception, object $response): ResponseInterface { - error($exception); + \Kiri::getLogger()->json_log($exception); $response->withContentType(ContentType::HTML)->withBody(new Stream(throwable($exception))); if ($exception->getCode() == 404) { return $response->withStatus(404); diff --git a/src/Base/SessionMiddleware.php b/src/Base/SessionMiddleware.php index d93a0f6..f7b55e8 100644 --- a/src/Base/SessionMiddleware.php +++ b/src/Base/SessionMiddleware.php @@ -16,30 +16,24 @@ use Psr\Http\Server\RequestHandlerInterface; class SessionMiddleware implements MiddlewareInterface { - /** - * @param ServerRequestInterface $request - * @param RequestHandlerInterface $handler - * @return ResponseInterface - * @throws - */ - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface - { - // 启动 Session - Session::start($request); - - try { - // 处理请求 - $response = $handler->handle($request); - - // 保存 Session - Session::save(); - - return $response; - } catch (\Throwable $e) { - // 即使出错也保存 Session - Session::save(); - throw $e; - } - } + /** + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * @return ResponseInterface + * @throws + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + // 启动 Session + Session::start($request); + + // 处理请求 + $response = $handler->handle($request); + + // 保存 Session + Session::save(); + + return $response; + } } diff --git a/src/Blade/BladeCompiler.php b/src/Blade/BladeCompiler.php index 9a17aab..d04e7cc 100644 --- a/src/Blade/BladeCompiler.php +++ b/src/Blade/BladeCompiler.php @@ -299,9 +299,7 @@ class BladeCompiler } // 替换剩余的 @yield 为空 - $layoutContent = preg_replace('/@yield\s*\([\'"](.+?)[\'"]\)/', '', $layoutContent); - - return $layoutContent; + return preg_replace('/@yield\s*\([\'"](.+?)[\'"]\)/', '', $layoutContent); } // 处理 @section ... @endsection (非继承模式,用于组件等) @@ -324,9 +322,7 @@ class BladeCompiler }, $content); // 处理 @parent(在 section 中使用) - $content = preg_replace('/@parent/', '', $content); - - return $content; + return preg_replace('/@parent/', '', $content); } /** @@ -348,6 +344,8 @@ class BladeCompiler $data = eval("return [{$dataStr}];"); $dataCode = var_export($data, true); } catch (\Throwable $e) { + \Kiri::getLogger()->json_log($e); + $dataCode = '[]'; } } else { diff --git a/src/Blade/BladeView.php b/src/Blade/BladeView.php index b647f1f..c56b037 100644 --- a/src/Blade/BladeView.php +++ b/src/Blade/BladeView.php @@ -61,6 +61,9 @@ class BladeView require $compiledPath; } catch (\Throwable $e) { ob_end_clean(); + + \Kiri::getLogger()->json_log($throwable); + throw new \RuntimeException("视图渲染失败: {$this->view}", 0, $e); } diff --git a/src/OnRequest.php b/src/OnRequest.php index 939305f..b77f23e 100644 --- a/src/OnRequest.php +++ b/src/OnRequest.php @@ -87,6 +87,7 @@ class OnRequest implements OnRequestInterface $PsrResponse = $this->router->query($request->server['path_info'], $request->getMethod())->run($PsrRequest); } catch (Throwable $throwable) { + \Kiri::getLogger()->json_log($throwable); $PsrResponse = $this->exception->emit($throwable, $this->constrictResponse); } finally { $this->responseEmitter->response($PsrResponse, $response, $PsrRequest); diff --git a/src/Response.php b/src/Response.php index 6bc3ca6..0782895 100644 --- a/src/Response.php +++ b/src/Response.php @@ -12,9 +12,9 @@ use Psr\Http\Message\StreamInterface; /** * 渲染 Blade 视图 - * + * * @param string $path 视图路径(支持 . 分隔,如 'user.profile') - * @param array $data 视图数据 + * @param array $data 视图数据 * * @return ResponseInterface */ @@ -22,26 +22,33 @@ 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'); - + $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); } - + // 渲染视图 - $content = $factory->render($path, $data); - } catch (\Exception $e) { - $content = function_exists('throwable') ? throwable($e) : $e->getMessage(); + 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); } - - return $response->html($content); } /** @@ -104,7 +111,7 @@ class Response implements ResponseInterface /** * @param array $content - * @param int $statusCode + * @param int $statusCode * * @return ResponseInterface */ @@ -115,8 +122,8 @@ class Response implements ResponseInterface /** * @param string $url - * @param array $params - * @param int $statusCode + * @param array $params + * @param int $statusCode * * @return ResponseInterface */ @@ -127,7 +134,7 @@ class Response implements ResponseInterface /** * @param array $content - * @param int $statusCode + * @param int $statusCode * * @return ResponseInterface */ @@ -139,7 +146,7 @@ class Response implements ResponseInterface /** * @param string $content - * @param int $statusCode + * @param int $statusCode * * @return ResponseInterface */ @@ -151,7 +158,7 @@ class Response implements ResponseInterface /** * @param string $content - * @param int $statusCode + * @param int $statusCode * * @return ResponseInterface */ @@ -162,8 +169,8 @@ class Response implements ResponseInterface /** - * @param mixed $data - * @param int $statusCode + * @param mixed $data + * @param int $statusCode * @param ContentType $type * * @return Response @@ -176,7 +183,7 @@ class Response implements ResponseInterface /** * @param string $method - * @param mixed ...$params + * @param mixed ...$params * * @return mixed */ @@ -342,7 +349,7 @@ class Response implements ResponseInterface * immutability of the message, and MUST return an instance that has the * new and/or updated header and value. * - * @param string $name Case-insensitive header field name. + * @param string $name Case-insensitive header field name. * @param string|string[] $value Header value(s). * * @return static @@ -376,7 +383,7 @@ class Response implements ResponseInterface * immutability of the message, and MUST return an instance that has the * new header and/or value. * - * @param string $name Case-insensitive header field name to add. + * @param string $name Case-insensitive header field name to add. * @param string|string[] $value Header value(s). * * @return static @@ -454,8 +461,8 @@ class Response implements ResponseInterface /** - * @param string $content - * @param int $statusCode + * @param string $content + * @param int $statusCode * @param ContentType $contentType * * @return ResponseInterface @@ -479,7 +486,7 @@ class Response implements ResponseInterface * @link http://tools.ietf.org/html/rfc7231#section-6 * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml * - * @param int $code The 3-digit integer result code to set. + * @param int $code The 3-digit integer result code to set. * @param string $reasonPhrase The reason phrase to use with the * provided status code; if none is provided, implementations MAY * use the defaults as suggested in the HTTP specification. diff --git a/src/Router.php b/src/Router.php index b2080ec..8f0aa4e 100644 --- a/src/Router.php +++ b/src/Router.php @@ -192,7 +192,7 @@ class Router $container = Kiri::getDi(); $scanner = $container->get(Kiri\Di\Scanner::class); - $scanner->load_directory(APP_PATH . 'app/Controller'); + $scanner->scan(APP_PATH . 'app/'); $this->reset($container); $coordinator->done(); @@ -255,7 +255,7 @@ class Router try { include "$files"; } catch (\Throwable $throwable) { - error($throwable); + \Kiri::getLogger()->json_log($throwable); } } diff --git a/src/RouterCollector.php b/src/RouterCollector.php index d3081a6..6fe86d7 100644 --- a/src/RouterCollector.php +++ b/src/RouterCollector.php @@ -133,7 +133,7 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate $this->register($route, $value, $handler); } } catch (Throwable $throwable) { - error($throwable); + \Kiri::getLogger()->json_log($throwable); } } diff --git a/src/Validator/Types/MixedProxy.php b/src/Validator/Types/MixedProxy.php index a6641d7..40b09c7 100644 --- a/src/Validator/Types/MixedProxy.php +++ b/src/Validator/Types/MixedProxy.php @@ -21,8 +21,8 @@ class MixedProxy extends TypesProxy try { return $value == ($form->{$field} = $value); } catch (\Throwable $throwable) { - return false; + return $this->getLogger()->json_log($throwable, [], false); } } -} \ No newline at end of file +} diff --git a/src/Validator/Types/TypesProxy.php b/src/Validator/Types/TypesProxy.php index 0446e25..0eb78f2 100644 --- a/src/Validator/Types/TypesProxy.php +++ b/src/Validator/Types/TypesProxy.php @@ -2,7 +2,9 @@ namespace Kiri\Router\Validator\Types; -abstract class TypesProxy +use Kiri\Abstracts\Component; + +abstract class TypesProxy extends Component { @@ -20,4 +22,4 @@ abstract class TypesProxy */ abstract public function dispatch(object $form, string $field, mixed $value): bool; -} \ No newline at end of file +} diff --git a/src/template/error.php b/src/template/error.php new file mode 100644 index 0000000..48f7500 --- /dev/null +++ b/src/template/error.php @@ -0,0 +1,498 @@ + + + + + + Error: <?= htmlspecialchars($errorData['type']) ?> + + + + +
+
+

+ + +

+
+ +
+
+ +
+ +
+
+ Error Details + +
+
+
+ + : +

+ +
+ + + + + + + + + + + + + + +
Type
Code
Timestamp
+
+
+ + +
+
+ Stack Trace + +
+
+ $trace): ?> +
+
+ #: + () +
+
+ +
+ +
+ Arguments: +
+ +
+ +
+
+ + +
+
+ Server Information + +
+ +
+ + +
+
+ Request Information + +
+ +
+ + +
+
+
Memory Usage
+
+
+
+
Peak Memory
+
+
+
+
Execution Time
+
seconds
+
+
+
PHP Version
+
+
+
+ + +
+ + + + Go Home + + +
+
+
+ + + +