forked from olamedia/kanon-mongo
/
connection.php
103 lines (101 loc) · 3 KB
/
connection.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
<?php
namespace ru\olamedia\kanon\mongo;
class connection{
protected static $_instances = [];
protected static $_classInstances = [];
protected static $_classCollectionName = [];
private $_instanceName = '';
private $_config = [];
private $_client = null;
private $_database = null;
private function __construct($instanceName){
$this->_instanceName = $instanceName;
}
public static function getInstance($instanceName = 'default'){
if (!isset(self::$_instances[$instanceName])){
self::$_instances[$instanceName] = new self($instanceName);
}
return self::$_instances[$instanceName];
}
// Lazy connection
public function connect(array $config = []){
$this->_config = $config;
return $this;
}
public static function getCollectionNameByClassName($className){
if (isset(self::$_classCollectionName[$className])){
return self::$_classCollectionName[$className];
}
return null;
}
public static function forClass($className){
if (!isset(self::$_classInstances[$className])){
return null;
}
return self::$_classInstances[$className];
}
public function registerModel($className, $collectionName){
self::$_classInstances[$className] = $this; // register own instance
self::$_classCollectionName[$className] = $collectionName;
typeManager::registerModel($className);
return $this;
}
/**
* @return \MongoClient
* @throws \Exception
*/
private function _getClient(){
if (null == $this->_client){
$config = $this->_config['connection'];
$config = \array_merge(array(
'hostnames' => 'localhost:27017'
), $config);
/* Add Username & Password to server string */
if (isset($config['username']) && isset($config['password'])){
$config['hostnames'] = $config['username'] . ':' . $config['password'] . '@' . $config['hostnames'] . '/' . $config['database'];
}
/* Add required 'mongodb://' prefix */
if (\strpos($config['hostnames'], 'mongodb://') !== 0){
$config['hostnames'] = 'mongodb://' . $config['hostnames'];
}
if (!\class_exists('\MongoClient')){ // PECL mongoclient >= 1.3.0
throw new \Exception('Required PECL mongoclient >= 1.3.0');
}
$this->_client = new \MongoClient($config['hostnames'], [
'connect' => false
]);
try{
$this->_client->connect();
}catch (\MongoConnectionException $e){
throw new \Exception('Unable to connect to MongoDB server at ' . $config['hostnames']);
}
if (!isset($config['database'])){
throw new \Exception('No database specified in MongoDB Config');
}
$this->_database = $this->_client->selectDB($config['database']);
}
return $this->_client;
}
/**
* @return \MongoCollection
* @param string $name
*/
public function getCollection($name){
return $this->getDatabase()->selectCollection($name);
}
/**
* @return \MongoDB
*/
private function getDatabase(){
if (null == $this->_database){
$this->_getClient();
}
return $this->_database;
}
/**
* @return boolean
*/
public function isConnected(){
return null !== $this->_client && $this->_client->connected;
}
}