Config.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Phergie
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE
  8. *
  9. * This source file is subject to the new BSD license that is bundled
  10. * with this package in the file LICENSE.
  11. * It is also available through the world-wide-web at this URL:
  12. * http://phergie.org/license
  13. *
  14. * @category Phergie
  15. * @package Phergie
  16. * @author Phergie Development Team <team@phergie.org>
  17. * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
  18. * @license http://phergie.org/license New BSD License
  19. * @link http://pear.phergie.org/package/Phergie
  20. */
  21. /**
  22. * Reads from and writes to PHP configuration files and provides access to
  23. * the settings they contain.
  24. *
  25. * @category Phergie
  26. * @package Phergie
  27. * @author Phergie Development Team <team@phergie.org>
  28. * @license http://phergie.org/license New BSD License
  29. * @link http://pear.phergie.org/package/Phergie
  30. */
  31. class Phergie_Config implements ArrayAccess
  32. {
  33. /**
  34. * Mapping of configuration file paths to an array of names of settings
  35. * they contain
  36. *
  37. * @var array
  38. */
  39. protected $files = array();
  40. /**
  41. * Mapping of setting names to their current corresponding values
  42. *
  43. * @var array
  44. */
  45. protected $settings = array();
  46. /**
  47. * Includes a specified PHP configuration file and incorporates its
  48. * return value (which should be an associative array) into the current
  49. * configuration settings.
  50. *
  51. * @param string $file Path to the file to read
  52. *
  53. * @return Phergie_Config Provides a fluent interface
  54. * @throws Phergie_Config_Exception
  55. */
  56. public function read($file)
  57. {
  58. if (!(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'
  59. && file_exists($file))
  60. && !is_executable($file)
  61. ) {
  62. throw new Phergie_Config_Exception(
  63. 'Path "' . $file . '" does not reference an executable file',
  64. Phergie_Config_Exception::ERR_FILE_NOT_EXECUTABLE
  65. );
  66. }
  67. $settings = include $file;
  68. if (!is_array($settings)) {
  69. throw new Phergie_Config_Exception(
  70. 'File "' . $file . '" does not return an array',
  71. Phergie_Config_Exception::ERR_ARRAY_NOT_RETURNED
  72. );
  73. }
  74. $this->files[$file] = array_keys($settings);
  75. $this->settings += $settings;
  76. return $this;
  77. }
  78. /**
  79. * Merges an associative array of configuration setting values into the
  80. * current configuration settings.
  81. *
  82. * @param array $settings Associative array of configuration setting
  83. * values keyed by setting name
  84. *
  85. * @return Phergie_Config Provides a fluent interface
  86. */
  87. public function readArray(array $settings)
  88. {
  89. $this->settings += $settings;
  90. return $this;
  91. }
  92. /**
  93. * Writes the values of the current configuration settings back to their
  94. * originating files.
  95. *
  96. * @return Phergie_Config Provides a fluent interface
  97. */
  98. public function write()
  99. {
  100. foreach ($this->files as $file => &$settings) {
  101. $values = array();
  102. foreach ($settings as $setting) {
  103. $values[$setting] = $this->settings[$setting];
  104. }
  105. $source = '<?php' . PHP_EOL . PHP_EOL .
  106. 'return ' . var_export($value, true) . ';';
  107. file_put_contents($file, $source);
  108. }
  109. }
  110. /**
  111. * Checks to see if a configuration setting is assigned a value.
  112. *
  113. * @param string $offset Configuration setting name
  114. *
  115. * @return bool TRUE if the setting has a value, FALSE otherwise
  116. * @see ArrayAccess::offsetExists()
  117. */
  118. public function offsetExists($offset)
  119. {
  120. return isset($this->settings[$offset]);
  121. }
  122. /**
  123. * Returns the value of a configuration setting.
  124. *
  125. * @param string $offset Configuration setting name
  126. *
  127. * @return mixed Configuration setting value or NULL if it is not
  128. * assigned a value
  129. * @see ArrayAccess::offsetGet()
  130. */
  131. public function offsetGet($offset)
  132. {
  133. if (isset($this->settings[$offset])) {
  134. $value = &$this->settings[$offset];
  135. } else {
  136. $value = null;
  137. }
  138. return $value;
  139. }
  140. /**
  141. * Sets the value of a configuration setting.
  142. *
  143. * @param string $offset Configuration setting name
  144. * @param mixed $value New setting value
  145. *
  146. * @return void
  147. * @see ArrayAccess::offsetSet()
  148. */
  149. public function offsetSet($offset, $value)
  150. {
  151. $this->settings[$offset] = $value;
  152. }
  153. /**
  154. * Removes the value set for a configuration setting.
  155. *
  156. * @param string $offset Configuration setting name
  157. *
  158. * @return void
  159. * @see ArrayAccess::offsetUnset()
  160. */
  161. public function offsetUnset($offset)
  162. {
  163. unset($this->settings[$offset]);
  164. foreach ($this->files as $file => $settings) {
  165. $key = array_search($offset, $settings);
  166. if ($key !== false) {
  167. unset($this->files[$file][$key]);
  168. }
  169. }
  170. }
  171. }