Commit
This commit is contained in:
parent
983af52653
commit
0e202e9b96
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
config/config.php
|
@ -2,33 +2,24 @@
|
||||
|
||||
namespace WillySoft\Http;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
abstract class ErrorHandler {
|
||||
private static array $error_messages = [];
|
||||
|
||||
/**
|
||||
* Start capturing output to be replaced, set error and exception handlers and register shutdown function.
|
||||
*
|
||||
* On capturing an error or exception the callback will be called with an array of strings containing HTML error
|
||||
* messages as an argument.
|
||||
* On error or exception erase the output buffer then hand the errors as an array of HTML formatted messages to the callback
|
||||
*/
|
||||
public static function register(callable $callback)
|
||||
{
|
||||
static function register(callable $callback) {
|
||||
ob_start();
|
||||
|
||||
set_error_handler(function($errno, $errstr, $errfile, $errline) {
|
||||
error_log("Error[$errno]: $errstr in $errfile at line $errline\n");
|
||||
$errstr = htmlspecialchars($errstr);
|
||||
self::$error_messages[] = "<b>Error[$errno]:</b> $errstr in <b>$errfile</b> at line <b>$errline</b>";
|
||||
});
|
||||
|
||||
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) {
|
||||
@ -36,7 +27,6 @@ abstract class ErrorHandler
|
||||
return;
|
||||
}
|
||||
|
||||
// remove current output to be replaced by error page
|
||||
ob_end_clean();
|
||||
|
||||
http_response_code(500);
|
||||
|
8
config/default.config.php
Normal file
8
config/default.config.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
/**
|
||||
* Do not modify this file!
|
||||
* Copy and name it config.php to override these defaults.
|
||||
*/
|
||||
return [
|
||||
'debug' => false
|
||||
];
|
@ -26,8 +26,8 @@ http {
|
||||
error_page 404 /index.php;
|
||||
|
||||
location /radio/hbr1/trance.ogg {
|
||||
proxy_pass http://radio.hbr1.com/stream/trance.ogg;
|
||||
proxy_buffering off;
|
||||
proxy_pass http://radio.hbr1.com/stream/trance.ogg;
|
||||
proxy_buffering off;
|
||||
}
|
||||
|
||||
location / {
|
||||
@ -35,8 +35,9 @@ http {
|
||||
}
|
||||
|
||||
location ~ \.php$ {
|
||||
fastcgi_pass 127.0.0.1:9000;
|
||||
include fastcgi.conf;
|
||||
try_files $uri = 404;
|
||||
fastcgi_pass 127.0.0.1:9000;
|
||||
include fastcgi.conf;
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
*/
|
||||
|
@ -3,9 +3,18 @@
|
||||
use WillySoft\Http\ErrorHandler;
|
||||
use WillySoft\Http\Route;
|
||||
|
||||
ErrorHandler::register(function($error_messages) {
|
||||
view('errors/500', ['error_messages' => $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]);
|
||||
} 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'));
|
||||
|
||||
|
@ -3,8 +3,8 @@
|
||||
use WillySoft\Http\Route;
|
||||
|
||||
abstract class WillyChat {
|
||||
public static string $data_path;
|
||||
public static array $messages;
|
||||
static string $data_path;
|
||||
static array $messages;
|
||||
}
|
||||
|
||||
Route::middleware(function() {
|
||||
@ -57,7 +57,7 @@ Route::match('get|post','/willychat/', function() {
|
||||
})();
|
||||
|
||||
if (!$errmsg) {
|
||||
if (count(WillyChat::$messages) > 10) {
|
||||
if (count(WillyChat::$messages) > 100) {
|
||||
array_pop(WillyChat::$messages);
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Internal Server Error 500</title>
|
||||
<title>500 Internal Server Error</title>
|
||||
</head>
|
||||
<body>
|
||||
<style>
|
||||
|
Loading…
Reference in New Issue
Block a user