config.php 2.6 KB

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