| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- declare(strict_types=1);
- define('APP_ROOT', dirname(__DIR__));
- define('APP_DATA', APP_ROOT . '/data');
- function app_configure_php_error_logging(): void
- {
- ini_set('log_errors', '1');
- ini_set('error_log', APP_DATA . '/php_errors.log');
- }
- spl_autoload_register(static function (string $class): void {
- $prefix = 'App\\';
- if (!str_starts_with($class, $prefix)) {
- return;
- }
- $relative = substr($class, strlen($prefix));
- $path = APP_ROOT . '/src/' . str_replace('\\', '/', $relative) . '.php';
- if (is_file($path)) {
- require_once $path;
- }
- });
- date_default_timezone_set('Europe/Berlin');
- app_configure_php_error_logging();
- function app_config_repository(): App\ConfigRepository
- {
- static $repository = null;
- if ($repository === null) {
- $repository = new App\ConfigRepository(APP_DATA . '/config.json');
- }
- return $repository;
- }
- function app_state_repository(): App\StateRepository
- {
- static $repository = null;
- if ($repository === null) {
- $repository = new App\StateRepository(APP_DATA . '/state.json');
- }
- return $repository;
- }
- function app_alert_service(): App\AlertService
- {
- static $service = null;
- if ($service === null) {
- $service = new App\AlertService(app_config_repository(), APP_DATA . '/alert_log.json');
- }
- return $service;
- }
- function app_monitor_service(): App\MonitorService
- {
- static $service = null;
- if ($service === null) {
- $service = new App\MonitorService(
- app_config_repository(),
- app_state_repository(),
- app_alert_service()
- );
- }
- return $service;
- }
- function app_admin_auth(): App\AdminAuth
- {
- static $auth = null;
- if ($auth === null) {
- $auth = new App\AdminAuth(app_config_repository());
- }
- return $auth;
- }
- function app_json_response(array $payload, int $statusCode = 200): never
- {
- http_response_code($statusCode);
- header('Content-Type: application/json; charset=utf-8');
- echo json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
- exit;
- }
- function app_redirect(string $location): never
- {
- header('Location: ' . $location);
- exit;
- }
- function app_normalize_base_path(?string $basePath): string
- {
- $normalized = trim((string) $basePath);
- if ($normalized === '' || $normalized === '/') {
- return '';
- }
- $normalized = '/' . trim($normalized, '/');
- return $normalized === '/' ? '' : $normalized;
- }
- function app_base_path(): string
- {
- static $basePath = null;
- if ($basePath === null) {
- $config = app_config_repository()->getConfig();
- $basePath = app_normalize_base_path((string) ($config['app']['base_path'] ?? ''));
- }
- return $basePath;
- }
- function app_url(string $path = '/'): string
- {
- $basePath = app_base_path();
- $normalizedPath = '/' . ltrim($path, '/');
- if ($normalizedPath === '/') {
- return $basePath === '' ? '/' : $basePath . '/';
- }
- return $basePath . $normalizedPath;
- }
- function app_read_json_body(): array
- {
- $raw = file_get_contents('php://input');
- if ($raw === false || trim($raw) === '') {
- return [];
- }
- $decoded = json_decode($raw, true);
- if (!is_array($decoded)) {
- throw new RuntimeException('Ungültiger JSON-Body.');
- }
- return $decoded;
- }
|