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 * Writes the values of the current configuration settings back to their
89 * @return Phergie_Config Provides a fluent interface
91 public function write()
93 foreach ($this->files as $file => &$settings) {
95 foreach ($settings as $setting) {
96 $values[$setting] = $this->settings[$setting];
98 $source = '<?php' . PHP_EOL . PHP_EOL .
99 'return ' . var_export($value, true) . ';';
100 file_put_contents($file, $source);
105 * Checks to see if a configuration setting is assigned a value.
107 * @param string $offset Configuration setting name
109 * @return bool TRUE if the setting has a value, FALSE otherwise
110 * @see ArrayAccess::offsetExists()
112 public function offsetExists($offset)
114 return isset($this->settings[$offset]);
118 * Returns the value of a configuration setting.
120 * @param string $offset Configuration setting name
122 * @return mixed Configuration setting value or NULL if it is not
124 * @see ArrayAccess::offsetGet()
126 public function offsetGet($offset)
128 if (isset($this->settings[$offset])) {
129 $value = &$this->settings[$offset];
138 * Sets the value of a configuration setting.
140 * @param string $offset Configuration setting name
141 * @param mixed $value New setting value
144 * @see ArrayAccess::offsetSet()
146 public function offsetSet($offset, $value)
148 $this->settings[$offset] = $value;
152 * Removes the value set for a configuration setting.
154 * @param string $offset Configuration setting name
157 * @see ArrayAccess::offsetUnset()
159 public function offsetUnset($offset)
161 unset($this->settings[$offset]);
163 foreach ($this->files as $file => $settings) {
164 $key = array_search($offset, $settings);
165 if ($key !== false) {
166 unset($this->files[$file][$key]);