2022-11-29 12:49:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// PSR-4 like autoloader
|
|
|
|
spl_autoload_register(
|
|
|
|
function ($class_name) {
|
|
|
|
$path = __DIR__ . '/../app/' . str_replace('\\', '/', $class_name) . '.php';
|
|
|
|
require $path;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-01-29 12:35:00 +00:00
|
|
|
/**
|
|
|
|
* 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)) {
|
2023-01-30 08:15:42 +00:00
|
|
|
trigger_error('Undefined key in config file', E_USER_ERROR);
|
2023-01-29 12:35:00 +00:00
|
|
|
}
|
|
|
|
if (gettype($override_value) !== gettype($config[$override_key])) {
|
2023-01-30 08:15:42 +00:00
|
|
|
trigger_error('Type mismatch in config file', E_USER_ERROR);
|
2023-01-29 12:35:00 +00:00
|
|
|
}
|
|
|
|
$config[$override_key] = $override_value;
|
|
|
|
}
|
|
|
|
return $config[$key];
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:49:29 +00:00
|
|
|
/**
|
|
|
|
* Helper for evaluating/including views
|
|
|
|
*/
|
|
|
|
function view(string $_view, array $_data = [])
|
|
|
|
{
|
|
|
|
$_path = __DIR__ . '/../views/' . $_view . '.php';
|
|
|
|
extract($_data);
|
|
|
|
require $_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper for converting and outputting JSON
|
|
|
|
*/
|
|
|
|
function json_response(mixed $data, int $status_code = 200)
|
|
|
|
{
|
|
|
|
http_response_code($status_code);
|
|
|
|
header('Content-type: application/json');
|
|
|
|
echo json_encode($data);
|
|
|
|
}
|
|
|
|
|
2023-01-20 19:33:45 +00:00
|
|
|
/**
|
|
|
|
* Helper for reading and decoding JSON from request body
|
|
|
|
*/
|
2023-01-30 08:15:42 +00:00
|
|
|
function json_decode_input(): array|bool|null
|
2023-01-20 19:33:45 +00:00
|
|
|
{
|
|
|
|
return json_decode(file_get_contents('php://input'), true);
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:49:29 +00:00
|
|
|
/**
|
|
|
|
* Helper for generating URLs
|
|
|
|
*/
|
|
|
|
function url(string $url, bool $full = false): string
|
|
|
|
{
|
|
|
|
$dir = dirname($_SERVER['SCRIPT_NAME']);
|
|
|
|
if ($dir === '/') {
|
|
|
|
$dir = '';
|
|
|
|
}
|
|
|
|
if (!$full) {
|
|
|
|
return $dir . $url;
|
|
|
|
}
|
|
|
|
return isset($_SERVER['HTTPS']) ? 'https' : 'http' . '://' . $_SERVER['HTTP_HOST'] . $dir . $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now lets get routing!
|
|
|
|
require __DIR__ . '/../routes/start.php';
|