PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

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