Skip to content

Commit 0b07e3e

Browse files
committed
parse dsn
1 parent f455047 commit 0b07e3e

File tree

1 file changed

+59
-12
lines changed

1 file changed

+59
-12
lines changed

pkg/mongodb/MongodbConnectionFactory.php

+59-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ class MongodbConnectionFactory implements PsrConnectionFactory
1212
*/
1313
private $config;
1414

15+
/**
16+
* The config could be an array, string DSN or null. In case of null it will attempt to connect to Mongodb localhost with default credentials.
17+
*
18+
* $config = [
19+
* 'uri' => 'mongodb://127.0.0.1/' - Mongodb connection string. see http://docs.mongodb.org/manual/reference/connection-string/
20+
* 'dbname' => 'enqueue', - database name.
21+
* 'collection_name' => 'enqueue' - collection name
22+
* 'polling_interval' => '1000', - How often query for new messages (milliseconds)
23+
* ]
24+
*
25+
* or
26+
*
27+
* mongodb://127.0.0.1:27017/dbname/collection_name?polling_interval=1000
28+
*
29+
* @param array|string|null $config
30+
*/
1531
public function __construct($config = 'mongodb:')
1632
{
1733
if (empty($config)) {
@@ -24,45 +40,76 @@ public function __construct($config = 'mongodb:')
2440
}
2541
$config = array_replace([
2642
'uri' => 'mongodb://127.0.0.1/',
27-
'uriOptions' => [],
28-
'driverOptions' => [],
2943
], $config);
3044

3145
$this->config = $config;
3246
}
3347

3448
public function createContext()
3549
{
36-
$client = new Client($this->config['uri'], $this->config['uriOptions'], $this->config['driverOptions']);
50+
$client = new Client($this->config['uri']);
3751

3852
return new MongodbContext($client, $this->config);
3953
}
4054

41-
private function parseDsn($dsn)
55+
public static function parseDsn($dsn)
4256
{
43-
if (false === parse_url($dsn)) {
57+
$parsedUrl = parse_url($dsn);
58+
59+
if (false === $parsedUrl) {
4460
throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn));
4561
}
4662

47-
$schema = parse_url($dsn, PHP_URL_SCHEME);
48-
if (empty($schema)) {
63+
if (empty($parsedUrl['scheme'])) {
4964
throw new \LogicException('Schema is empty');
5065
}
5166

5267
$supported = [
5368
'mongodb' => true,
5469
];
5570

56-
if (false == isset($supported[$schema])) {
71+
if (false == isset($parsedUrl['scheme'])) {
5772
throw new \LogicException(sprintf(
5873
'The given DSN schema "%s" is not supported. There are supported schemes: "%s".',
59-
$schema,
74+
$parsedUrl['scheme'],
6075
implode('", "', array_keys($supported))
6176
));
6277
}
6378

64-
return [
65-
'uri' => $schema.':' === $dsn ? $schema.'://127.0.0.1/' : $dsn,
66-
];
79+
if ($parsedUrl['scheme'].':' === $dsn) {
80+
return [
81+
'uri' => $parsedUrl['scheme'].'://127.0.0.1/',
82+
];
83+
}
84+
85+
$config['uri'] = $parsedUrl['scheme'].'://'.$parsedUrl['host'];
86+
87+
if (isset($parsedUrl['path']) && '/' !== $parsedUrl['path']) {
88+
$pathArr = explode('/', $parsedUrl['path']);
89+
90+
//DB name
91+
if ($pathArr[1]) {
92+
$config['dbname'] = $pathArr[1];
93+
$config['uri'] .= '/'.$pathArr[1];
94+
}
95+
96+
//Collection name
97+
if ($pathArr[2]) {
98+
$config['collection_name'] = $pathArr[2];
99+
}
100+
}
101+
102+
if (isset($parsedUrl['query'])) {
103+
$config['uri'] .= '?'.$parsedUrl['query'];
104+
//get polling_interval value
105+
if (false !== strpos($parsedUrl['query'], 'polling_interval')) {
106+
preg_match_all('/polling_interval=([0-9]+)/', $parsedUrl['query'], $matches);
107+
if (isset($matches[1][0])) {
108+
$config['polling_interval'] = $matches[1][0];
109+
}
110+
}
111+
}
112+
113+
return $config;
67114
}
68115
}

0 commit comments

Comments
 (0)