Ticket #550: Database.php.diff
| File Database.php.diff, 4.8 kB (added by JAAulde, 9 months ago) |
|---|
-
Database.php
74 74 // Load the default group 75 75 $config = Config::item('database.default'); 76 76 } 77 elseif (is_array($config) && count($config) > 0) 78 { 79 if ( ! array_key_exists('connection', $config)) 80 { 81 $config = array('connection' => $config); 82 } 83 } 77 84 elseif (is_string($config)) 78 85 { 79 86 // The config is a DSN string … … 95 102 // Merge the default config with the passed config 96 103 $this->config = array_merge($this->config, $config); 97 104 98 // Make sure the connection is valid 99 if (strpos($this->config['connection'], '://') === FALSE) 100 throw new Kohana_Database_Exception('database.invalid_dsn', $this->config['connection']); 105 if(is_string($this->config['connection'])) 106 { 107 // Make sure the connection is valid 108 if (strpos($this->config['connection'], '://') === FALSE) 109 throw new Kohana_Database_Exception('database.invalid_dsn', $this->config['connection']); 101 110 102 // Parse the DSN, creating an array to hold the connection parameters103 $db = array104 (105 'type' => FALSE,106 'user' => FALSE,107 'pass' => FALSE,108 'host' => FALSE,109 'port' => FALSE,110 'socket' => FALSE,111 'database' => FALSE112 );111 // Parse the DSN, creating an array to hold the connection parameters 112 $db = array 113 ( 114 'type' => FALSE, 115 'user' => FALSE, 116 'pass' => FALSE, 117 'host' => FALSE, 118 'port' => FALSE, 119 'socket' => FALSE, 120 'database' => FALSE 121 ); 113 122 114 // Get the protocol and arguments115 list ($db['type'], $connection) = explode('://', $this->config['connection'], 2);123 // Get the protocol and arguments 124 list ($db['type'], $connection) = explode('://', $this->config['connection'], 2); 116 125 117 if (strpos($connection, '@') !== FALSE)118 {119 // Get the username and password120 list ($db['pass'], $connection) = explode('@', $connection, 2);121 // Check if a password is supplied122 $logindata = explode(':', $db['pass'], 2);123 $db['pass'] = (count($logindata) > 1) ? $logindata[1] : '';124 $db['user'] = $logindata[0];126 if (strpos($connection, '@') !== FALSE) 127 { 128 // Get the username and password 129 list ($db['pass'], $connection) = explode('@', $connection, 2); 130 // Check if a password is supplied 131 $logindata = explode(':', $db['pass'], 2); 132 $db['pass'] = (count($logindata) > 1) ? $logindata[1] : ''; 133 $db['user'] = $logindata[0]; 125 134 126 // Prepare for finding the database127 $connection = explode('/', $connection);135 // Prepare for finding the database 136 $connection = explode('/', $connection); 128 137 129 // Find the database name130 $db['database'] = array_pop($connection);138 // Find the database name 139 $db['database'] = array_pop($connection); 131 140 132 // Reset connection string133 $connection = implode('/', $connection);141 // Reset connection string 142 $connection = implode('/', $connection); 134 143 135 // Find the socket 136 if (preg_match('/^unix\([^)]++\)/', $connection)) 137 { 138 // This one is a little hairy: we explode based on the end of 139 // the socket, removing the 'unix(' from the connection string 140 list ($db['socket'], $connection) = explode(')', substr($connection, 5), 2); 144 // Find the socket 145 if (preg_match('/^unix\([^)]++\)/', $connection)) 146 { 147 // This one is a little hairy: we explode based on the end of 148 // the socket, removing the 'unix(' from the connection string 149 list ($db['socket'], $connection) = explode(')', substr($connection, 5), 2); 150 } 151 elseif (strpos($connection, ':') !== FALSE) 152 { 153 // Fetch the host and port name 154 list ($db['host'], $db['port']) = explode(':', $connection, 2); 155 } 156 else 157 { 158 $db['host'] = $connection; 159 } 141 160 } 142 elseif (strpos($connection, ':') !== FALSE)143 {144 // Fetch the host and port name145 list ($db['host'], $db['port']) = explode(':', $connection, 2);146 }147 161 else 148 162 { 149 $db['host'] = $connection; 163 // File connection 164 $connection = explode('/', $connection); 165 166 // Find database file name 167 $db['database'] = array_pop($connection); 168 169 // Find database directory name 170 $db['socket'] = implode('/', $connection).'/'; 150 171 } 151 }152 else153 {154 // File connection155 $connection = explode('/', $connection);156 172 157 // Find database file name 158 $db['database'] = array_pop($connection); 159 160 // Find database directory name 161 $db['socket'] = implode('/', $connection).'/'; 173 // Reset the connection array to the database config 174 $this->config['connection'] = $db; 162 175 } 163 164 // Reset the connection array to the database config165 $this->config['connection'] = $db;166 167 176 // Set driver name 168 177 $driver = 'Database_'.ucfirst($this->config['connection']['type']).'_Driver'; 169 178
