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

/system/classes/Kohana/Config.php

https://bitbucket.org/chrispiechowicz/zepto
PHP | 192 lines | 92 code | 25 blank | 75 comment | 12 complexity | ca89bdd96c43dcb1b95aec773304b097 MD5 | raw file
Possible License(s): LGPL-2.1, MIT, 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. // Array of config groups
  22. protected $_groups = array();
  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 = array();
  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 = array();
  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. } // End Kohana_Config