config.php 6.0 KB

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