|
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 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Tool_Framework_Provider_Abstract |
|
24 */ |
|
25 require_once "Zend/Tool/Framework/Provider/Abstract.php"; |
|
26 |
|
27 /** |
|
28 * @see Zend_Config |
|
29 */ |
|
30 require_once "Zend/Config.php"; |
|
31 |
|
32 /** |
|
33 * @see Zend_Config_Writer_Ini |
|
34 */ |
|
35 require_once "Zend/Config/Writer/Ini.php"; |
|
36 |
|
37 /** |
|
38 * @see Zend_Loader |
|
39 */ |
|
40 require_once "Zend/Loader.php"; |
|
41 |
|
42 /** |
|
43 * Configuration Provider |
|
44 * |
|
45 * @category Zend |
|
46 * @package Zend_Tool |
|
47 * @package Framework |
|
48 * @uses Zend_Tool_Framework_Provider_Abstract |
|
49 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
50 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
51 * @version $Id: Config.php 23206 2010-10-21 15:49:31Z ralph $ |
|
52 */ |
|
53 class Zend_Tool_Framework_System_Provider_Config extends Zend_Tool_Framework_Provider_Abstract |
|
54 { |
|
55 /** |
|
56 * @var array |
|
57 */ |
|
58 protected $_levelCompleted = array(); |
|
59 |
|
60 /** |
|
61 * array of specialties handled by this provider |
|
62 * |
|
63 * @var array |
|
64 */ |
|
65 protected $_specialties = array('Manifest', 'Provider'); |
|
66 |
|
67 /** |
|
68 * @param string $type |
|
69 */ |
|
70 public function create() |
|
71 { |
|
72 /* @var $userConfig Zend_Tool_Framework_Client_Config */ |
|
73 $userConfig = $this->_registry->getConfig(); |
|
74 |
|
75 $resp = $this->_registry->getResponse(); |
|
76 if ($userConfig->exists()) { |
|
77 require_once "Zend/Tool/Framework/Exception.php"; |
|
78 throw new Zend_Tool_Framework_Exception( |
|
79 "A configuration already exists, cannot create a new one."); |
|
80 } |
|
81 |
|
82 $homeDirectory = $this->_detectHomeDirectory(); |
|
83 |
|
84 $writer = new Zend_Config_Writer_Ini(); |
|
85 $writer->setRenderWithoutSections(); |
|
86 $filename = $homeDirectory."/.zf.ini"; |
|
87 |
|
88 $config = array( |
|
89 'php' => array( |
|
90 'include_path' => get_include_path(), |
|
91 ), |
|
92 ); |
|
93 $writer->write($filename, new Zend_Config($config)); |
|
94 |
|
95 $resp = $this->_registry->getResponse(); |
|
96 $resp->appendContent("Successfully written Zend Tool config."); |
|
97 $resp->appendContent("It is located at: ".$filename); |
|
98 } |
|
99 |
|
100 /** |
|
101 * @return string |
|
102 */ |
|
103 protected function _detectHomeDirectory() |
|
104 { |
|
105 $envVars = array("ZF_HOME", "HOME", "HOMEPATH"); |
|
106 foreach($envVars AS $env) { |
|
107 $homeDirectory = getenv($env); |
|
108 if ($homeDirectory != false && file_exists($homeDirectory)) { |
|
109 return $homeDirectory; |
|
110 } |
|
111 } |
|
112 require_once "Zend/Tool/Framework/Exception.php"; |
|
113 throw new Zend_Tool_Framework_Exception("Cannot detect user home directory, set ZF_HOME enviroment variable."); |
|
114 } |
|
115 |
|
116 /** |
|
117 * Show Zend Tool User Configuration |
|
118 * |
|
119 * @return void |
|
120 */ |
|
121 public function show() |
|
122 { |
|
123 $userConfig = $this->_loadUserConfigIfExists(); |
|
124 $configArray = $userConfig->getConfigInstance()->toArray(); |
|
125 |
|
126 $resp = $this->_registry->getResponse(); |
|
127 |
|
128 $i = 0; |
|
129 $tree = ""; |
|
130 foreach($configArray AS $k => $v) { |
|
131 $i++; |
|
132 $tree .= $this->_printTree($k, $v, 1, count($configArray)==$i); |
|
133 } |
|
134 $resp->appendContent("User Configuration: ".$userConfig->getConfigFilepath(), array("color" => "green")); |
|
135 $resp->appendContent($tree, array("indention" => 2)); |
|
136 } |
|
137 |
|
138 /** |
|
139 * |
|
140 * @param string $key |
|
141 * @param string $value |
|
142 * @param int $level |
|
143 * @return string |
|
144 */ |
|
145 protected function _printTree($key, $value, $level=1, $isLast=false) |
|
146 { |
|
147 $this->_levelCompleted[$level] = false; |
|
148 |
|
149 $prefix = ""; |
|
150 for ($i = 1; $i < $level; $i++) { |
|
151 if ($this->_levelCompleted[$i] == true) { |
|
152 $prefix .= " "; |
|
153 } else { |
|
154 $prefix .= "| "; |
|
155 } |
|
156 } |
|
157 if ($isLast) { |
|
158 $pointer = "`-- "; |
|
159 } else { |
|
160 $pointer = "|-- "; |
|
161 } |
|
162 |
|
163 $tree = ""; |
|
164 if (is_array($value)) { |
|
165 $tree .= $prefix.$pointer.$key.PHP_EOL; |
|
166 |
|
167 if ($isLast == true) { |
|
168 $this->_levelCompleted[$level] = true; |
|
169 } |
|
170 |
|
171 $i = 0; |
|
172 foreach ($value as $k => $v) { |
|
173 $i++; |
|
174 $tree .= $this->_printTree($k, $v, $level+1, (count($value)==$i)); |
|
175 } |
|
176 } else { |
|
177 $tree .= $prefix.$pointer.$key.": ".trim($value).PHP_EOL; |
|
178 } |
|
179 |
|
180 return $tree; |
|
181 } |
|
182 |
|
183 public function enable() |
|
184 { |
|
185 $resp = $this->_registry->getResponse(); |
|
186 $resp->appendContent('Use either "zf enable config.provider" or "zf enable config.manifest".'); |
|
187 } |
|
188 |
|
189 public function disable() |
|
190 { |
|
191 $resp = $this->_registry->getResponse(); |
|
192 $resp->appendContent('Use either "zf disable config.provider" or "zf disable config.manifest".'); |
|
193 } |
|
194 |
|
195 /** |
|
196 * @param string $className |
|
197 */ |
|
198 public function enableProvider($className) |
|
199 { |
|
200 Zend_Loader::loadClass($className); |
|
201 $reflClass = new ReflectionClass($className); |
|
202 if (!in_array("Zend_Tool_Framework_Provider_Interface", $reflClass->getInterfaceNames())) { |
|
203 require_once "Zend/Tool/Framework/Exception.php"; |
|
204 throw new Zend_Tool_Framework_Exception("Given class is not a provider"); |
|
205 } |
|
206 $this->_doEnable($className); |
|
207 } |
|
208 |
|
209 protected function _doEnable($className) |
|
210 { |
|
211 |
|
212 $userConfig = $this->_loadUserConfigIfExists(); |
|
213 |
|
214 if (!isset($userConfig->basicloader)) { |
|
215 $userConfig->basicloader = array(); |
|
216 } |
|
217 if (!isset($userConfig->basicloader->classes)) { |
|
218 $userConfig->basicloader->classes = array(); |
|
219 } |
|
220 |
|
221 $providerClasses = $userConfig->basicloader->classes->toArray(); |
|
222 if (!in_array($className, $providerClasses)) { |
|
223 if (count($providerClasses)) { |
|
224 $pos = max(array_keys($providerClasses))+1; |
|
225 } else { |
|
226 $pos = 0; |
|
227 } |
|
228 $userConfig->basicloader->classes->$pos = $className; |
|
229 |
|
230 if ($userConfig->save()) { |
|
231 $this->_registry->getResponse()->appendContent( |
|
232 "Provider/Manifest '".$className."' was enabled for usage with Zend Tool.", |
|
233 array("color" => "green", "aligncenter" => true) |
|
234 ); |
|
235 } else { |
|
236 require_once "Zend/Tool/Framework/Exception.php"; |
|
237 throw new Zend_Tool_Framework_Exception( |
|
238 "Could not write user configuration to persistence." |
|
239 ); |
|
240 } |
|
241 } else { |
|
242 require_once "Zend/Tool/Framework/Exception.php"; |
|
243 throw new Zend_Tool_Framework_Exception( |
|
244 "Provider/Manifest '".$className."' is already enabled." |
|
245 ); |
|
246 } |
|
247 } |
|
248 |
|
249 /** |
|
250 * @param string $className |
|
251 */ |
|
252 public function enableManifest($className) |
|
253 { |
|
254 Zend_Loader::loadClass($className); |
|
255 $reflClass = new ReflectionClass($className); |
|
256 if (!in_array("Zend_Tool_Framework_Manifest_Interface", $reflClass->getInterfaceNames())) { |
|
257 require_once "Zend/Tool/Framework/Exception.php"; |
|
258 throw new Zend_Tool_Framework_Exception("Given class is not a manifest."); |
|
259 } |
|
260 $this->_doEnable($className); |
|
261 } |
|
262 |
|
263 /** |
|
264 * @param string $className |
|
265 */ |
|
266 public function disableManifest($className) |
|
267 { |
|
268 $this->disableProvider($className); |
|
269 } |
|
270 |
|
271 /** |
|
272 * @param string $className |
|
273 */ |
|
274 public function disableProvider($className) |
|
275 { |
|
276 $userConfig = $this->_loadUserConfigIfExists(); |
|
277 |
|
278 if (!isset($userConfig->basicloader)) { |
|
279 $userConfig->basicloader = array(); |
|
280 } |
|
281 if (!isset($userConfig->basicloader->classes)) { |
|
282 $userConfig->basicloader->classes = array(); |
|
283 } |
|
284 |
|
285 $providerClasses = $userConfig->basicloader->classes->toArray(); |
|
286 if (($key = array_search($className, $providerClasses)) !== false) { |
|
287 unset($userConfig->basicloader->classes->$key); |
|
288 |
|
289 if ($userConfig->save()) { |
|
290 $this->_registry->getResponse()->appendContent( |
|
291 "Provider/Manifest '".$className."' was disabled.", |
|
292 array("color" => "green", "aligncenter" => true) |
|
293 ); |
|
294 } else { |
|
295 require_once "Zend/Tool/Framework/Exception.php"; |
|
296 throw new Zend_Tool_Framework_Exception( |
|
297 "Could not write user configuration to persistence." |
|
298 ); |
|
299 } |
|
300 } else { |
|
301 require_once "Zend/Tool/Framework/Exception.php"; |
|
302 throw new Zend_Tool_Framework_Exception( |
|
303 "Provider/Manifest '".$className."' is not enabled." |
|
304 ); |
|
305 } |
|
306 } |
|
307 |
|
308 /** |
|
309 * @return Zend_Tool_Framework_Client_Config |
|
310 */ |
|
311 protected function _loadUserConfigIfExists() |
|
312 { |
|
313 /* @var $userConfig Zend_Tool_Framework_Client_Config */ |
|
314 $userConfig = $this->_registry->getConfig(); |
|
315 |
|
316 $resp = $this->_registry->getResponse(); |
|
317 if (!$userConfig->exists()) { |
|
318 $resp->appendContent("User has no config file.", array("aligncenter" => true, "color" => array('hiWhite', 'bgRed'))); |
|
319 } |
|
320 |
|
321 return $userConfig; |
|
322 } |
|
323 } |