N98\Magento\DbSettings::getDsn PHP Method

getDsn() public method

Get Mysql PDO DSN
public getDsn ( ) : string
return string
    public function getDsn()
    {
        $dsn = 'mysql:';
        $named = array();
        // blacklisted in prev. DSN creation: username, password, options, charset, persistent, driver_options, dbname
        if (isset($this->unixSocket)) {
            $named['unix_socket'] = $this->unixSocket;
        } else {
            $named['host'] = $this->host;
            if (isset($this->port)) {
                $named['port'] = $this->port;
            }
        }
        $options = array();
        foreach ($named as $name => $value) {
            $options[$name] = "{$name}={$value}";
        }
        return $dsn . implode(';', $options);
    }

Usage Example

示例#1
0
 /**
  * @test
  */
 public function settings()
 {
     $file = __DIR__ . '/local.xml';
     $settings = new DbSettings($file);
     $this->assertSame('', $settings->getTablePrefix());
     $this->assertSame('localhost', $settings->getHost());
     $this->assertNull($settings->getPort());
     $this->assertNull($settings->getUnixSocket());
     $this->assertSame('user', $settings->getUsername());
     $this->assertSame('pass', $settings->getPassword());
     // DbSettings is more strict here, only using known DSN settings, see @link http://php.net/ref.pdo-mysql.connection
     // minus those settings that are black-listed: dbname, charset
     // "mysql:host=localhost;initStatements=SET NAMES utf8;model=mysql4;type=pdo_mysql;pdoType=;active=1;prefix="
     $this->assertEquals('mysql:host=localhost', $settings->getDsn());
 }