File indexing completed on 2025-01-19 05:21:35

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Tool
0017  * @subpackage Framework
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @category   Zend
0025  * @package    Zend_Tool
0026  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0027  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0028  */
0029 class Zend_Tool_Framework_Client_Config
0030 {
0031 
0032     protected $_configFilepath = null;
0033 
0034     /**
0035      * @var Zend_Config
0036      */
0037     protected $_config = null;
0038 
0039     /**
0040      * @param array $options
0041      */
0042     public function __config($options = array())
0043     {
0044         if ($options) {
0045             $this->setOptions($options);
0046         }
0047     }
0048 
0049     /**
0050      * @param array $options
0051      */
0052     public function setOptions(Array $options)
0053     {
0054         foreach ($options as $optionName => $optionValue) {
0055             $setMethodName = 'set' . $optionName;
0056             if (method_exists($this, $setMethodName)) {
0057                 $this->{$setMethodName}($optionValue);
0058             }
0059         }
0060     }
0061 
0062     /**
0063      * @param  string $configFilepath
0064      * @return Zend_Tool_Framework_Client_Config
0065      */
0066     public function setConfigFilepath($configFilepath)
0067     {
0068         if (!file_exists($configFilepath)) {
0069             // require_once 'Zend/Tool/Framework/Client/Exception.php';
0070             throw new Zend_Tool_Framework_Client_Exception('Provided path to config ' . $configFilepath . ' does not exist');
0071         }
0072 
0073         $this->_configFilepath = $configFilepath;
0074         $this->loadConfig($configFilepath);
0075 
0076         return $this;
0077     }
0078 
0079     /**
0080      * Load the configuration from the given path.
0081      *
0082      * @param string $configFilepath
0083      */
0084     protected function loadConfig($configFilepath)
0085     {
0086         $suffix = substr($configFilepath, -4);
0087 
0088         switch ($suffix) {
0089             case '.ini':
0090                 // require_once 'Zend/Config/Ini.php';
0091                 $this->_config = new Zend_Config_Ini($configFilepath, null, array('allowModifications' => true));
0092                 break;
0093             case '.xml':
0094                 // require_once 'Zend/Config/Xml.php';
0095                 $this->_config = new Zend_Config_Xml($configFilepath, null, array('allowModifications' => true));
0096                 break;
0097             case '.php':
0098                 // require_once 'Zend/Config.php';
0099                 $this->_config = new Zend_Config(include $configFilepath, true);
0100                 break;
0101             default:
0102                 // require_once 'Zend/Tool/Framework/Client/Exception.php';
0103                 throw new Zend_Tool_Framework_Client_Exception('Unknown config file type '
0104                     . $suffix . ' at location ' . $configFilepath
0105                     );
0106         }
0107     }
0108 
0109     /**
0110      * Return the filepath of the configuration.
0111      *
0112      * @return string
0113      */
0114     public function getConfigFilepath()
0115     {
0116         return $this->_configFilepath;
0117     }
0118 
0119     /**
0120      * Get a configuration value.
0121      *
0122      * @param string $name
0123      * @param string $defaultValue
0124      * @return mixed
0125      */
0126     public function get($name, $defaultValue=null)
0127     {
0128         return $this->getConfigInstance()->get($name, $defaultValue);
0129     }
0130 
0131     /**
0132      * Get a configuration value
0133      *
0134      * @param string $name
0135      * @return mixed
0136      */
0137     public function __get($name)
0138     {
0139         return $this->getConfigInstance()->{$name};
0140     }
0141 
0142     /**
0143      * Check if a configuration value isset.
0144      *
0145      * @param  string $name
0146      * @return boolean
0147      */
0148     public function __isset($name)
0149     {
0150         if($this->exists() == false) {
0151             return false;
0152         }
0153         return isset($this->getConfigInstance()->{$name});
0154     }
0155 
0156     /**
0157      * @param string $name
0158      */
0159     public function __unset($name)
0160     {
0161         unset($this->getConfigInstance()->$name);
0162     }
0163 
0164     /**
0165      * @param string $name
0166      * @param mixed $value
0167      */
0168     public function __set($name, $value)
0169     {
0170         return $this->getConfigInstance()->$name = $value;
0171     }
0172 
0173     /**
0174      * Check if the User profile has a configuration.
0175      *
0176      * @return bool
0177      */
0178     public function exists()
0179     {
0180         return ($this->_config!==null);
0181     }
0182 
0183     /**
0184      * @throws Zend_Tool_Framework_Client_Exception
0185      * @return Zend_Config
0186      */
0187     public function getConfigInstance()
0188     {
0189         if(!$this->exists()) {
0190             // require_once "Zend/Tool/Framework/Client/Exception.php";
0191             throw new Zend_Tool_Framework_Client_Exception("Client has no persistent configuration.");
0192         }
0193 
0194         return $this->_config;
0195     }
0196 
0197     /**
0198      * Save changes to the configuration into persistence.
0199      *
0200      * @return bool
0201      */
0202     public function save()
0203     {
0204         if($this->exists()) {
0205             $writer = $this->getConfigWriter();
0206             $writer->write($this->getConfigFilepath(), $this->getConfigInstance(), true);
0207             $this->loadConfig($this->getConfigFilepath());
0208 
0209             return true;
0210         }
0211         return false;
0212     }
0213 
0214     /**
0215      * Get the config writer that corresponds to the current config file type.
0216      *
0217      * @return Zend_Config_Writer_FileAbstract
0218      */
0219     protected function getConfigWriter()
0220     {
0221         $suffix = substr($this->getConfigFilepath(), -4);
0222         switch($suffix) {
0223             case '.ini':
0224                 // require_once "Zend/Config/Writer/Ini.php";
0225                 $writer = new Zend_Config_Writer_Ini();
0226                 $writer->setRenderWithoutSections();
0227                 break;
0228             case '.xml':
0229                 // require_once "Zend/Config/Writer/Xml.php";
0230                 $writer = new Zend_Config_Writer_Xml();
0231                 break;
0232             case '.php':
0233                 // require_once "Zend/Config/Writer/Array.php";
0234                 $writer = new Zend_Config_Writer_Array();
0235                 break;
0236             default:
0237                 // require_once 'Zend/Tool/Framework/Client/Exception.php';
0238                 throw new Zend_Tool_Framework_Client_Exception('Unknown config file type '
0239                     . $suffix . ' at location ' . $this->getConfigFilepath()
0240                     );
0241         }
0242         return $writer;
0243     }
0244 }