forked from mongodb/laravel-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Connection.php
222 lines (191 loc) · 5.21 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php namespace Moloquent;
use MongoDB\Client;
class Connection extends \Illuminate\Database\Connection
{
/**
* The MongoDB database handler.
*
* @var \MongoDB\Database
*/
protected $db;
/**
* The MongoDB connection handler.
*
* @var \MongoDB\Client
*/
protected $connection;
/**
* Create a new database connection instance.
*
* @param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
// Build the connection string
$dsn = $this->getDsn($config);
// You can pass options directly to the MongoDB constructor
$options = array_get($config, 'options', []);
// Create the connection
$this->connection = $this->createConnection($dsn, $config, $options);
// Select database
$this->db = $this->connection->selectDatabase($config['database']);
$this->useDefaultPostProcessor();
}
/**
* Get the default post processor instance.
*
* @return Query\Processor
*/
protected function getDefaultPostProcessor()
{
return new Query\Processor;
}
/**
* Begin a fluent query against a database collection.
*
* @param string $collection
* @return Query\Builder
*/
public function collection($collection)
{
$processor = $this->getPostProcessor();
$query = new Query\Builder($this, $processor);
return $query->from($collection);
}
/**
* Begin a fluent query against a database collection.
*
* @param string $table
* @return Query\Builder
*/
public function table($table)
{
return $this->collection($table);
}
/**
* Get a MongoDB collection.
*
* @param string $name
* @return Collection
*/
public function getCollection($name)
{
return new Collection($this, $this->db->selectCollection($name));
}
/**
* Get a schema builder instance for the connection.
*
* @return Schema\Builder
*/
public function getSchemaBuilder()
{
return new Schema\Builder($this);
}
/**
* Get the MongoDB database object.
*
* @return \MongoDB\Database
*/
public function getMongoDB()
{
return $this->db;
}
/**
* return MongoDB object.
*
* @return \MongoDB\Client
*/
public function getMongoClient()
{
return $this->connection;
}
/**
* Create a new MongoDB connection.
*
* @param string $dsn
* @param array $config
* @param array $options
* @return \MongoDB\Client
*/
protected function createConnection($dsn, array $config, array $options)
{
// By default driver options is an empty array.
$driverOptions = [];
if (isset($config['driver_options']) && is_array($config['driver_options'])) {
$driverOptions = $config['driver_options'];
}
// Check if the credentials are not already set in the options
if (!isset($options['username']) && !empty($config['username'])) {
$options['username'] = $config['username'];
}
if (!isset($options['password']) && !empty($config['password'])) {
$options['password'] = $config['password'];
}
return new Client($dsn, $options, $driverOptions);
}
/**
* Disconnect from the underlying MongoDB connection.
*/
public function disconnect()
{
unset($this->connection);
}
/**
* Create a DSN string from a configuration.
*
* @param array $config
* @return string
*/
protected function getDsn(array $config)
{
// First we will create the basic DSN setup as well as the port if it is in
// in the configuration options. This will give us the basic DSN we will
// need to establish the MongoDB and return them back for use.
$host = $database = null;
extract($config);
// Check if the user passed a complete dsn to the configuration.
if (! empty($dsn)) {
return $dsn;
}
// Treat host option as array of hosts
$hosts = is_array($host) ? $host : [$host];
foreach ($hosts as &$host) {
// Check if we need to add a port to the host
if (strpos($host, ':') === false and isset($port)) {
$host = "{$host}:{$port}";
}
}
return "mongodb://" . implode(',', $hosts) . "/{$database}";
}
/**
* Get the elapsed time since a given starting point.
*
* @param int $start
* @return float
*/
public function getElapsedTime($start)
{
return parent::getElapsedTime($start);
}
/**
* Get the PDO driver name.
*
* @return string
*/
public function getDriverName()
{
return 'mongodb';
}
/**
* Dynamically pass methods to the connection.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return call_user_func_array([$this->db, $method], $parameters);
}
}