config.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php namespace Laravel;
  2. class Config {
  3. /**
  4. * All of the loaded configuration items.
  5. *
  6. * The configuration arrays are keyed by their owning file name.
  7. *
  8. * @var array
  9. */
  10. protected static $items = array();
  11. /**
  12. * The paths to the configuration files.
  13. *
  14. * @var array
  15. */
  16. protected static $paths = array();
  17. /**
  18. * Set the paths in which the configuration files are located.
  19. *
  20. * @param array $paths
  21. * @return void
  22. */
  23. public static function paths($paths)
  24. {
  25. static::$paths = $paths;
  26. }
  27. /**
  28. * Determine if a configuration item or file exists.
  29. *
  30. * @param string $key
  31. * @return bool
  32. */
  33. public static function has($key)
  34. {
  35. return ! is_null(static::get($key));
  36. }
  37. /**
  38. * Get a configuration item.
  39. *
  40. * @param string $key
  41. * @param string $default
  42. * @return array
  43. */
  44. public static function get($key, $default = null)
  45. {
  46. list($file, $key) = static::parse($key);
  47. if ( ! static::load($file))
  48. {
  49. return ($default instanceof \Closure) ? call_user_func($default) : $default;
  50. }
  51. if (is_null($key)) return static::$items[$file];
  52. return Arr::get(static::$items[$file], $key, $default);
  53. }
  54. /**
  55. * Set a configuration item.
  56. *
  57. * @param string $key
  58. * @param mixed $value
  59. * @return void
  60. */
  61. public static function set($key, $value)
  62. {
  63. list($file, $key) = static::parse($key);
  64. static::load($file);
  65. (is_null($key)) ? Arr::set(static::$items, $file, $value) : Arr::set(static::$items[$file], $key, $value);
  66. }
  67. /**
  68. * Parse a configuration key and return its file and key segments.
  69. *
  70. * @param string $key
  71. * @return array
  72. */
  73. protected static function parse($key)
  74. {
  75. $segments = explode('.', $key);
  76. $key = (count($segments) > 1) ? implode('.', array_slice($segments, 1)) : null;
  77. return array($segments[0], $key);
  78. }
  79. /**
  80. * Load all of the configuration items from a module configuration file.
  81. *
  82. * @param string $file
  83. * @return bool
  84. */
  85. protected static function load($file)
  86. {
  87. if (isset(static::$items[$file])) return true;
  88. $config = array();
  89. foreach (static::$paths as $directory)
  90. {
  91. if (file_exists($path = $directory.$file.EXT))
  92. {
  93. $config = array_merge($config, require $path);
  94. }
  95. }
  96. if (count($config) > 0)
  97. {
  98. static::$items[$file] = $config;
  99. }
  100. return isset(static::$items[$file]);
  101. }
  102. }