config.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php namespace Laravel; use Closure;
  2. class Config {
  3. /**
  4. * All of the loaded configuration items.
  5. *
  6. * The configuration arrays are keyed by their owning file name.
  7. *
  8. * @var array
  9. */
  10. public static $items = array();
  11. /**
  12. * The paths to the configuration files.
  13. *
  14. * @var array
  15. */
  16. public static $paths = array(SYS_CONFIG_PATH, CONFIG_PATH, ENV_CONFIG_PATH);
  17. /**
  18. * Determine if a configuration item or file exists.
  19. *
  20. * <code>
  21. * // Determine if the "session" configuration file exists
  22. * $exists = Config::has('session');
  23. *
  24. * // Determine if the "timezone" option exists in the "application" configuration
  25. * $exists = Config::has('application.timezone');
  26. * </code>
  27. *
  28. * @param string $key
  29. * @return bool
  30. */
  31. public static function has($key)
  32. {
  33. return ! is_null(static::get($key));
  34. }
  35. /**
  36. * Get a configuration item.
  37. *
  38. * If no item is requested, the entire configuration array will be returned.
  39. *
  40. * <code>
  41. * // Get the "session" configuration array
  42. * $session = Config::get('session');
  43. *
  44. * // Get the "timezone" option from the "application" configuration file
  45. * $timezone = Config::get('application.timezone');
  46. * </code>
  47. *
  48. * @param string $key
  49. * @param string $default
  50. * @return array
  51. */
  52. public static function get($key, $default = null)
  53. {
  54. list($file, $key) = static::parse($key);
  55. if ( ! static::load($file))
  56. {
  57. return ($default instanceof Closure) ? call_user_func($default) : $default;
  58. }
  59. $items = static::$items[$file];
  60. // If a specific configuration item was not requested, the key will be null,
  61. // meaning we need to return the entire array of configuration item from the
  62. // requested configuration file. Otherwise we can return the item.
  63. return (is_null($key)) ? $items : Arr::get($items, $key, $default);
  64. }
  65. /**
  66. * Set a configuration item's value.
  67. *
  68. * <code>
  69. * // Set the "session" configuration array
  70. * Config::set('session', $array);
  71. *
  72. * // Set the "timezone" option in the "application" configuration file
  73. * Config::set('application.timezone', 'UTC');
  74. * </code>
  75. *
  76. * @param string $key
  77. * @param mixed $value
  78. * @return void
  79. */
  80. public static function set($key, $value)
  81. {
  82. list($file, $key) = static::parse($key);
  83. static::load($file);
  84. if (is_null($key))
  85. {
  86. Arr::set(static::$items, $file, $value);
  87. }
  88. else
  89. {
  90. Arr::set(static::$items[$file], $key, $value);
  91. }
  92. }
  93. /**
  94. * Parse a configuration key and return its file and key segments.
  95. *
  96. * The first segment of a configuration key represents the configuration
  97. * file, while the remaining segments represent an item within that file.
  98. * If no item segment is present, null will be returned for the item value
  99. * indicating that the entire configuration array should be returned.
  100. *
  101. * @param string $key
  102. * @return array
  103. */
  104. protected static function parse($key)
  105. {
  106. $segments = explode('.', $key);
  107. if (count($segments) >= 2)
  108. {
  109. return array($segments[0], implode('.', array_slice($segments, 1)));
  110. }
  111. else
  112. {
  113. return array($segments[0], null);
  114. }
  115. }
  116. /**
  117. * Load all of the configuration items from a configuration file.
  118. *
  119. * @param string $file
  120. * @return bool
  121. */
  122. public static function load($file)
  123. {
  124. if (isset(static::$items[$file])) return true;
  125. $config = array();
  126. // Configuration files cascade. Typically, the system configuration array is
  127. // loaded first, followed by the application array, providing the convenient
  128. // cascading of configuration options from system to application.
  129. foreach (static::$paths as $directory)
  130. {
  131. if ($directory !== '' and file_exists($path = $directory.$file.EXT))
  132. {
  133. $config = array_merge($config, require $path);
  134. }
  135. }
  136. if (count($config) > 0) static::$items[$file] = $config;
  137. return isset(static::$items[$file]);
  138. }
  139. }