diff --git a/app/core/App.php b/app/core/App.php new file mode 100644 index 0000000..738c7fa --- /dev/null +++ b/app/core/App.php @@ -0,0 +1,96 @@ + */ + private static array $services = []; + + /** + * Register or override a service value. + */ + public static function set(string $key, $value): void + { + self::$services[$key] = $value; + } + + /** + * Determine whether a value is registered. + */ + public static function has(string $key): bool + { + if (array_key_exists($key, self::$services)) { + return true; + } + return self::fallback($key) !== null; + } + + /** + * Retrieve a registered value. + * Falls back to legacy globals when no explicit service was registered. + */ + public static function get(string $key, $default = null) + { + if (array_key_exists($key, self::$services)) { + return self::$services[$key]; + } + $fallback = self::fallback($key); + return $fallback !== null ? $fallback : $default; + } + + /** + * Convenience accessor for the database connection. + */ + public static function db() + { + return self::get('db'); + } + + /** + * Convenience accessor for the configuration array. + */ + public static function config(): array + { + $config = self::get('config', []); + return is_array($config) ? $config : []; + } + + /** + * Convenience accessor for the authenticated user object, if any. + */ + public static function user() + { + return self::get('user'); + } + + /** + * Basic fallback bridge for legacy globals. + */ + private static function fallback(string $key) + { + switch ($key) { + case 'config': + return $GLOBALS['config'] ?? null; + case 'config_path': + return $GLOBALS['config_file'] ?? null; + case 'db': + return $GLOBALS['db'] ?? null; + case 'user': + case 'user_object': + return $GLOBALS['userObject'] ?? null; + case 'user_id': + return $GLOBALS['userId'] ?? null; + case 'logger': + return $GLOBALS['logObject'] ?? null; + case 'app_root': + return $GLOBALS['app_root'] ?? null; + default: + return null; + } + } +}