|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the PHPMongo package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dmytro Sokil <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sokil\Mongo; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Pool of mongo connections. May be used if required few connections to different databases |
|
16
|
|
|
* |
|
17
|
|
|
* @link https://github.com/sokil/php-mongo/blob/master/README.md#pool-of-connections |
|
18
|
|
|
*/ |
|
19
|
|
|
class ClientPool |
|
20
|
|
|
{ |
|
21
|
|
|
private $pool = array(); |
|
22
|
|
|
|
|
23
|
|
|
private $configuration; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(array $configuration = array()) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->configuration = $configuration; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Add connection to pool |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $name connection identifier |
|
34
|
|
|
* @param string $dsn data source name |
|
35
|
|
|
* @param array $mapping mapping configuration |
|
36
|
|
|
* @param string $defaultDatabase name of database used as default |
|
37
|
|
|
* @param array $connectOptions connect options |
|
38
|
|
|
* @return \Sokil\Mongo\ClientPool |
|
39
|
|
|
*/ |
|
40
|
|
|
public function addConnection( |
|
41
|
|
|
$name, |
|
42
|
|
|
$dsn = null, |
|
43
|
|
|
array $mapping = null, |
|
44
|
|
|
$defaultDatabase = null, |
|
45
|
|
|
array $connectOptions = null |
|
46
|
|
|
) { |
|
47
|
|
|
$this->configuration[$name] = array( |
|
48
|
|
|
'dsn' => $dsn, |
|
49
|
|
|
'connectOptions' => $connectOptions, |
|
50
|
|
|
'defaultDatabase' => $defaultDatabase, |
|
51
|
|
|
'mapping' => $mapping, |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function __get($name) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->get($name); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get instance of connection |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $name connection identifier |
|
66
|
|
|
* @return \Sokil\Mongo\Client |
|
67
|
|
|
* @throws \Sokil\Mongo\Exception |
|
68
|
|
|
*/ |
|
69
|
|
|
public function get($name) |
|
70
|
|
|
{ |
|
71
|
|
|
// get from cache |
|
72
|
|
|
if (isset($this->pool[$name])) { |
|
73
|
|
|
return $this->pool[$name]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// check if connection exists |
|
77
|
|
|
if (!isset($this->configuration[$name])) { |
|
78
|
|
|
throw new Exception('Connection with name ' . $name . ' not found'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// check if dsn exists |
|
82
|
|
|
if (!isset($this->configuration[$name]['dsn'])) { |
|
83
|
|
|
$this->configuration[$name]['dsn'] = null; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// check if connect options exists |
|
87
|
|
|
if (empty($this->configuration[$name]['connectOptions'])) { |
|
88
|
|
|
$this->configuration[$name]['connectOptions'] = null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// init client |
|
92
|
|
|
$client = new Client( |
|
93
|
|
|
$this->configuration[$name]['dsn'], |
|
94
|
|
|
$this->configuration[$name]['connectOptions'] |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
if (isset($this->configuration[$name]['mapping'])) { |
|
98
|
|
|
$client->map($this->configuration[$name]['mapping']); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (isset($this->configuration[$name]['defaultDatabase'])) { |
|
102
|
|
|
$client->useDatabase($this->configuration[$name]['defaultDatabase']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$this->pool[$name] = $client; |
|
106
|
|
|
|
|
107
|
|
|
return $client; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|