~thirdplace/components

8ce6124fd114046c612c23f8803f897a94443a82 — Dag a month ago 57e1109
yup
M src/Application.php => src/Application.php +1 -0
@@ 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));

M src/Renderer.php => src/Renderer.php +16 -1
@@ 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
}

M src/Router.php => src/Router.php +1 -0
@@ 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,

M src/Session.php => src/Session.php +1 -0
@@ 60,6 60,7 @@ final class Session

    public function get(string $key, $default = null)
    {
        // todo: guard against session not started
        return $_SESSION[$key] ?? $default;
    }


M src/common.php => src/common.php +2 -1
@@ 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);
        }
    }
}

M src/logger/StreamHandler.php => src/logger/StreamHandler.php +7 -2
@@ 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?
            }
        }
    }
}