]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Config.php
f011db2365dc61cbf5afc2b99c9cb4191d076a3d
[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      * Writes the values of the current configuration settings back to their
87      * originating files.
88      *
89      * @return Phergie_Config Provides a fluent interface
90      */
91     public function write()
92     {
93         foreach ($this->files as $file => &$settings) {
94             $values = array();
95             foreach ($settings as $setting) {
96                 $values[$setting] = $this->settings[$setting];
97             }
98             $source = '<?php' . PHP_EOL . PHP_EOL .
99                 'return ' . var_export($value, true) . ';';
100             file_put_contents($file, $source);
101         }
102     }
103
104     /**
105      * Checks to see if a configuration setting is assigned a value.
106      *
107      * @param string $offset Configuration setting name
108      *
109      * @return bool TRUE if the setting has a value, FALSE otherwise
110      * @see ArrayAccess::offsetExists()
111      */
112     public function offsetExists($offset)
113     {
114         return isset($this->settings[$offset]);
115     }
116
117     /**
118      * Returns the value of a configuration setting.
119      *
120      * @param string $offset Configuration setting name
121      *
122      * @return mixed Configuration setting value or NULL if it is not
123      *         assigned a value
124      * @see ArrayAccess::offsetGet()
125      */
126     public function offsetGet($offset)
127     {
128         if (isset($this->settings[$offset])) {
129             $value = &$this->settings[$offset];
130         } else {
131             $value = null;
132         }
133
134         return $value;
135     }
136
137     /**
138      * Sets the value of a configuration setting.
139      *
140      * @param string $offset Configuration setting name
141      * @param mixed  $value  New setting value
142      *
143      * @return void
144      * @see ArrayAccess::offsetSet()
145      */
146     public function offsetSet($offset, $value)
147     {
148         $this->settings[$offset] = $value;
149     }
150
151     /**
152      * Removes the value set for a configuration setting.
153      *
154      * @param string $offset Configuration setting name
155      *
156      * @return void
157      * @see ArrayAccess::offsetUnset()
158      */
159     public function offsetUnset($offset)
160     {
161         unset($this->settings[$offset]);
162
163         foreach ($this->files as $file => $settings) {
164             $key = array_search($offset, $settings);
165             if ($key !== false) {
166                 unset($this->files[$file][$key]);
167             }
168         }
169     }
170 }