LazyRecord\DSN\DSNParser::parse PHP Method

parse() public method

public parse ( $dsn )
    public function parse($dsn)
    {
        if (preg_match('/^(\\w+):/', $dsn, $matches)) {
            $driver = $matches[1];
        } else {
            throw new Exception("Invalid DSN string: {$dsn}");
        }
        $reststr = preg_replace('/^\\w+:/', '', $dsn);
        $attributes = [];
        $arguments = [];
        $parts = preg_split('/[ ;]/', $reststr);
        foreach ($parts as $part) {
            if (strpos($part, '=') === false) {
                $arguments[] = $part;
            } else {
                list($key, $val) = explode('=', $part);
                $attributes[trim($key)] = trim($val);
            }
        }
        return new DSN($driver, $attributes, $arguments, $dsn);
    }

Usage Example

 public function execute()
 {
     $configLoader = $this->getConfigLoader(true);
     $dsId = $this->getCurrentDataSourceId();
     $ds = $configLoader->getDataSource($dsId);
     $dsnParser = new DSNParser();
     $dsn = $dsnParser->parse($ds['dsn']);
     $dbName = $dsn->getAttribute('dbname');
     $dsn->removeAttribute('dbname');
     $this->logger->debug("Connection DSN: " . $dsn);
     $pdo = new PDO($dsn, @$ds['user'], @$ds['pass'], @$ds['connection_options']);
     $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $q = new DropDatabaseQuery($dbName);
     $q->ifExists();
     // Create query Driver object
     $queryDriver = PDODriverFactory::create($pdo);
     $sql = $q->toSql($queryDriver, new ArgumentArray());
     $this->logger->info($sql);
     if ($pdo->query($sql) === false) {
         list($statusCode, $errorCode, $message) = $pdo->errorInfo();
         $this->logger->error("{$statusCode}:{$errorCode} {$message}");
         return false;
     }
     $this->logger->info('Database dropped successfully.');
 }
All Usage Examples Of LazyRecord\DSN\DSNParser::parse
DSNParser