PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Tool/Framework/System/Provider/Config.php

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