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
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
23 * Reads from and writes to PHP configuration files and provides access to
24 * the settings they contain.
28 * @author Phergie Development Team <team@phergie.org>
29 * @license http://phergie.org/license New BSD License
30 * @link http://pear.phergie.org/package/Phergie
32 class Phergie_Config implements ArrayAccess
35 * Mapping of configuration file paths to an array of names of settings
40 protected $files = array();
43 * Mapping of setting names to their current corresponding values
47 protected $settings = array();
50 * Includes a specified PHP configuration file and incorporates its
51 * return value (which should be an associative array) into the current
52 * configuration settings.
54 * @param string $file Path to the file to read
56 * @return Phergie_Config Provides a fluent interface
57 * @throws Phergie_Config_Exception
59 public function read($file)
61 if (!(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'
62 && file_exists($file))
63 && !is_executable($file)
65 throw new Phergie_Config_Exception(
66 'Path "' . $file . '" does not reference an executable file',
67 Phergie_Config_Exception::ERR_FILE_NOT_EXECUTABLE
71 $settings = include $file;
72 if (!is_array($settings)) {
73 throw new Phergie_Config_Exception(
74 'File "' . $file . '" does not return an array',
75 Phergie_Config_Exception::ERR_ARRAY_NOT_RETURNED
79 $this->files[$file] = array_keys($settings);
80 $this->settings += $settings;
86 * Merges an associative array of configuration setting values into the
87 * current configuration settings.
89 * @param array $settings Associative array of configuration setting
90 * values keyed by setting name
92 * @return Phergie_Config Provides a fluent interface
94 public function readArray(array $settings)
96 $this->settings += $settings;
102 * Writes the values of the current configuration settings back to their
105 * @return Phergie_Config Provides a fluent interface
107 public function write()
109 foreach ($this->files as $file => &$settings) {
111 foreach ($settings as $setting) {
112 $values[$setting] = $this->settings[$setting];
114 $source = '<?php' . PHP_EOL . PHP_EOL .
115 'return ' . var_export($value, true) . ';';
116 file_put_contents($file, $source);
121 * Checks to see if a configuration setting is assigned a value.
123 * @param string $offset Configuration setting name
125 * @return bool TRUE if the setting has a value, FALSE otherwise
126 * @see ArrayAccess::offsetExists()
128 public function offsetExists($offset)
130 return isset($this->settings[$offset]);
134 * Returns the value of a configuration setting.
136 * @param string $offset Configuration setting name
138 * @return mixed Configuration setting value or NULL if it is not
140 * @see ArrayAccess::offsetGet()
142 public function offsetGet($offset)
144 if (isset($this->settings[$offset])) {
145 $value = &$this->settings[$offset];
154 * Sets the value of a configuration setting.
156 * @param string $offset Configuration setting name
157 * @param mixed $value New setting value
160 * @see ArrayAccess::offsetSet()
162 public function offsetSet($offset, $value)
164 $this->settings[$offset] = $value;
168 * Removes the value set for a configuration setting.
170 * @param string $offset Configuration setting name
173 * @see ArrayAccess::offsetUnset()
175 public function offsetUnset($offset)
177 unset($this->settings[$offset]);
179 foreach ($this->files as $file => $settings) {
180 $key = array_search($offset, $settings);
181 if ($key !== false) {
182 unset($this->files[$file][$key]);