config.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. public $items = array();
  11. /**
  12. * Determine if a configuration item or file exists.
  13. *
  14. * @param string $key
  15. * @return bool
  16. */
  17. public function has($key)
  18. {
  19. return ! is_null($this->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 function get($key, $default = null)
  36. {
  37. list($file, $key) = $this->parse($key);
  38. if ( ! $this->load($file))
  39. {
  40. return ($default instanceof \Closure) ? call_user_func($default) : $default;
  41. }
  42. if (is_null($key)) return $this->items[$file];
  43. return Arr::get($this->items[$file], $key, $default);
  44. }
  45. /**
  46. * Set a configuration item.
  47. *
  48. * Like the get method, "dot" notation is used to set items, and setting items
  49. * at any depth in the configuration array is supported.
  50. *
  51. * If a specific configuration item is not specified, the entire configuration
  52. * array will be replaced with the given value.
  53. *
  54. * @param string $key
  55. * @param mixed $value
  56. * @return void
  57. */
  58. public function set($key, $value)
  59. {
  60. list($file, $key) = $this->parse($key);
  61. $this->load($file);
  62. (is_null($key)) ? Arr::set($this->items, $file, $value) : Arr::set($this->items[$file], $key, $value);
  63. }
  64. /**
  65. * Parse a configuration key and return its file and key segments.
  66. *
  67. * Configuration keys follow a {file}.{key} convention.
  68. *
  69. * @param string $key
  70. * @return array
  71. */
  72. private function parse($key)
  73. {
  74. $segments = explode('.', $key);
  75. return array($segments[0], (count($segments) > 1) ? implode('.', array_slice($segments, 1)) : null);
  76. }
  77. /**
  78. * Load all of the configuration items from a module configuration file.
  79. *
  80. * If the configuration file has already been loaded, it will not be loaded again.
  81. *
  82. * @param string $file
  83. * @return bool
  84. */
  85. private function load($file)
  86. {
  87. if (isset($this->items[$file])) return true;
  88. $config = array();
  89. foreach ($this->paths() as $directory)
  90. {
  91. $config = (file_exists($path = $directory.$file.EXT)) ? array_merge($config, require $path) : $config;
  92. }
  93. if (count($config) > 0)
  94. {
  95. $this->items[$file] = $config;
  96. }
  97. return isset($this->items[$file]);
  98. }
  99. /**
  100. * Get the path hierarchy for a given configuration file and module.
  101. *
  102. * The paths returned by this method paths will be searched by the load method when merging
  103. * configuration files, meaning the configuration files will cascade in this order.
  104. *
  105. * The system configuration directory will be searched first, followed by the application
  106. * directory, and finally the environment directory.
  107. *
  108. * @return array
  109. */
  110. private function paths()
  111. {
  112. $paths = array(SYS_CONFIG_PATH, CONFIG_PATH);
  113. if (isset($_SERVER['LARAVEL_ENV']))
  114. {
  115. $paths[] = CONFIG_PATH.$_SERVER['LARAVEL_ENV'].'/';
  116. }
  117. return $paths;
  118. }
  119. }