Config.php 804 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. declare(strict_types=1);
  3. namespace Glacier\Core;
  4. /**
  5. * 全局配置(点号路径访问,如 app.debug / storage.logs)。
  6. */
  7. final class Config
  8. {
  9. private static array $items = [];
  10. public static function load(array $items): void
  11. {
  12. self::$items = $items;
  13. }
  14. public static function get(string $key, mixed $default = null): mixed
  15. {
  16. $current = self::$items;
  17. foreach (explode('.', $key) as $segment) {
  18. if (!is_array($current) || !array_key_exists($segment, $current)) {
  19. return $default;
  20. }
  21. $current = $current[$segment];
  22. }
  23. return $current;
  24. }
  25. /** 全部配置(调试/诊断用)。 */
  26. public static function all(): array
  27. {
  28. return self::$items;
  29. }
  30. }