config.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php namespace Laravel; defined('DS') or die('No direct script access.');
  2. use Closure;
  3. class Config {
  4. /**
  5. * All of the loaded configuration items.
  6. *
  7. * The configuration arrays are keyed by their owning bundle and file.
  8. *
  9. * @var array
  10. */
  11. public static $items = array();
  12. /**
  13. * A cache of the parsed configuration items.
  14. *
  15. * @var array
  16. */
  17. public static $cache = array();
  18. /**
  19. * Determine if a configuration item or file exists.
  20. *
  21. * <code>
  22. * // Determine if the "session" configuration file exists
  23. * $exists = Config::has('session');
  24. *
  25. * // Determine if the "timezone" option exists in the configuration
  26. * $exists = Config::has('application.timezone');
  27. * </code>
  28. *
  29. * @param string $key
  30. * @return bool
  31. */
  32. public static function has($key)
  33. {
  34. return ! is_null(static::get($key));
  35. }
  36. /**
  37. * Get a configuration item.
  38. *
  39. * If no item is requested, the entire configuration array will be returned.
  40. *
  41. * <code>
  42. * // Get the "session" configuration array
  43. * $session = Config::get('session');
  44. *
  45. * // Get a configuration item from a bundle's configuration file
  46. * $name = Config::get('admin::names.first');
  47. *
  48. * // Get the "timezone" option from the "application" configuration file
  49. * $timezone = Config::get('application.timezone');
  50. * </code>
  51. *
  52. * @param string $key
  53. * @param mixed $default
  54. * @return array
  55. */
  56. public static function get($key, $default = null)
  57. {
  58. list($bundle, $file, $item) = static::parse($key);
  59. if ( ! static::load($bundle, $file)) return value($default);
  60. $items = static::$items[$bundle][$file];
  61. // If a specific configuration item was not requested, the key will be null,
  62. // meaning we need to return the entire array of configuration item from the
  63. // requested configuration file. Otherwise we can return the item.
  64. if (is_null($item))
  65. {
  66. return $items;
  67. }
  68. else
  69. {
  70. return array_get($items, $item, $default);
  71. }
  72. }
  73. /**
  74. * Set a configuration item's value.
  75. *
  76. * <code>
  77. * // Set the "session" configuration array
  78. * Config::set('session', $array);
  79. *
  80. * // Set a configuration option that belongs by a bundle
  81. * Config::set('admin::names.first', 'Taylor');
  82. *
  83. * // Set the "timezone" option in the "application" configuration file
  84. * Config::set('application.timezone', 'UTC');
  85. * </code>
  86. *
  87. * @param string $key
  88. * @param mixed $value
  89. * @return void
  90. */
  91. public static function set($key, $value)
  92. {
  93. list($bundle, $file, $item) = static::parse($key);
  94. static::load($bundle, $file);
  95. // If the item is null, it means the developer wishes to set the entire
  96. // configuration array to a given value, so we will pass the entire
  97. // array for the bundle into the array_set method, otherwise we'll
  98. // only pass the file array for the bundle.
  99. if (is_null($item))
  100. {
  101. array_set(static::$items[$bundle], $file, $value);
  102. }
  103. else
  104. {
  105. array_set(static::$items[$bundle][$file], $item, $value);
  106. }
  107. }
  108. /**
  109. * Parse a key and return its bundle, file, and key segments.
  110. *
  111. * Configuration items are named using the {bundle}::{file}.{item} convention.
  112. *
  113. * @param string $key
  114. * @return array
  115. */
  116. protected static function parse($key)
  117. {
  118. // First, we'll check the keyed cache of configuration items, as this will
  119. // be the fastest method of retrieving the configuration option. After an
  120. // item is parsed, it is always stored in the cache by its key.
  121. if (array_key_exists($key, static::$cache))
  122. {
  123. return static::$cache[$key];
  124. }
  125. $bundle = Bundle::name($key);
  126. $segments = explode('.', Bundle::element($key));
  127. // If there are not at least two segments in the array, it means that the
  128. // developer is requesting the entire configuration array to be returned.
  129. // If that is the case, we'll make the item field "null".
  130. if (count($segments) >= 2)
  131. {
  132. $parsed = array($bundle, $segments[0], implode('.', array_slice($segments, 1)));
  133. }
  134. else
  135. {
  136. $parsed = array($bundle, $segments[0], null);
  137. }
  138. return static::$cache[$key] = $parsed;
  139. }
  140. /**
  141. * Load all of the configuration items from a configuration file.
  142. *
  143. * @param string $bundle
  144. * @param string $file
  145. * @return bool
  146. */
  147. public static function load($bundle, $file)
  148. {
  149. if (isset(static::$items[$bundle][$file])) return true;
  150. $config = array();
  151. // Configuration files cascade. Typically, the bundle configuration array is
  152. // loaded first, followed by the environment array, providing the convenient
  153. // cascading of configuration options across environments.
  154. foreach (static::paths($bundle) as $directory)
  155. {
  156. if ($directory !== '' and file_exists($path = $directory.$file.EXT))
  157. {
  158. $config = array_merge($config, require $path);
  159. }
  160. }
  161. if (count($config) > 0)
  162. {
  163. static::$items[$bundle][$file] = $config;
  164. }
  165. return isset(static::$items[$bundle][$file]);
  166. }
  167. /**
  168. * Get the array of configuration paths that should be searched for a bundle.
  169. *
  170. * @param string $bundle
  171. * @return array
  172. */
  173. protected static function paths($bundle)
  174. {
  175. $paths[] = Bundle::path($bundle).'config/';
  176. // Configuration files can be made specific for a given environment. If an
  177. // environment has been set, we will merge the environment configuration
  178. // in last, so that it overrides all other options.
  179. if (isset($_SERVER['LARAVEL_ENV']))
  180. {
  181. $paths[] = $paths[count($paths) - 1].$_SERVER['LARAVEL_ENV'].'/';
  182. }
  183. return $paths;
  184. }
  185. }