Spot\Adapter\AdapterAbstract::parseDSN PHP Метод

parseDSN() публичный статический Метод

Taken from PHPUnit 3.4:
public static parseDSN ( string $dsn ) : array
$dsn string Data Source Name to be parsed
Результат array an associative array with the following keys: + adapter: Database backend used in PHP (mysql, odbc etc.) + dbsyntax: Database used with regards to SQL syntax etc. + protocol: Communication protocol to use (tcp, unix etc.) + host: Host specification (hostname[:port]) + database: Database to use on the DBMS server + username: User name for login + password: Password for login
    public static function parseDSN($dsn)
    {
        if ($dsn == 'sqlite::memory:') {
            $dsn = 'sqlite://:memory:';
        }
        $parsed = array('adapter' => FALSE, 'dbsyntax' => FALSE, 'username' => FALSE, 'password' => FALSE, 'protocol' => FALSE, 'host' => FALSE, 'port' => FALSE, 'socket' => FALSE, 'database' => FALSE);
        if (is_array($dsn)) {
            $dsn = array_merge($parsed, $dsn);
            if (!$dsn['dbsyntax']) {
                $dsn['dbsyntax'] = $dsn['adapter'];
            }
            return $dsn;
        }
        // Find phptype and dbsyntax
        if (($pos = strpos($dsn, '://')) !== FALSE) {
            $str = substr($dsn, 0, $pos);
            $dsn = substr($dsn, $pos + 3);
        } else {
            if (($pos = strpos($dsn, ':')) !== FALSE) {
                $str = substr($dsn, 0, $pos);
                $dsn = substr($dsn, $pos + 1);
            } else {
                $str = $dsn;
                $dsn = NULL;
            }
        }
        // Get phptype and dbsyntax
        // $str => phptype(dbsyntax)
        if (preg_match('|^(.+?)\\((.*?)\\)$|', $str, $arr)) {
            $parsed['adapter'] = $arr[1];
            $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
        } else {
            $parsed['adapter'] = $str;
            $parsed['dbsyntax'] = $str;
        }
        if (!count($dsn)) {
            return $parsed;
        }
        // Get (if found): username and password
        // $dsn => username:password@protocol+host/database
        if (($at = strrpos((string) $dsn, '@')) !== FALSE) {
            $str = substr($dsn, 0, $at);
            $dsn = substr($dsn, $at + 1);
            if (($pos = strpos($str, ':')) !== FALSE) {
                $parsed['username'] = rawurldecode(substr($str, 0, $pos));
                $parsed['password'] = rawurldecode(substr($str, $pos + 1));
            } else {
                $parsed['username'] = rawurldecode($str);
            }
        }
        // Find protocol and host
        if (preg_match('|^([^(]+)\\((.*?)\\)/?(.*?)$|', $dsn, $match)) {
            // $dsn => proto(proto_opts)/database
            $proto = $match[1];
            $proto_opts = $match[2] ? $match[2] : FALSE;
            $dsn = $match[3];
        } else {
            // $dsn => protocol+host/database (old format)
            if (strpos($dsn, '+') !== FALSE) {
                list($proto, $dsn) = explode('+', $dsn, 2);
            }
            if (strpos($dsn, '/') !== FALSE) {
                list($proto_opts, $dsn) = explode('/', $dsn, 2);
            } else {
                $proto_opts = $dsn;
                $dsn = NULL;
            }
        }
        // process the different protocol options
        $parsed['protocol'] = !empty($proto) ? $proto : 'tcp';
        $proto_opts = rawurldecode($proto_opts);
        if ($parsed['protocol'] == 'tcp') {
            if (strpos($proto_opts, ':') !== FALSE) {
                list($parsed['host'], $parsed['port']) = explode(':', $proto_opts);
            } else {
                $parsed['host'] = $proto_opts;
            }
        } elseif ($parsed['protocol'] == 'unix') {
            $parsed['socket'] = $proto_opts;
        }
        // Get dabase if any
        // $dsn => database
        if ($dsn) {
            if (($pos = strpos($dsn, '?')) === FALSE) {
                // /database
                $parsed['database'] = rawurldecode($dsn);
            } else {
                // /database?param1=value1&param2=value2
                $parsed['database'] = rawurldecode(substr($dsn, 0, $pos));
                $dsn = substr($dsn, $pos + 1);
                if (strpos($dsn, '&') !== FALSE) {
                    $opts = explode('&', $dsn);
                } else {
                    // database?param1=value1
                    $opts = array($dsn);
                }
                foreach ($opts as $opt) {
                    list($key, $value) = explode('=', $opt);
                    if (!isset($parsed[$key])) {
                        // don't allow params overwrite
                        $parsed[$key] = rawurldecode($value);
                    }
                }
            }
        }
        return $parsed;
    }

Usage Example

Пример #1
0
 /**
  * Add database connection
  *
  * @param string $name Unique name for the connection
  * @param string $dsn DSN string for this connection
  * @param array $options Array of key => value options for adapter
  * @param boolean $defaut Use this connection as the default? The first connection added is automatically set as the default, even if this flag is false.
  * @return Spot_Adapter_Interface Spot adapter instance
  * @throws Spot_Exception
  */
 public function addConnection($name, $dsn, array $options = array(), $default = false)
 {
     // Connection name must be unique
     if (isset($this->_connections[$name])) {
         throw new Exception("Connection for '" . $name . "' already exists. Connection name must be unique.");
     }
     $dsnp = \Spot\Adapter\AdapterAbstract::parseDSN($dsn);
     $adapterClass = "\\Spot\\Adapter\\" . ucfirst($dsnp['adapter']);
     $adapter = new $adapterClass($dsn, $options);
     // Set as default connection?
     if (true === $default || null === $this->_defaultConnection) {
         $this->_defaultConnection = $name;
     }
     // Store connection and return adapter instance
     $this->_connections[$name] = $adapter;
     return $adapter;
 }