Config.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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-2009 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-2009 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. public function __config($options = array())
  36. {
  37. if ($options) {
  38. $this->setOptions($options);
  39. }
  40. }
  41. public function setOptions(Array $options)
  42. {
  43. foreach ($options as $optionName => $optionValue) {
  44. $setMethodName = 'set' . $optionName;
  45. if (method_exists($this, $setMethodName)) {
  46. $this->{$setMethodName}($optionValue);
  47. }
  48. }
  49. }
  50. public function setConfigFilepath($configFilepath)
  51. {
  52. if (!file_exists($configFilepath)) {
  53. require_once 'Zend/Tool/Framework/Client/Exception.php';
  54. throw new Zend_Tool_Framework_Client_Exception('Provided path to config ' . $configFilepath . ' does not exist');
  55. }
  56. $this->_configFilepath = $configFilepath;
  57. $suffix = substr($configFilepath, -4);
  58. switch ($suffix) {
  59. case '.ini':
  60. require_once 'Zend/Config/Ini.php';
  61. $this->_config = new Zend_Config_Ini($configFilepath);
  62. break;
  63. case '.xml':
  64. require_once 'Zend/Config/Xml.php';
  65. $this->_config = new Zend_Config_Xml($configFilepath);
  66. break;
  67. case '.php':
  68. require_once 'Zend/Config.php';
  69. $this->_config = new Zend_Config(include $configFilepath);
  70. break;
  71. default:
  72. require_once 'Zend/Tool/Framework/Client/Exception.php';
  73. throw new Zend_Tool_Framework_Client_Exception('Unknown config file type '
  74. . $suffix . ' at location ' . $configFilepath
  75. );
  76. }
  77. return $this;
  78. }
  79. public function getConfigFilepath()
  80. {
  81. return $this->_configFilepath;
  82. }
  83. public function get($name, $defaultValue)
  84. {
  85. return $this->_config->get($name, $defaultValue);
  86. }
  87. public function __get($name)
  88. {
  89. return $this->_config->{$name};
  90. }
  91. }