diff --git a/src/App/bootstrap.php b/src/App/bootstrap.php index c249515b..708567ec 100644 --- a/src/App/bootstrap.php +++ b/src/App/bootstrap.php @@ -22,3 +22,5 @@ spl_autoload_register(static function (string $class): void { } } }); + +require_once dirname(__DIR__) . '/ModulesCore/LegacyCompat.php'; diff --git a/src/ModulesCore/LegacyCompat.php b/src/ModulesCore/LegacyCompat.php new file mode 100644 index 00000000..a63bb885 --- /dev/null +++ b/src/ModulesCore/LegacyCompat.php @@ -0,0 +1,220 @@ +config === null) { + $this->config = new LegacyCompatConfig($this->projectRoot); + } + + return $this->config; + } + + public function auth(): LegacyCompatAuthContext + { + if ($this->auth === null) { + $this->auth = new LegacyCompatAuthContext($this->projectRoot); + } + + return $this->auth; + } + } + + final class LegacyCompatConfig + { + /** @var array */ + public array $dbConfig; + + public function __construct(string $projectRoot) + { + $baseDb = ConfigLoader::load($projectRoot, 'base_db'); + $db = ConfigLoader::load($projectRoot, 'db_settings_basic'); + $baseDbEnabled = (bool) ($baseDb['enabled'] ?? false); + $baseDbConfig = is_array($baseDb['db'] ?? null) ? $baseDb['db'] : []; + $dbConfig = is_array($db) ? $db : []; + + $this->dbConfig = $baseDbEnabled && $baseDbConfig !== [] + ? $baseDbConfig + : $dbConfig; + } + } + + final class LegacyCompatAuthContext + { + private ?KeycloakAuth $auth = null; + + public function __construct( + private readonly string $projectRoot, + ) { + } + + public function isEnabled(): bool + { + return $this->auth()->isEnabled(); + } + + /** + * @return array|null + */ + public function user(): ?array + { + $session = $_SESSION['desktop_auth']['user'] ?? null; + + return is_array($session) ? $session : null; + } + + private function auth(): KeycloakAuth + { + if ($this->auth === null) { + $this->auth = new KeycloakAuth(ConfigLoader::load($this->projectRoot, 'keycloak')); + } + + return $this->auth; + } + } + + final class LegacyCompatModuleRuntime + { + /** @var array> */ + private array $settingsCache = []; + + public function __construct( + private readonly string $projectRoot, + ) { + } + + public function isEnabled(string $moduleName): bool + { + return is_dir($this->modulePath($moduleName)); + } + + public function hasFunction(string $moduleName, string $functionName): bool + { + return false; + } + + public function call(string $moduleName, string $functionName, mixed ...$args): mixed + { + throw new RuntimeException(sprintf( + 'Modulfunktion %s::%s ist in dieser Laufzeit nicht registriert.', + $moduleName, + $functionName + )); + } + + /** + * @return array + */ + public function settings(string $moduleName): array + { + if (isset($this->settingsCache[$moduleName])) { + return $this->settingsCache[$moduleName]; + } + + $configPath = $this->modulePath($moduleName) . '/config/module.php'; + if (!is_file($configPath)) { + return $this->settingsCache[$moduleName] = []; + } + + $config = require $configPath; + + return $this->settingsCache[$moduleName] = is_array($config) ? $config : []; + } + + private function modulePath(string $moduleName): string + { + return $this->projectRoot . '/modules/' . trim($moduleName, '/'); + } + } +} + +namespace { + + use ModulesCore\LegacyCompat; + + if (!function_exists('app')) { + function app(): \ModulesCore\LegacyCompatAppContext + { + return LegacyCompat::app(); + } + } + + if (!function_exists('modules')) { + function modules(): \ModulesCore\LegacyCompatModuleRuntime + { + return LegacyCompat::modules(); + } + } + + if (!function_exists('module_fn')) { + function module_fn(string $moduleName, string $functionName, mixed ...$args): mixed + { + return modules()->call($moduleName, $functionName, ...$args); + } + } + + if (!function_exists('module_debug_enabled')) { + function module_debug_enabled(string $moduleName): bool + { + $settings = modules()->settings($moduleName); + $debug = $settings['debug'] ?? null; + + return is_array($debug) && (bool) ($debug['enabled'] ?? false); + } + } + + if (!function_exists('nexus_display_timezone_name')) { + function nexus_display_timezone_name(): string + { + $timezone = trim((string) date_default_timezone_get()); + + return $timezone !== '' ? $timezone : 'Europe/Berlin'; + } + } +}