PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/rxwandc/system/classes/kohana/config.php

https://bitbucket.org/i1598/caiyun_stat
PHP | 187 lines | 90 code | 24 blank | 73 comment | 12 complexity | c51c8945e10acdfce585fc5cb7f83368 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Wrapper for configuration arrays. Multiple configuration readers can be
  4. * attached to allow loading configuration from files, database, etc.
  5. *
  6. * Configuration directives cascade across config sources in the same way that
  7. * files cascade across the filesystem.
  8. *
  9. * Directives from sources high in the sources list will override ones from those
  10. * below them.
  11. *
  12. * @package Kohana
  13. * @category Configuration
  14. * @author Kohana Team
  15. * @copyright (c) 2009-2012 Kohana Team
  16. * @license http://kohanaframework.org/license
  17. */
  18. class Kohana_Config {
  19. // Configuration readers
  20. protected $_sources = array();
  21. /**
  22. * Attach a configuration reader. By default, the reader will be added as
  23. * the first used reader. However, if the reader should be used only when
  24. * all other readers fail, use `FALSE` for the second parameter.
  25. *
  26. * $config->attach($reader); // Try first
  27. * $config->attach($reader, FALSE); // Try last
  28. *
  29. * @param Kohana_Config_Source $source instance
  30. * @param boolean $first add the reader as the first used object
  31. * @return $this
  32. */
  33. public function attach(Kohana_Config_Source $source, $first = TRUE)
  34. {
  35. if ($first === TRUE)
  36. {
  37. // Place the log reader at the top of the stack
  38. array_unshift($this->_sources, $source);
  39. }
  40. else
  41. {
  42. // Place the reader at the bottom of the stack
  43. $this->_sources[] = $source;
  44. }
  45. return $this;
  46. }
  47. /**
  48. * Detach a configuration reader.
  49. *
  50. * $config->detach($reader);
  51. *
  52. * @param Kohana_Config_Source $source instance
  53. * @return $this
  54. */
  55. public function detach(Kohana_Config_Source $source)
  56. {
  57. if (($key = array_search($source, $this->_sources)) !== FALSE)
  58. {
  59. // Remove the writer
  60. unset($this->_sources[$key]);
  61. }
  62. return $this;
  63. }
  64. /**
  65. * Load a configuration group. Searches all the config sources, merging all the
  66. * directives found into a single config group. Any changes made to the config
  67. * in this group will be mirrored across all writable sources.
  68. *
  69. * $array = $config->load($name);
  70. *
  71. * See [Kohana_Config_Group] for more info
  72. *
  73. * @param string $group configuration group name
  74. * @return Kohana_Config_Group
  75. * @throws Kohana_Exception
  76. */
  77. public function load($group)
  78. {
  79. if( ! count($this->_sources))
  80. {
  81. throw new Kohana_Exception('No configuration sources attached');
  82. }
  83. if (empty($group))
  84. {
  85. throw new Kohana_Exception("Need to specify a config group");
  86. }
  87. if ( ! is_string($group))
  88. {
  89. throw new Kohana_Exception("Config group must be a string");
  90. }
  91. if (strpos($group, '.') !== FALSE)
  92. {
  93. // Split the config group and path
  94. list ($group, $path) = explode('.', $group, 2);
  95. }
  96. if(isset($this->_groups[$group]))
  97. {
  98. if (isset($path))
  99. {
  100. return Arr::path($this->_groups[$group], $path, NULL, '.');
  101. }
  102. return $this->_groups[$group];
  103. }
  104. $config = array();
  105. // We search from the "lowest" source and work our way up
  106. $sources = array_reverse($this->_sources);
  107. foreach ($sources as $source)
  108. {
  109. if ($source instanceof Kohana_Config_Reader)
  110. {
  111. if ($source_config = $source->load($group))
  112. {
  113. $config = Arr::merge($config, $source_config);
  114. }
  115. }
  116. }
  117. $this->_groups[$group] = new Config_Group($this, $group, $config);
  118. if (isset($path))
  119. {
  120. return Arr::path($config, $path, NULL, '.');
  121. }
  122. return $this->_groups[$group];
  123. }
  124. /**
  125. * Copy one configuration group to all of the other writers.
  126. *
  127. * $config->copy($name);
  128. *
  129. * @param string $group configuration group name
  130. * @return $this
  131. */
  132. public function copy($group)
  133. {
  134. // Load the configuration group
  135. $config = $this->load($group);
  136. foreach($config->as_array() as $key => $value)
  137. {
  138. $this->_write_config($group, $key, $value);
  139. }
  140. return $this;
  141. }
  142. /**
  143. * Callback used by the config group to store changes made to configuration
  144. *
  145. * @param string $group Group name
  146. * @param string $key Variable name
  147. * @param mixed $value The new value
  148. * @return Kohana_Config Chainable instance
  149. */
  150. public function _write_config($group, $key, $value)
  151. {
  152. foreach($this->_sources as $source)
  153. {
  154. if ( ! ($source instanceof Kohana_Config_Writer))
  155. {
  156. continue;
  157. }
  158. // Copy each value in the config
  159. $source->write($group, $key, $value);
  160. }
  161. return $this;
  162. }
  163. } // End Kohana_Config