config.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php namespace Laravel; isset($GLOBALS['APP_PATH']) 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 loaded 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. * @return array
  54. */
  55. public static function get($key)
  56. {
  57. // First, we'll check the keyed cache of configuration items, as this will
  58. // be the fastest method of retrieving the configuration option. After an
  59. // item is retrieved, it is always stored in the cache by its key.
  60. if (array_key_exists($key, static::$cache))
  61. {
  62. return static::$cache[$key];
  63. }
  64. list($bundle, $file, $item) = static::parse($key);
  65. if ( ! static::load($bundle, $file)) return;
  66. $items = static::$items[$bundle][$file];
  67. // If a specific configuration item was not requested, the key will be null,
  68. // meaning we need to return the entire array of configuration item from the
  69. // requested configuration file. Otherwise we can return the item.
  70. $value = (is_null($item)) ? $items : array_get($items, $item);
  71. return static::$cache[$key] = $value;
  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. static::$cache[$key] = $value;
  94. }
  95. /**
  96. * Parse a key and return its bundle, file, and key segments.
  97. *
  98. * Configuration items are named using the {bundle}::{file}.{item} convention.
  99. *
  100. * @param string $key
  101. * @return array
  102. */
  103. protected static function parse($key)
  104. {
  105. $bundle = Bundle::name($key);
  106. $segments = explode('.', Bundle::element($key));
  107. // If there are not at least two segments in the array, it means that the
  108. // developer is requesting the entire configuration array to be returned.
  109. // If that is the case, we'll make the item field of the array "null".
  110. if (count($segments) >= 2)
  111. {
  112. return array($bundle, $segments[0], implode('.', array_slice($segments, 1)));
  113. }
  114. else
  115. {
  116. return array($bundle, $segments[0], null);
  117. }
  118. }
  119. /**
  120. * Load all of the configuration items from a configuration file.
  121. *
  122. * @param string $bundle
  123. * @param string $file
  124. * @return bool
  125. */
  126. public static function load($bundle, $file)
  127. {
  128. if (isset(static::$items[$bundle][$file])) return true;
  129. $config = array();
  130. // Configuration files cascade. Typically, the bundle configuration array is
  131. // loaded first, followed by the environment array, providing the convenient
  132. // cascading of configuration options across environments.
  133. foreach (static::paths($bundle) as $directory)
  134. {
  135. if ($directory !== '' and file_exists($path = $directory.$file.EXT))
  136. {
  137. $config = array_merge($config, require $path);
  138. }
  139. }
  140. if (count($config) > 0)
  141. {
  142. static::$items[$bundle][$file] = $config;
  143. }
  144. return isset(static::$items[$bundle][$file]);
  145. }
  146. /**
  147. * Get the array of configuration paths that should be searched for a bundle.
  148. *
  149. * @param string $bundle
  150. * @return array
  151. */
  152. protected static function paths($bundle)
  153. {
  154. $paths[] = Bundle::path($bundle).'config/';
  155. // Configuration files can be made specific for a given environment. If an
  156. // environment has been set, we will merge the environment configuration
  157. // in last, so that it overrides all other options.
  158. //
  159. // This allows the developer to quickly and easily create configurations
  160. // for various scenarios, such as local development and production,
  161. // without constantly changing configuration files.
  162. if (isset($_SERVER['LARAVEL_ENV']))
  163. {
  164. $paths[] = $paths[count($paths) - 1].$_SERVER['LARAVEL_ENV'].'/';
  165. }
  166. return $paths;
  167. }
  168. }