config.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php namespace System;
  2. class Config {
  3. /**
  4. * All of the loaded configuration items.
  5. *
  6. * The configuration item arrays are keyed by module and file.
  7. *
  8. * @var array
  9. */
  10. public static $items = array();
  11. /**
  12. * Determine if a configuration file or item exists.
  13. *
  14. * @param string $key
  15. * @return bool
  16. */
  17. public static function has($key)
  18. {
  19. return ! is_null(static::get($key));
  20. }
  21. /**
  22. * Get a configuration item.
  23. *
  24. * Configuration items are retrieved using "dot" notation. So, asking for the
  25. * "application.timezone" configuration item would return the "timezone" option
  26. * from the "application" configuration file.
  27. *
  28. * If the name of a configuration file is passed without specifying an item, the
  29. * entire configuration array will be returned.
  30. *
  31. * @param string $key
  32. * @param string $default
  33. * @return array
  34. */
  35. public static function get($key, $default = null)
  36. {
  37. list($module, $file, $key) = static::parse($key);
  38. static::load($module, $file);
  39. if (is_null($key)) return static::$items[$module][$file];
  40. return Arr::get(static::$items[$module][$file], $key, $default);
  41. }
  42. /**
  43. * Set a configuration item.
  44. *
  45. * If a configuration item is not specified, the entire configuration array will be set.
  46. *
  47. * @param string $key
  48. * @param mixed $value
  49. * @return void
  50. */
  51. public static function set($key, $value)
  52. {
  53. list($module, $file, $key) = static::parse($key);
  54. static::load($module, $file);
  55. if (is_null($key))
  56. {
  57. static::$items[$module][$file] = $value;
  58. }
  59. else
  60. {
  61. Arr::set(static::$items[$module][$file], $key, $value);
  62. }
  63. }
  64. /**
  65. * Parse a configuration key into its module, file, and key segments.
  66. *
  67. * @param string $key
  68. * @return array
  69. */
  70. private static function parse($key)
  71. {
  72. $module = (strpos($key, '::') !== false) ? substr($key, 0, strpos($key, ':')) : 'application';
  73. if ($module !== 'application')
  74. {
  75. $key = substr($key, strpos($key, ':') + 2);
  76. }
  77. $key = (count($segments = explode('.', $key)) > 1) ? implode('.', array_slice($segments, 1)) : null;
  78. return array($module, $segments[0], $key);
  79. }
  80. /**
  81. * Load all of the configuration items from a file.
  82. *
  83. * @param string $file
  84. * @param string $module
  85. * @return void
  86. */
  87. private static function load($module, $file)
  88. {
  89. if (isset(static::$items[$module][$file])) return true;
  90. $path = ($module === 'application') ? CONFIG_PATH : MODULE_PATH.$module.'/config/';
  91. // Load the base configuration file. Once that is loaded, we will merge any environment
  92. // specific configuration options into the base array. This allows for the convenient
  93. // cascading of configuration options depending on the application environment.
  94. $config = (file_exists($base = $path.$file.EXT)) ? require $base : array();
  95. if (isset($_SERVER['LARAVEL_ENV']) and file_exists($path = $path.$_SERVER['LARAVEL_ENV'].'/'.$file.EXT))
  96. {
  97. $config = array_merge($config, require $path);
  98. }
  99. static::$items[$module][$file] = $config;
  100. }
  101. }