config.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. * The configuration loader event name.
  20. *
  21. * @var string
  22. */
  23. const loader = 'laravel.config.loader';
  24. /**
  25. * Determine if a configuration item or file exists.
  26. *
  27. * <code>
  28. * // Determine if the "session" configuration file exists
  29. * $exists = Config::has('session');
  30. *
  31. * // Determine if the "timezone" option exists in the configuration
  32. * $exists = Config::has('application.timezone');
  33. * </code>
  34. *
  35. * @param string $key
  36. * @return bool
  37. */
  38. public static function has($key)
  39. {
  40. return ! is_null(static::get($key));
  41. }
  42. /**
  43. * Get a configuration item.
  44. *
  45. * If no item is requested, the entire configuration array will be returned.
  46. *
  47. * <code>
  48. * // Get the "session" configuration array
  49. * $session = Config::get('session');
  50. *
  51. * // Get a configuration item from a bundle's configuration file
  52. * $name = Config::get('admin::names.first');
  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 mixed $default
  60. * @return array
  61. */
  62. public static function get($key, $default = null)
  63. {
  64. list($bundle, $file, $item) = static::parse($key);
  65. if ( ! static::load($bundle, $file)) return value($default);
  66. $items = static::$items[$bundle][$file];
  67. // If a specific configuration item was not requested, the key will be null,
  68. // meaning we'll to return the entire array of configuration item from the
  69. // requested configuration file. Otherwise we can return the item.
  70. if (is_null($item))
  71. {
  72. return $items;
  73. }
  74. else
  75. {
  76. return array_get($items, $item, $default);
  77. }
  78. }
  79. /**
  80. * Set a configuration item's value.
  81. *
  82. * <code>
  83. * // Set the "session" configuration array
  84. * Config::set('session', $array);
  85. *
  86. * // Set a configuration option that belongs by a bundle
  87. * Config::set('admin::names.first', 'Taylor');
  88. *
  89. * // Set the "timezone" option in the "application" configuration file
  90. * Config::set('application.timezone', 'UTC');
  91. * </code>
  92. *
  93. * @param string $key
  94. * @param mixed $value
  95. * @return void
  96. */
  97. public static function set($key, $value)
  98. {
  99. list($bundle, $file, $item) = static::parse($key);
  100. static::load($bundle, $file);
  101. // If the item is null, it means the developer wishes to set the entire
  102. // configuration array to a given value, so we will pass the entire
  103. // array for the bundle into the array_set method.
  104. if (is_null($item))
  105. {
  106. array_set(static::$items[$bundle], $file, $value);
  107. }
  108. else
  109. {
  110. array_set(static::$items[$bundle][$file], $item, $value);
  111. }
  112. }
  113. /**
  114. * Parse a key and return its bundle, file, and key segments.
  115. *
  116. * Configuration items are named using the {bundle}::{file}.{item} convention.
  117. *
  118. * @param string $key
  119. * @return array
  120. */
  121. protected static function parse($key)
  122. {
  123. // First, we'll check the keyed cache of configuration items, as this will
  124. // be the fastest method of retrieving the configuration option. After an
  125. // item is parsed, it is always stored in the cache by its key.
  126. if (array_key_exists($key, static::$cache))
  127. {
  128. return static::$cache[$key];
  129. }
  130. $bundle = Bundle::name($key);
  131. $segments = explode('.', Bundle::element($key));
  132. // If there are not at least two segments in the array, it means that the
  133. // developer is requesting the entire configuration array to be returned.
  134. // If that is the case, we'll make the item field "null".
  135. if (count($segments) >= 2)
  136. {
  137. $parsed = array($bundle, $segments[0], implode('.', array_slice($segments, 1)));
  138. }
  139. else
  140. {
  141. $parsed = array($bundle, $segments[0], null);
  142. }
  143. return static::$cache[$key] = $parsed;
  144. }
  145. /**
  146. * Load all of the configuration items from a configuration file.
  147. *
  148. * @param string $bundle
  149. * @param string $file
  150. * @return bool
  151. */
  152. public static function load($bundle, $file)
  153. {
  154. if (isset(static::$items[$bundle][$file])) return true;
  155. // We allow a "config.loader" event to be registered which is responsible for
  156. // returning an array representing the configuration for the bundle and file
  157. // requested. This allows many types of config "drivers".
  158. $config = Event::first(static::loader, func_get_args());
  159. // If configuration items were actually found for the bundle and file we
  160. // will add them to the configuration array and return true, otherwise
  161. // we will return false indicating the file was not found.
  162. if (count($config) > 0)
  163. {
  164. static::$items[$bundle][$file] = $config;
  165. }
  166. return isset(static::$items[$bundle][$file]);
  167. }
  168. /**
  169. * Load the configuration items from a configuration file.
  170. *
  171. * @param string $bundle
  172. * @param string $file
  173. * @return array
  174. */
  175. public static function file($bundle, $file)
  176. {
  177. $config = array();
  178. // Configuration files cascade. Typically, the bundle configuration array is
  179. // loaded first, followed by the environment array, providing the convenient
  180. // cascading of configuration options across environments.
  181. foreach (static::paths($bundle) as $directory)
  182. {
  183. if ($directory !== '' and file_exists($path = $directory.$file.EXT))
  184. {
  185. $config = array_merge($config, require $path);
  186. }
  187. }
  188. return $config;
  189. }
  190. /**
  191. * Get the array of configuration paths that should be searched for a bundle.
  192. *
  193. * @param string $bundle
  194. * @return array
  195. */
  196. protected static function paths($bundle)
  197. {
  198. $paths[] = Bundle::path($bundle).'config/';
  199. // Configuration files can be made specific for a given environment. If an
  200. // environment has been set, we will merge the environment configuration
  201. // in last, so that it overrides all other options.
  202. if ( ! is_null(Request::env()))
  203. {
  204. $paths[] = $paths[count($paths) - 1].Request::env().'/';
  205. }
  206. return $paths;
  207. }
  208. }