config.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php namespace Laravel;
  2. class Config {
  3. /**
  4. * All of the loaded configuration items.
  5. *
  6. * The configuration arrays are keyed by module and file names.
  7. *
  8. * @var array
  9. */
  10. public static $items = array();
  11. /**
  12. * Determine if a configuration item or file exists.
  13. *
  14. * <code>
  15. * // Determine if the "session" configuration file exists
  16. * Config::has('session');
  17. *
  18. * // Determine if the application timezone option exists
  19. * Config::has('application.timezone');
  20. * </code>
  21. *
  22. * @param string $key
  23. * @return bool
  24. */
  25. public static function has($key)
  26. {
  27. return ! is_null(static::get($key));
  28. }
  29. /**
  30. * Get a configuration item.
  31. *
  32. * Configuration items are retrieved using "dot" notation. So, asking for the
  33. * "application.timezone" configuration item would return the "timezone" option
  34. * from the "application" configuration file.
  35. *
  36. * If the name of a configuration file is passed without specifying an item, the
  37. * entire configuration array will be returned.
  38. *
  39. * <code>
  40. * // Get the timezone option from the application configuration file
  41. * $timezone = Config::get('application.timezone');
  42. *
  43. * // Get the SQLite database connection configuration
  44. * $sqlite = Config::get('db.connections.sqlite');
  45. * </code>
  46. *
  47. * @param string $key
  48. * @param string $default
  49. * @return array
  50. */
  51. public static function get($key, $default = null)
  52. {
  53. list($module, $file, $key) = static::parse($key);
  54. if ( ! static::load($module, $file))
  55. {
  56. return is_callable($default) ? call_user_func($default) : $default;
  57. }
  58. if (is_null($key)) return static::$items[$module][$file];
  59. return Arr::get(static::$items[$module][$file], $key, $default);
  60. }
  61. /**
  62. * Set a configuration item.
  63. *
  64. * Like the get method, "dot" notation is used to set items, and setting items
  65. * at any depth in the configuration array is supported.
  66. *
  67. * If a specific configuration item is not specified, the entire configuration
  68. * array will be replaced with the given value.
  69. *
  70. * <code>
  71. * // Set the timezone option in the application configuration file
  72. * Config::set('application.timezone', 'America/Chicago');
  73. *
  74. * // Set the session configuration array
  75. * Config::set('session', array());
  76. * </code>
  77. *
  78. * @param string $key
  79. * @param mixed $value
  80. * @return void
  81. */
  82. public static function set($key, $value)
  83. {
  84. list($module, $file, $key) = static::parse($key);
  85. if ( ! static::load($module, $file))
  86. {
  87. throw new \Exception("Error setting configuration option. Configuration file [$file] is not defined.");
  88. }
  89. Arr::set(static::$items[$module][$file], $key, $value);
  90. }
  91. /**
  92. * Parse a configuration key and return its module, file, and key segments.
  93. *
  94. * Modular configuration keys follow a {module}::{file}.{key} convention.
  95. *
  96. * @param string $key
  97. * @return array
  98. */
  99. private static function parse($key)
  100. {
  101. list($module, $key) = Module::parse($key);
  102. $segments = explode('.', $key);
  103. return array($module, $segments[0], (count($segments) > 1) ? implode('.', array_slice($segments, 1)) : null);
  104. }
  105. /**
  106. * Load all of the configuration items from a module configuration file.
  107. *
  108. * If the configuration file has already been loaded, it will not be loaded again.
  109. *
  110. * @param string $file
  111. * @param string $module
  112. * @return bool
  113. */
  114. private static function load($module, $file)
  115. {
  116. if (isset(static::$items[$module]) and array_key_exists($file, static::$items[$module])) return true;
  117. $config = array();
  118. foreach (static::paths($module, $file) as $directory)
  119. {
  120. $config = (file_exists($path = $directory.$file.EXT)) ? array_merge($config, require $path) : $config;
  121. }
  122. if (count($config) > 0) static::$items[$module][$file] = $config;
  123. return isset(static::$items[$module][$file]);
  124. }
  125. /**
  126. * Get the path hierarchy for a given configuration file and module.
  127. *
  128. * The paths returned by this method paths will be searched by the load method when merging
  129. * configuration files, meaning the configuration files will cascade in this order.
  130. *
  131. * By default, the base configuration directory will be searched first, followed by the configuration
  132. * directory for the active module. Next, any environment specific configuration directories
  133. * will be searched.
  134. *
  135. * @param string $module
  136. * @param string $file
  137. * @return array
  138. */
  139. private static function paths($module, $file)
  140. {
  141. $paths = array(CONFIG_PATH, Module::path($module).'config/');
  142. if (isset($_SERVER['LARAVEL_ENV']))
  143. {
  144. $paths[] = Module::path($module).'/config/'.$_SERVER['LARAVEL_ENV'].'/';
  145. }
  146. return $paths;
  147. }
  148. }