Config.php 6.6 KB

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