/** * Constructor. * @param null $db * @param null $config */ public function __construct($db = null, $config = null) { // Set a name if ($db !== null) { $this->db = $db; } else { $this->db = 'default'; } // Initialize a new DB object $this->db = Database_Init::instance((string) $this->db, $config); }
/** * Get sa singleton instance. * @param string $name * @param null $config * @return mixed * @throws Exception_Exido */ public static function &instance($name = 'default', $config = null) { if (!isset(self::$instances[$name])) { // Get the DB settings if ($config == null) { $config = @Exido::config('database')->{$name}; } if (!isset($config['type'])) { throw new Exception_Exido("DB instance %s doesn't supported by the system", array($name)); } // Merge a default configuration with user's configuration self::$_config = array_merge(self::$_config, $config); if (is_file(SYSPATH . 'database/driver/' . self::$_config['type'] . '/adapter.php')) { // Initialize a driver include_once 'database/driver/' . self::$_config['type'] . '/adapter.php'; $driver = 'Database_Driver_' . ucfirst(self::$_config['type']) . '_Adapter'; self::$instances[$name] = new $driver(self::$_config); } else { throw new Exception_Exido("Driver :driver doesn't exist", array('database/driver/' . self::$_config['type'] . '/adapter.php')); } } return self::$instances[$name]; }