From 8ce6124fd114046c612c23f8803f897a94443a82 Mon Sep 17 00:00:00 2001 From: Dag Date: Mon, 17 Oct 2022 03:11:54 +0200 Subject: [PATCH] yup --- src/Application.php | 1 + src/Renderer.php | 17 ++++++++++++++++- src/Router.php | 1 + src/Session.php | 1 + src/common.php | 3 ++- src/logger/StreamHandler.php | 9 +++++++-- 6 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Application.php b/src/Application.php index 16c0296..d89381a 100755 --- a/src/Application.php +++ b/src/Application.php @@ -16,6 +16,7 @@ final class Application $this->container = $container; $this->router = new Router(); + // perhaps add a default logger ErrorHandler::register($container['error_logger']); $this->addRoute('GET', '/', fn() => response("Hello World!\n", 200)); diff --git a/src/Renderer.php b/src/Renderer.php index 0b2743b..1513a08 100755 --- a/src/Renderer.php +++ b/src/Renderer.php @@ -19,8 +19,10 @@ final class Renderer $this->context = $this->config['context']; } + // This method not used much because each app typically customises it public function render(string $filePath, array $context = []): string { + // might be bug here because the context contains values from previous calls $this->context = array_merge($this->context, $context); extract($this->context); ob_start(); @@ -51,6 +53,19 @@ final class Renderer } } +function render_template(string $template, array $context = []) +{ + extract($context); + ob_start(); + try { + require $template; + } catch (\Throwable $e) { + ob_end_clean(); + throw $e; + } + return ob_get_clean(); +} + /** * Escape for html context */ @@ -97,4 +112,4 @@ function truncate(string $s, int $length = 100, $marker = ' [...]'): string $properTruncate = mb_substr($s, 0, $lastSpace); return $properTruncate . $marker; -} \ No newline at end of file +} diff --git a/src/Router.php b/src/Router.php index e2ae7cb..eb3f7f4 100755 --- a/src/Router.php +++ b/src/Router.php @@ -17,6 +17,7 @@ final class Router public function addRoute($method, string $pattern, $handler): void { $this->routes[$pattern] = [ + // todo: add support for BOTH which means ['GET', 'POST'] 'methods' => (array) $method, 'pattern' => $pattern, 'handler' => $handler, diff --git a/src/Session.php b/src/Session.php index 6919f62..881ca11 100755 --- a/src/Session.php +++ b/src/Session.php @@ -60,6 +60,7 @@ final class Session public function get(string $key, $default = null) { + // todo: guard against session not started return $_SESSION[$key] ?? $default; } diff --git a/src/common.php b/src/common.php index cf027fc..55085b7 100755 --- a/src/common.php +++ b/src/common.php @@ -47,7 +47,8 @@ class ExceptionMiddleware try { return $next($request); } catch (\Throwable $e) { - return ($this->fn)($e); + // todo: consider wrapping this call in a try too? + return ($this->fn)($e); } } } diff --git a/src/logger/StreamHandler.php b/src/logger/StreamHandler.php index cc3d269..8d50541 100755 --- a/src/logger/StreamHandler.php +++ b/src/logger/StreamHandler.php @@ -15,7 +15,10 @@ final class StreamHandler public function __invoke(array $record): void { if (isset($record['context']['e'])) { - $record['context']['e'] = create_sane_stacktrace($record['context']['e']); + // todo: improve message + $record['context']['message'] = $e->getMessage(); + $record['context']['code'] = $e->getCode(); + $record['context']['trace'] = create_sane_stacktrace($record['context']['e']); } if ($record['context'] === []) { $record['context'] = ''; @@ -40,7 +43,9 @@ final class StreamHandler if (!fwrite($this->stream, $text)) { // Maybe drop this last effort to write to error log - error_log('Unable to write log record'); + if (!error_log('Unable to write log record: ' . $record['message']])) { + // todo: write to stderr or stdout? + } } } } -- 2.45.2