config.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. protected static $items = array();
  11. /**
  12. * The paths to the configuration files.
  13. *
  14. * @var array
  15. */
  16. protected static $paths = array();
  17. /**
  18. * Set the paths in which the configuration files are located.
  19. *
  20. * @param array $paths
  21. * @return void
  22. */
  23. public static function paths($paths)
  24. {
  25. static::$paths = $paths;
  26. }
  27. /**
  28. * Determine if a configuration item or file exists.
  29. *
  30. * <code>
  31. * // Determine if the "session" configuration file exists
  32. * $exists = Config::has('session');
  33. *
  34. * // Determine if the "timezone" option exists in the "application" configuration array
  35. * $exists = Config::has('application.timezone');
  36. * </code>
  37. *
  38. * @param string $key
  39. * @return bool
  40. */
  41. public static function has($key)
  42. {
  43. return ! is_null(static::get($key));
  44. }
  45. /**
  46. * Get a configuration item.
  47. *
  48. * If no item is requested, the entire configuration array will be returned.
  49. *
  50. * <code>
  51. * // Get the "session" configuration array
  52. * $session = Config::get('session');
  53. *
  54. * // Get the "timezone" option from the "application" configuration file
  55. * $timezone = Config::get('application.timezone');
  56. * </code>
  57. *
  58. * @param string $key
  59. * @param string $default
  60. * @return array
  61. */
  62. public static function get($key, $default = null)
  63. {
  64. list($file, $key) = static::parse($key);
  65. if ( ! static::load($file))
  66. {
  67. return ($default instanceof Closure) ? call_user_func($default) : $default;
  68. }
  69. if (is_null($key)) return static::$items[$file];
  70. return Arr::get(static::$items[$file], $key, $default);
  71. }
  72. /**
  73. * Set a configuration item's value.
  74. *
  75. * <code>
  76. * // Set the "session" configuration array
  77. * Config::set('session', $array);
  78. *
  79. * // Set the "timezone" option in the "application" configuration file
  80. * Config::set('application.timezone', 'UTC');
  81. * </code>
  82. *
  83. * @param string $key
  84. * @param mixed $value
  85. * @return void
  86. */
  87. public static function set($key, $value)
  88. {
  89. list($file, $key) = static::parse($key);
  90. static::load($file);
  91. (is_null($key)) ? Arr::set(static::$items, $file, $value) : Arr::set(static::$items[$file], $key, $value);
  92. }
  93. /**
  94. * Parse a configuration key and return its file and key segments.
  95. *
  96. * @param string $key
  97. * @return array
  98. */
  99. protected static function parse($key)
  100. {
  101. $segments = explode('.', $key);
  102. // If there is only one segment after exploding on dots, we will return NULL
  103. // as the key value, causing the entire configuration array to be returned.
  104. $key = (count($segments) > 1) ? implode('.', array_slice($segments, 1)) : null;
  105. return array($segments[0], $key);
  106. }
  107. /**
  108. * Load all of the configuration items from a configuration file.
  109. *
  110. * @param string $file
  111. * @return bool
  112. */
  113. protected static function load($file)
  114. {
  115. if (isset(static::$items[$file])) return true;
  116. $config = array();
  117. // Configuration files cascade. Typically, the system configuration array is loaded
  118. // first, followed by the application array, followed by the environment array.
  119. // This allows the convenient overriding of configuration options.
  120. foreach (static::$paths as $directory)
  121. {
  122. if (file_exists($path = $directory.$file.EXT))
  123. {
  124. $config = array_merge($config, require $path);
  125. }
  126. }
  127. // If configuration options were actually found, they will be loaded into the
  128. // array containing all of the options for all files. The array is keyed by the
  129. // configuration file name.
  130. if (count($config) > 0) static::$items[$file] = $config;
  131. return isset(static::$items[$file]);
  132. }
  133. }