]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Config.php
Merged in changes to Phergie
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Config.php
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 /**
23  * Reads from and writes to PHP configuration files and provides access to
24  * the settings they contain.
25  *
26  * @category Phergie
27  * @package  Phergie
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
31  */
32 class Phergie_Config implements ArrayAccess
33 {
34     /**
35      * Mapping of configuration file paths to an array of names of settings
36      * they contain
37      *
38      * @var array
39      */
40     protected $files = array();
41
42     /**
43      * Mapping of setting names to their current corresponding values
44      *
45      * @var array
46      */
47     protected $settings = array();
48
49     /**
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.
53      *
54      * @param string $file Path to the file to read
55      *
56      * @return Phergie_Config Provides a fluent interface
57      * @throws Phergie_Config_Exception
58      */
59     public function read($file)
60     {
61         if (!(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'
62             && file_exists($file))
63             && !is_executable($file)
64         ) {
65             throw new Phergie_Config_Exception(
66                 'Path "' . $file . '" does not reference an executable file',
67                 Phergie_Config_Exception::ERR_FILE_NOT_EXECUTABLE
68             );
69         }
70
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
76             );
77         }
78
79         $this->files[$file] = array_keys($settings);
80         $this->settings += $settings;
81
82         return $this;
83     }
84
85     /**
86      * Merges an associative array of configuration setting values into the
87      * current configuration settings.
88      *
89      * @param array $settings Associative array of configuration setting
90      *        values keyed by setting name
91      *
92      * @return Phergie_Config Provides a fluent interface
93      */
94     public function readArray(array $settings)
95     {
96         $this->settings += $settings;
97
98         return $this;
99     }
100
101     /**
102      * Writes the values of the current configuration settings back to their
103      * originating files.
104      *
105      * @return Phergie_Config Provides a fluent interface
106      */
107     public function write()
108     {
109         foreach ($this->files as $file => &$settings) {
110             $values = array();
111             foreach ($settings as $setting) {
112                 $values[$setting] = $this->settings[$setting];
113             }
114             $source = '<?php' . PHP_EOL . PHP_EOL .
115                 'return ' . var_export($value, true) . ';';
116             file_put_contents($file, $source);
117         }
118     }
119
120     /**
121      * Checks to see if a configuration setting is assigned a value.
122      *
123      * @param string $offset Configuration setting name
124      *
125      * @return bool TRUE if the setting has a value, FALSE otherwise
126      * @see ArrayAccess::offsetExists()
127      */
128     public function offsetExists($offset)
129     {
130         return isset($this->settings[$offset]);
131     }
132
133     /**
134      * Returns the value of a configuration setting.
135      *
136      * @param string $offset Configuration setting name
137      *
138      * @return mixed Configuration setting value or NULL if it is not
139      *         assigned a value
140      * @see ArrayAccess::offsetGet()
141      */
142     public function offsetGet($offset)
143     {
144         if (isset($this->settings[$offset])) {
145             $value = &$this->settings[$offset];
146         } else {
147             $value = null;
148         }
149
150         return $value;
151     }
152
153     /**
154      * Sets the value of a configuration setting.
155      *
156      * @param string $offset Configuration setting name
157      * @param mixed  $value  New setting value
158      *
159      * @return void
160      * @see ArrayAccess::offsetSet()
161      */
162     public function offsetSet($offset, $value)
163     {
164         $this->settings[$offset] = $value;
165     }
166
167     /**
168      * Removes the value set for a configuration setting.
169      *
170      * @param string $offset Configuration setting name
171      *
172      * @return void
173      * @see ArrayAccess::offsetUnset()
174      */
175     public function offsetUnset($offset)
176     {
177         unset($this->settings[$offset]);
178
179         foreach ($this->files as $file => $settings) {
180             $key = array_search($offset, $settings);
181             if ($key !== false) {
182                 unset($this->files[$file][$key]);
183             }
184         }
185     }
186 }