|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Tool |
|
17 * @subpackage Framework |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: Config.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @category Zend |
|
25 * @package Zend_Tool |
|
26 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
27 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
28 */ |
|
29 class Zend_Tool_Framework_Client_Config |
|
30 { |
|
31 |
|
32 protected $_configFilepath = null; |
|
33 |
|
34 /** |
|
35 * @var Zend_Config |
|
36 */ |
|
37 protected $_config = null; |
|
38 |
|
39 /** |
|
40 * @param array $options |
|
41 */ |
|
42 public function __config($options = array()) |
|
43 { |
|
44 if ($options) { |
|
45 $this->setOptions($options); |
|
46 } |
|
47 } |
|
48 |
|
49 /** |
|
50 * @param array $options |
|
51 */ |
|
52 public function setOptions(Array $options) |
|
53 { |
|
54 foreach ($options as $optionName => $optionValue) { |
|
55 $setMethodName = 'set' . $optionName; |
|
56 if (method_exists($this, $setMethodName)) { |
|
57 $this->{$setMethodName}($optionValue); |
|
58 } |
|
59 } |
|
60 } |
|
61 |
|
62 /** |
|
63 * @param string $configFilepath |
|
64 * @return Zend_Tool_Framework_Client_Config |
|
65 */ |
|
66 public function setConfigFilepath($configFilepath) |
|
67 { |
|
68 if (!file_exists($configFilepath)) { |
|
69 require_once 'Zend/Tool/Framework/Client/Exception.php'; |
|
70 throw new Zend_Tool_Framework_Client_Exception('Provided path to config ' . $configFilepath . ' does not exist'); |
|
71 } |
|
72 |
|
73 $this->_configFilepath = $configFilepath; |
|
74 $this->loadConfig($configFilepath); |
|
75 |
|
76 return $this; |
|
77 } |
|
78 |
|
79 /** |
|
80 * Load the configuration from the given path. |
|
81 * |
|
82 * @param string $configFilepath |
|
83 */ |
|
84 protected function loadConfig($configFilepath) |
|
85 { |
|
86 $suffix = substr($configFilepath, -4); |
|
87 |
|
88 switch ($suffix) { |
|
89 case '.ini': |
|
90 require_once 'Zend/Config/Ini.php'; |
|
91 $this->_config = new Zend_Config_Ini($configFilepath, null, array('allowModifications' => true)); |
|
92 break; |
|
93 case '.xml': |
|
94 require_once 'Zend/Config/Xml.php'; |
|
95 $this->_config = new Zend_Config_Xml($configFilepath, null, array('allowModifications' => true)); |
|
96 break; |
|
97 case '.php': |
|
98 require_once 'Zend/Config.php'; |
|
99 $this->_config = new Zend_Config(include $configFilepath, true); |
|
100 break; |
|
101 default: |
|
102 require_once 'Zend/Tool/Framework/Client/Exception.php'; |
|
103 throw new Zend_Tool_Framework_Client_Exception('Unknown config file type ' |
|
104 . $suffix . ' at location ' . $configFilepath |
|
105 ); |
|
106 } |
|
107 } |
|
108 |
|
109 /** |
|
110 * Return the filepath of the configuration. |
|
111 * |
|
112 * @return string |
|
113 */ |
|
114 public function getConfigFilepath() |
|
115 { |
|
116 return $this->_configFilepath; |
|
117 } |
|
118 |
|
119 /** |
|
120 * Get a configuration value. |
|
121 * |
|
122 * @param string $name |
|
123 * @param string $defaultValue |
|
124 * @return mixed |
|
125 */ |
|
126 public function get($name, $defaultValue=null) |
|
127 { |
|
128 return $this->getConfigInstance()->get($name, $defaultValue); |
|
129 } |
|
130 |
|
131 /** |
|
132 * Get a configuration value |
|
133 * |
|
134 * @param string $name |
|
135 * @return mixed |
|
136 */ |
|
137 public function __get($name) |
|
138 { |
|
139 return $this->getConfigInstance()->{$name}; |
|
140 } |
|
141 |
|
142 /** |
|
143 * Check if a configuration value isset. |
|
144 * |
|
145 * @param string $name |
|
146 * @return boolean |
|
147 */ |
|
148 public function __isset($name) |
|
149 { |
|
150 if($this->exists() == false) { |
|
151 return false; |
|
152 } |
|
153 return isset($this->getConfigInstance()->{$name}); |
|
154 } |
|
155 |
|
156 /** |
|
157 * @param string $name |
|
158 */ |
|
159 public function __unset($name) |
|
160 { |
|
161 unset($this->getConfigInstance()->$name); |
|
162 } |
|
163 |
|
164 /** |
|
165 * @param string $name |
|
166 * @param mixed $value |
|
167 */ |
|
168 public function __set($name, $value) |
|
169 { |
|
170 return $this->getConfigInstance()->$name = $value; |
|
171 } |
|
172 |
|
173 /** |
|
174 * Check if the User profile has a configuration. |
|
175 * |
|
176 * @return bool |
|
177 */ |
|
178 public function exists() |
|
179 { |
|
180 return ($this->_config!==null); |
|
181 } |
|
182 |
|
183 /** |
|
184 * @throws Zend_Tool_Framework_Client_Exception |
|
185 * @return Zend_Config |
|
186 */ |
|
187 public function getConfigInstance() |
|
188 { |
|
189 if(!$this->exists()) { |
|
190 require_once "Zend/Tool/Framework/Client/Exception.php"; |
|
191 throw new Zend_Tool_Framework_Client_Exception("Client has no persistent configuration."); |
|
192 } |
|
193 |
|
194 return $this->_config; |
|
195 } |
|
196 |
|
197 /** |
|
198 * Save changes to the configuration into persistence. |
|
199 * |
|
200 * @return bool |
|
201 */ |
|
202 public function save() |
|
203 { |
|
204 if($this->exists()) { |
|
205 $writer = $this->getConfigWriter(); |
|
206 $writer->write($this->getConfigFilepath(), $this->getConfigInstance(), true); |
|
207 $this->loadConfig($this->getConfigFilepath()); |
|
208 |
|
209 return true; |
|
210 } |
|
211 return false; |
|
212 } |
|
213 |
|
214 /** |
|
215 * Get the config writer that corresponds to the current config file type. |
|
216 * |
|
217 * @return Zend_Config_Writer_FileAbstract |
|
218 */ |
|
219 protected function getConfigWriter() |
|
220 { |
|
221 $suffix = substr($this->getConfigFilepath(), -4); |
|
222 switch($suffix) { |
|
223 case '.ini': |
|
224 require_once "Zend/Config/Writer/Ini.php"; |
|
225 $writer = new Zend_Config_Writer_Ini(); |
|
226 $writer->setRenderWithoutSections(); |
|
227 break; |
|
228 case '.xml': |
|
229 require_once "Zend/Config/Writer/Xml.php"; |
|
230 $writer = new Zend_Config_Writer_Xml(); |
|
231 break; |
|
232 case '.php': |
|
233 require_once "Zend/Config/Writer/Array.php"; |
|
234 $writer = new Zend_Config_Writer_Array(); |
|
235 break; |
|
236 default: |
|
237 require_once 'Zend/Tool/Framework/Client/Exception.php'; |
|
238 throw new Zend_Tool_Framework_Client_Exception('Unknown config file type ' |
|
239 . $suffix . ' at location ' . $this->getConfigFilepath() |
|
240 ); |
|
241 } |
|
242 return $writer; |
|
243 } |
|
244 } |