This commit is contained in:
William 2023-01-29 13:35:00 +01:00
parent 983af52653
commit 0e202e9b96
8 changed files with 60 additions and 26 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config/config.php

View File

@ -2,33 +2,24 @@
namespace WillySoft\Http; namespace WillySoft\Http;
/** abstract class ErrorHandler {
* Captures errors and exceptions thrown by your application and renders them to the user
*/
abstract class ErrorHandler
{
/**
* Array of strings containing HTML error and exception messages
*/
private static array $error_messages = []; private static array $error_messages = [];
/** /**
* Start capturing output to be replaced, set error and exception handlers and register shutdown function. * On error or exception erase the output buffer then hand the errors as an array of HTML formatted messages to the callback
*
* On capturing an error or exception the callback will be called with an array of strings containing HTML error
* messages as an argument.
*/ */
public static function register(callable $callback) static function register(callable $callback) {
{
ob_start(); ob_start();
set_error_handler(function($errno, $errstr, $errfile, $errline) { set_error_handler(function($errno, $errstr, $errfile, $errline) {
error_log("Error[$errno]: $errstr in $errfile at line $errline\n");
$errstr = htmlspecialchars($errstr); $errstr = htmlspecialchars($errstr);
self::$error_messages[] = "<b>Error[$errno]:</b> $errstr in <b>$errfile</b> at line <b>$errline</b>"; self::$error_messages[] = "<b>Error[$errno]:</b> $errstr in <b>$errfile</b> at line <b>$errline</b>";
}); });
set_exception_handler(function($exception) { set_exception_handler(function($exception) {
self::$error_messages[] = "<b>Uncaught Exception:</b> " . $exception; error_log("Uncaught Exception: $exception\n");
self::$error_messages[] = "<b>Uncaught Exception:</b> {$exception}";
}); });
register_shutdown_function(function() use(&$callback) { register_shutdown_function(function() use(&$callback) {
@ -36,7 +27,6 @@ abstract class ErrorHandler
return; return;
} }
// remove current output to be replaced by error page
ob_end_clean(); ob_end_clean();
http_response_code(500); http_response_code(500);

View File

@ -0,0 +1,8 @@
<?php
/**
* Do not modify this file!
* Copy and name it config.php to override these defaults.
*/
return [
'debug' => false
];

View File

@ -35,6 +35,7 @@ http {
} }
location ~ \.php$ { location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000; fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf; include fastcgi.conf;
} }

View File

@ -8,6 +8,31 @@ spl_autoload_register(
} }
); );
/**
* Shitty config loader
*/
function config(string $key): bool|string|int|float {
static $config;
if (isset($config)) {
return $config[$key];
}
$config = require __DIR__ . '/../config/default.config.php';
$config_override_path = __DIR__ . '/../config/config.php';
if (!file_exists($config_override_path)) {
return $config[$key];
}
foreach (require $config_override_path as $override_key => $override_value) {
if (!array_key_exists($override_key, $config)) {
throw new Exception('Undefined key in config override file');
}
if (gettype($override_value) !== gettype($config[$override_key])) {
throw new Exception('Type mismatch in config override file');
}
$config[$override_key] = $override_value;
}
return $config[$key];
}
/** /**
* Helper for evaluating/including views * Helper for evaluating/including views
*/ */

View File

@ -3,9 +3,18 @@
use WillySoft\Http\ErrorHandler; use WillySoft\Http\ErrorHandler;
use WillySoft\Http\Route; use WillySoft\Http\Route;
ErrorHandler::register(function($error_messages) { // enable debug here so we display config parse errors to the user
$debug = true;
ErrorHandler::register(function($error_messages) use (&$debug) {
if ($debug) {
view('errors/500', ['error_messages' => $error_messages]); view('errors/500', ['error_messages' => $error_messages]);
} else {
view('errors/500', ['error_messages' => []]);
}
}); });
// if config loads successfully use that value instead
$debug = config('debug');
unset($debug);
Route::get('/', fn() => view('pages/home')); Route::get('/', fn() => view('pages/home'));

View File

@ -3,8 +3,8 @@
use WillySoft\Http\Route; use WillySoft\Http\Route;
abstract class WillyChat { abstract class WillyChat {
public static string $data_path; static string $data_path;
public static array $messages; static array $messages;
} }
Route::middleware(function() { Route::middleware(function() {
@ -57,7 +57,7 @@ Route::match('get|post','/willychat/', function() {
})(); })();
if (!$errmsg) { if (!$errmsg) {
if (count(WillyChat::$messages) > 10) { if (count(WillyChat::$messages) > 100) {
array_pop(WillyChat::$messages); array_pop(WillyChat::$messages);
} }

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Internal Server Error 500</title> <title>500 Internal Server Error</title>
</head> </head>
<body> <body>
<style> <style>