config.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php namespace System;
  2. class Config {
  3. /**
  4. * All of the loaded configuration items.
  5. *
  6. * @var array
  7. */
  8. private static $items = array();
  9. /**
  10. * Determine if a configuration item exists.
  11. *
  12. * @param string $key
  13. * @return bool
  14. */
  15. public static function has($key)
  16. {
  17. return ! is_null(static::get($key));
  18. }
  19. /**
  20. * Get a configuration item.
  21. *
  22. * @param string $key
  23. * @param string $default
  24. * @return mixed
  25. */
  26. public static function get($key, $default = null)
  27. {
  28. // -----------------------------------------------------
  29. // If no dot is in the key, we will just return the
  30. // entire configuration array.
  31. // -----------------------------------------------------
  32. if(strpos($key, '.') === false)
  33. {
  34. static::load($key);
  35. return (array_key_exists($key, static::$items)) ? static::$items[$key] : $default;
  36. }
  37. list($file, $key) = static::parse($key);
  38. static::load($file);
  39. // -----------------------------------------------------
  40. // If the file doesn't exist, return the default.
  41. // -----------------------------------------------------
  42. if ( ! array_key_exists($file, static::$items))
  43. {
  44. return $default;
  45. }
  46. // -----------------------------------------------------
  47. // Return the configuration item. If the item doesn't
  48. // exist, the default value will be returned.
  49. // -----------------------------------------------------
  50. return (array_key_exists($key, static::$items[$file])) ? static::$items[$file][$key] : $default;
  51. }
  52. /**
  53. * Set a configuration item.
  54. *
  55. * @param string $key
  56. * @param mixed $value
  57. * @return void
  58. */
  59. public static function set($key, $value)
  60. {
  61. list($file, $key) = static::parse($key);
  62. static::load($file);
  63. static::$items[$file][$key] = $value;
  64. }
  65. /**
  66. * Parse a configuration key.
  67. *
  68. * @param string $key
  69. * @return array
  70. */
  71. private static function parse($key)
  72. {
  73. // -----------------------------------------------------
  74. // The left side of the dot is the file name, while
  75. // the right side of the dot is the item within that
  76. // file being requested.
  77. // -----------------------------------------------------
  78. $segments = explode('.', $key);
  79. if (count($segments) < 2)
  80. {
  81. throw new \Exception("Invalid configuration key [$key].");
  82. }
  83. return array($segments[0], implode('.', array_slice($segments, 1)));
  84. }
  85. /**
  86. * Load all of the configuration items.
  87. *
  88. * @param string $file
  89. * @return void
  90. */
  91. public static function load($file)
  92. {
  93. // -----------------------------------------------------
  94. // Bail out if already loaded or doesn't exist.
  95. // -----------------------------------------------------
  96. if (array_key_exists($file, static::$items) or ! file_exists($path = APP_PATH.'config/'.$file.EXT))
  97. {
  98. return;
  99. }
  100. // -----------------------------------------------------
  101. // Load the configuration array into the array of items.
  102. // The items array is keyed by filename.
  103. // -----------------------------------------------------
  104. static::$items[$file] = require $path;
  105. }
  106. }