forked from php-enqueue/enqueue-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongodbConnectionFactory.php
101 lines (92 loc) · 3.2 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
<?php
namespace Enqueue\Mongodb;
use Interop\Queue\PsrConnectionFactory;
use MongoDB\Client;
class MongodbConnectionFactory implements PsrConnectionFactory
{
/**
* @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 = [
* 'uri' => '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/dbname?polling_interval=1000&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)) {
} else {
throw new \LogicException('The config must be either an array of options, a DSN string or null');
}
$config = array_replace([
'uri' => 'mongodb://127.0.0.1/',
], $config);
$this->config = $config;
}
public function createContext()
{
$client = new Client($this->config['uri']);
return new MongodbContext($client, $this->config);
}
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 ('mongodb:' === $dsn) {
return [
'uri' => 'mongodb://127.0.0.1/',
];
}
$config['uri'] = $dsn;
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'] = $queryParts['polling_interval'];
}
if (!empty($queryParts['enqueue_collection'])) {
$config['collection_name'] = $queryParts['enqueue_collection'];
}
}
return $config;
}
}