* @copyright Copyright (c) 2008-9 TypeOneError Studios (http://www.typeoneerror.com) * @license MIT ~ http://www.opensource.org/licenses/mit-license.php * @version $Id$ * @link http://typeoneerror.com/asra * @category Asra * @package Db */ /** * MySQL database connection configuration for multiple server use. * * @author Ben Borowski * @copyright Copyright (c) 2008-9 TypeOneError Studios (http://www.typeoneerror.com) * @license MIT ~ http://www.opensource.org/licenses/mit-license.php * @link http://typeoneerror.com/asra * @category Asra * @package Db */ class Asra_Db_Connect { /** * Store the configuration. * * @var object */ public $config; /** * The connection that is finally used. * Some defaults are defined here. * * @var array */ public $connection = array( "database.adapter" => "mysqli", "database.params.host" => "localhost", "database.params.dbname" => "database", "database.params.username" => "root", "database.params.password" => "root", ); /** * Host name. * * @var string */ public $host; /** * Constructor. * * @param array $config User defined configuration. * @param string $host Override host. * @return void */ public function __construct($config, $host = null) { //Asra_Utils_Arrays::print_array($config); // -- how is it config'ed $this->config = $config; // -- where is it deployed if ($host) { $this->host = $host; } else if (isset($_SERVER['SERVER_NAME'])) { // @codeCoverageIgnoreStart $this->host = $_SERVER['SERVER_NAME']; } // @codeCoverageIgnoreEnd // -- add the default connections if (isset($this->config->default)) { $this->__addConnectionParams($this->config['default']); unset($this->config->default); } // -- additional params based on server if (isset($this->config['$this->host'])) { $this->__addConnectionParams($this->config['$this->host']); unset($this->config['$this->host']); } // -- default params already have been set $configs = $this->config; // -- extending if (count($configs)) { foreach ($configs as $var => $val) { if ($this->__hostMatch($var, $this->host)) { $this->__addConnectionParams($configs['$var']); } $bits = explode(':', $var); if (count($bits) > 1) { $section = trim($bits[1]); $next = trim($bits[0]); if ($this->__hostMatch($next, $this->host)) { $this->__addConnectionParams($configs[$var]); } } } } //Arrays::print_array($this->connection); } /** * Incrementally add connection parameters. * * @param array $params Connection parameters * @return void */ private function __addConnectionParams($params) { foreach ($params as $var => $val) { if (!$this->connection) $this->connection = array(); $this->connection[$var] = $val; } } /** * Return the database name. * * @return string */ public function getDb() { return $this->connection['database.params.dbname']; } /** * Matches host names matches server 'typeoneerror.com' and * 'www.typeoneerror.com' for a config called 'typeoneerror.com' * 'dev.typeoneerror has to be explicitly defined though (no * wildcarded subdomain support as of yet). * * @param string $check The config setting to check * @param string $host See if the $check matches the host * @return bool True if the hosts match, otherwise, false. */ private function __hostMatch($check, $host) { return ($check == $host || "www." . $check == $host); } }