Location: PHPKode > scripts > DB Version Manager > lib/config_manager.php
<?php
/**
* DB version manager
*
* Copyright (c) 2011 Przemek Berezowski (hide@address.com)
* All rights reserved.
*
*
* @category	  	 Library
* @package  	 DBVersionManager
* @copyright     Copyright (c) 2011 Przemek Berezowski (hide@address.com)
* @version       0.9
* @license       New BSD License
*/

require_once('tools.php');

/**
 * Class ConfigManager
 * Manages config values stored in xml file
 * @author pberezowski
 *
 */
class ConfigManager {
	
	/**
	 * dsn for PDO
	 * @var string
	 */
	private $dsn;

	/**
	 * Database user
	 * @var string
	 */
	private $dbUser;
	
	/**
	* Database pass for $dbUser
	* @var string
	*/
	private $dbPass;
		
	/**
	 * Path to directory where sql scripts resides
	 * @var string
	 */
	private $sqlPath;
		
	/**
	 * Name of the config profile to perform db update operations
	 * @var string
	 */
	private $name;
	
	/**
	 * 
	 * Whether sql use transactions
	 * @var bool
	 */
	private $sqlUseTransactions; 
	
	/**
	 * 
	 * Construct
	 * @param string $profile name of profile
	 */
	public function __construct($profile) {
		$this->readConfig($profile);
	}
	
	/**
	 * dsn getter
	 * @return string
	 */
	public function getDsn() {
		return $this->dsn;
	}
	
	/**
	* db user getter
	* @return string
	*/
	public function getDbUser() {
		return $this->dbUser;
	}
	
	/**
	* db pass getter
	* @return string
	*/
	public function getDbPass() {
		return $this->dbPass;
	}
	
	/**
	* sqlPath getter
	* @return string
	*/
	public function getSqlPath() {
		return $this->sqlPath;
	}
	
	/**
	* name getter
	* @return string
	*/
	public function getName() {
		return $this->name;
	}
		
	/**
	 * sqlUseTransactions getter
	 * @return bool;
	 */
	public function getSqlUseTransactions() {
		return $this->sqlUseTransactions;
	}
	
	/**
	 * Reads the config and setup variables
	 * @param string $profile
	 * @throws UpdaterException when there is a problem with config
	 */
	private function readConfig($profile) {
		
		//load the config
		$xml = Tools::readXml(Tools::getProjectPath().'/conf/configure.xml');
		
		//find config for current profile
		$xPath = "//profile[@name='".$profile."']";
		$currentConfig =  $xml->xpath($xPath); 
		
		if (count($currentConfig) == 0) {
			Throw new UpdaterException('There is no entry in config for profile '.$profile);
		}
		
		if (count($currentConfig) > 1) {
			Throw new UpdaterException('There are multiple entries in config for profile '.$profile);
		}
		
		if ( !($currentConfig[0] instanceof SimpleXMLElement)) {
			Throw new UpdaterException('Config for profile '.$profile.' is invalid');
		}
		$this->name = $profile;
		$this->setup($currentConfig[0]);
		
	}
	
	private function setup(SimpleXMLElement $node) {
		
		$this->dsn = (string)$node->dsn;
		$this->dbUser = (string)$node->dbUser;
		$this->dbPass = (string)$node->dbPass;
		$this->sqlPath = (string)$node->sqlPath;
		$this->sqlUseTransactions = (bool)$node->sqlUseTransactions;
		
		if(empty($this->dsn)) {
			Throw new UpdaterException('Empty dsn');
		}
		if (empty($this->dbUser)) {
			Throw new UpdaterException('Empty db user');
		}
		if (empty($this->sqlPath)) {
			Throw new UpdaterException('Empty sqlPath');
		}
	}

}
Return current item: DB Version Manager