-
Notifications
You must be signed in to change notification settings - Fork 3
/
MongodbConnectionFactory.php
114 lines (104 loc) · 3.83 KB
/
MongodbConnectionFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace Enqueue\Mongodb;
use Interop\Queue\ConnectionFactory;
use Interop\Queue\Context;
use MongoDB\Client;
class MongodbConnectionFactory implements ConnectionFactory
{
/**
* @var array
*/
private $config;
/**
* 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.
*
* $config = [
* 'dsn' => 'mongodb://127.0.0.1/' - Mongodb connection string. see http://docs.mongodb.org/manual/reference/connection-string/
* 'dbname' => 'enqueue', - database name.
* 'collection_name' => 'enqueue' - collection name
* 'polling_interval' => '1000', - How often query for new messages (milliseconds)
* ]
*
* or
*
* mongodb://127.0.0.1:27017/defaultauthdb?polling_interval=1000&enqueue_database=enqueue&enqueue_collection=enqueue
*
* @param array|string|null $config
*/
public function __construct($config = 'mongodb:')
{
if (empty($config)) {
$config = $this->parseDsn('mongodb:');
} elseif (is_string($config)) {
$config = $this->parseDsn($config);
} elseif (is_array($config)) {
$config = array_replace(
$config,
$this->parseDsn(empty($config['dsn']) ? 'mongodb:' : $config['dsn'])
);
} else {
throw new \LogicException('The config must be either an array of options, a DSN string or null');
}
$config = array_replace([
'dsn' => 'mongodb://127.0.0.1/',
'dbname' => 'enqueue',
'collection_name' => 'enqueue',
], $config);
$this->config = $config;
}
/**
* @return MongodbContext
*/
public function createContext(): Context
{
$client = new Client($this->config['dsn']);
return new MongodbContext($client, $this->config);
}
public static function parseDsn(string $dsn): array
{
$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 ('mongodb:' === $dsn) {
return [
'dsn' => 'mongodb://127.0.0.1/',
];
}
$config['dsn'] = $dsn;
// FIXME this is NOT a dbname but rather authdb. But removing this would be a BC break.
// see: https://github.com/php-enqueue/enqueue-dev/issues/1027
if (isset($parsedUrl['path']) && '/' !== $parsedUrl['path']) {
$pathParts = explode('/', $parsedUrl['path']);
//DB name
if ($pathParts[1]) {
$config['dbname'] = $pathParts[1];
}
}
if (isset($parsedUrl['query'])) {
$queryParts = null;
parse_str($parsedUrl['query'], $queryParts);
//get enqueue attributes values
if (!empty($queryParts['polling_interval'])) {
$config['polling_interval'] = (int) $queryParts['polling_interval'];
}
if (!empty($queryParts['enqueue_collection'])) {
$config['collection_name'] = $queryParts['enqueue_collection'];
}
if (!empty($queryParts['enqueue_database'])) {
$config['dbname'] = $queryParts['enqueue_database'];
}
}
return $config;
}
}