bootstrap.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. declare(strict_types=1);
  3. define('APP_ROOT', dirname(__DIR__));
  4. define('APP_DATA', APP_ROOT . '/data');
  5. function app_configure_php_error_logging(): void
  6. {
  7. ini_set('log_errors', '1');
  8. ini_set('error_log', APP_DATA . '/php_errors.log');
  9. }
  10. spl_autoload_register(static function (string $class): void {
  11. $prefix = 'App\\';
  12. if (!str_starts_with($class, $prefix)) {
  13. return;
  14. }
  15. $relative = substr($class, strlen($prefix));
  16. $path = APP_ROOT . '/src/' . str_replace('\\', '/', $relative) . '.php';
  17. if (is_file($path)) {
  18. require_once $path;
  19. }
  20. });
  21. date_default_timezone_set('Europe/Berlin');
  22. app_configure_php_error_logging();
  23. function app_config_repository(): App\ConfigRepository
  24. {
  25. static $repository = null;
  26. if ($repository === null) {
  27. $repository = new App\ConfigRepository(APP_DATA . '/config.json');
  28. }
  29. return $repository;
  30. }
  31. function app_state_repository(): App\StateRepository
  32. {
  33. static $repository = null;
  34. if ($repository === null) {
  35. $repository = new App\StateRepository(APP_DATA . '/state.json');
  36. }
  37. return $repository;
  38. }
  39. function app_alert_service(): App\AlertService
  40. {
  41. static $service = null;
  42. if ($service === null) {
  43. $service = new App\AlertService(app_config_repository(), APP_DATA . '/alert_log.json');
  44. }
  45. return $service;
  46. }
  47. function app_monitor_service(): App\MonitorService
  48. {
  49. static $service = null;
  50. if ($service === null) {
  51. $service = new App\MonitorService(
  52. app_config_repository(),
  53. app_state_repository(),
  54. app_alert_service()
  55. );
  56. }
  57. return $service;
  58. }
  59. function app_admin_auth(): App\AdminAuth
  60. {
  61. static $auth = null;
  62. if ($auth === null) {
  63. $auth = new App\AdminAuth(app_config_repository());
  64. }
  65. return $auth;
  66. }
  67. function app_json_response(array $payload, int $statusCode = 200): never
  68. {
  69. http_response_code($statusCode);
  70. header('Content-Type: application/json; charset=utf-8');
  71. echo json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  72. exit;
  73. }
  74. function app_redirect(string $location): never
  75. {
  76. header('Location: ' . $location);
  77. exit;
  78. }
  79. function app_normalize_base_path(?string $basePath): string
  80. {
  81. $normalized = trim((string) $basePath);
  82. if ($normalized === '' || $normalized === '/') {
  83. return '';
  84. }
  85. $normalized = '/' . trim($normalized, '/');
  86. return $normalized === '/' ? '' : $normalized;
  87. }
  88. function app_base_path(): string
  89. {
  90. static $basePath = null;
  91. if ($basePath === null) {
  92. $config = app_config_repository()->getConfig();
  93. $basePath = app_normalize_base_path((string) ($config['app']['base_path'] ?? ''));
  94. }
  95. return $basePath;
  96. }
  97. function app_url(string $path = '/'): string
  98. {
  99. $basePath = app_base_path();
  100. $normalizedPath = '/' . ltrim($path, '/');
  101. if ($normalizedPath === '/') {
  102. return $basePath === '' ? '/' : $basePath . '/';
  103. }
  104. return $basePath . $normalizedPath;
  105. }
  106. function app_read_json_body(): array
  107. {
  108. $raw = file_get_contents('php://input');
  109. if ($raw === false || trim($raw) === '') {
  110. return [];
  111. }
  112. $decoded = json_decode($raw, true);
  113. if (!is_array($decoded)) {
  114. throw new RuntimeException('Ungültiger JSON-Body.');
  115. }
  116. return $decoded;
  117. }