Skip to content

Commit

Permalink
develop
Browse files Browse the repository at this point in the history
  • Loading branch information
turboboy88 committed Apr 27, 2018
1 parent d5459f5 commit 85e4b9d
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions pkg/mongodb/MongodbConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,58 +55,44 @@ public function createContext()
public static function parseDsn($dsn)
{
$parsedUrl = parse_url($dsn);

if (false === $parsedUrl) {
throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn));
}

if (empty($parsedUrl['scheme'])) {
throw new \LogicException('Schema is empty');
}

$supported = [
'mongodb' => true,
];

if (false == isset($parsedUrl['scheme'])) {
throw new \LogicException(sprintf(
'The given DSN schema "%s" is not supported. There are supported schemes: "%s".',
$parsedUrl['scheme'],
implode('", "', array_keys($supported))
));
}

if ($parsedUrl['scheme'].':' === $dsn) {
if ('mongodb:' === $dsn) {
return [
'uri' => $parsedUrl['scheme'].'://127.0.0.1/',
'uri' => 'mongodb://127.0.0.1/',
];
}

$config['uri'] = $parsedUrl['scheme'].'://'.$parsedUrl['host'];

$config['uri'] = $dsn;
if (isset($parsedUrl['path']) && '/' !== $parsedUrl['path']) {
$pathArr = explode('/', $parsedUrl['path']);

$pathParts = explode('/', $parsedUrl['path']);
//DB name
if ($pathArr[1]) {
$config['dbname'] = $pathArr[1];
$config['uri'] .= '/'.$pathArr[1];
}

//Collection name
if ($pathArr[2]) {
$config['collection_name'] = $pathArr[2];
if ($pathParts[1]) {
$config['dbname'] = $pathParts[1];
}
}

if (isset($parsedUrl['query'])) {
$config['uri'] .= '?'.$parsedUrl['query'];
//get polling_interval value
if (false !== strpos($parsedUrl['query'], 'polling_interval')) {
preg_match_all('/polling_interval=([0-9]+)/', $parsedUrl['query'], $matches);
if (isset($matches[1][0])) {
$config['polling_interval'] = $matches[1][0];
}
$queryParts = null;
parse_str($parsedUrl['query'], $queryParts);
//get enqueue attributes values
if (!empty($queryParts['polling_interval'])) {
$config['polling_interval'] = $queryParts['polling_interval'];
}
if (!empty($queryParts['enqueue_collection'])) {
$config['collection_name'] = $queryParts['enqueue_collection'];
}
}

Expand Down

0 comments on commit 85e4b9d

Please sign in to comment.