Config.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  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) Kohana Team
  16. * @license https://koseven.ga/LICENSE.md
  17. */
  18. class Kohana_Config {
  19. // Configuration readers
  20. protected $_sources = [];
  21. // Array of config groups
  22. protected $_groups = [];
  23. /**
  24. * Attach a configuration reader. By default, the reader will be added as
  25. * the first used reader. However, if the reader should be used only when
  26. * all other readers fail, use `FALSE` for the second parameter.
  27. *
  28. * $config->attach($reader); // Try first
  29. * $config->attach($reader, FALSE); // Try last
  30. *
  31. * @param Kohana_Config_Source $source instance
  32. * @param boolean $first add the reader as the first used object
  33. * @return $this
  34. */
  35. public function attach(Kohana_Config_Source $source, $first = TRUE)
  36. {
  37. if ($first === TRUE)
  38. {
  39. // Place the log reader at the top of the stack
  40. array_unshift($this->_sources, $source);
  41. }
  42. else
  43. {
  44. // Place the reader at the bottom of the stack
  45. $this->_sources[] = $source;
  46. }
  47. // Clear any cached _groups
  48. $this->_groups = [];
  49. return $this;
  50. }
  51. /**
  52. * Detach a configuration reader.
  53. *
  54. * $config->detach($reader);
  55. *
  56. * @param Kohana_Config_Source $source instance
  57. * @return $this
  58. */
  59. public function detach(Kohana_Config_Source $source)
  60. {
  61. if (($key = array_search($source, $this->_sources)) !== FALSE)
  62. {
  63. // Remove the writer
  64. unset($this->_sources[$key]);
  65. }
  66. return $this;
  67. }
  68. /**
  69. * Load a configuration group. Searches all the config sources, merging all the
  70. * directives found into a single config group. Any changes made to the config
  71. * in this group will be mirrored across all writable sources.
  72. *
  73. * $array = $config->load($name);
  74. *
  75. * See [Kohana_Config_Group] for more info
  76. *
  77. * @param string $group configuration group name
  78. * @return Kohana_Config_Group
  79. * @throws Kohana_Exception
  80. */
  81. public function load($group)
  82. {
  83. if ( ! count($this->_sources))
  84. {
  85. throw new Kohana_Exception('No configuration sources attached');
  86. }
  87. if (empty($group))
  88. {
  89. throw new Kohana_Exception("Need to specify a config group");
  90. }
  91. if ( ! is_string($group))
  92. {
  93. throw new Kohana_Exception("Config group must be a string");
  94. }
  95. if (strpos($group, '.') !== FALSE)
  96. {
  97. // Split the config group and path
  98. list($group, $path) = explode('.', $group, 2);
  99. }
  100. if (isset($this->_groups[$group]))
  101. {
  102. if (isset($path))
  103. {
  104. return Arr::path($this->_groups[$group], $path, NULL, '.');
  105. }
  106. return $this->_groups[$group];
  107. }
  108. $config = [];
  109. // We search from the "lowest" source and work our way up
  110. $sources = array_reverse($this->_sources);
  111. foreach ($sources as $source)
  112. {
  113. if ($source instanceof Kohana_Config_Reader)
  114. {
  115. if ($source_config = $source->load($group))
  116. {
  117. $config = Arr::merge($config, $source_config);
  118. }
  119. }
  120. }
  121. $this->_groups[$group] = new Config_Group($this, $group, $config);
  122. if (isset($path))
  123. {
  124. return Arr::path($config, $path, NULL, '.');
  125. }
  126. return $this->_groups[$group];
  127. }
  128. /**
  129. * Copy one configuration group to all of the other writers.
  130. *
  131. * $config->copy($name);
  132. *
  133. * @param string $group configuration group name
  134. * @return $this
  135. */
  136. public function copy($group)
  137. {
  138. // Load the configuration group
  139. $config = $this->load($group);
  140. foreach ($config->as_array() as $key => $value)
  141. {
  142. $this->_write_config($group, $key, $value);
  143. }
  144. return $this;
  145. }
  146. /**
  147. * Callback used by the config group to store changes made to configuration
  148. *
  149. * @param string $group Group name
  150. * @param string $key Variable name
  151. * @param mixed $value The new value
  152. * @return Kohana_Config Chainable instance
  153. */
  154. public function _write_config($group, $key, $value)
  155. {
  156. foreach ($this->_sources as $source)
  157. {
  158. if ( ! ($source instanceof Kohana_Config_Writer))
  159. {
  160. continue;
  161. }
  162. // Copy each value in the config
  163. $source->write($group, $key, $value);
  164. }
  165. return $this;
  166. }
  167. }