-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathConnection.php
435 lines (405 loc) · 15 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mongodb;
use MongoDB\Driver\Manager;
use yii\base\Component;
use yii\base\InvalidConfigException;
use Yii;
/**
* Connection represents a connection to a MongoDb server.
*
* Connection works together with [[Database]] and [[Collection]] to provide data access
* to the Mongo database. They are wrappers of the [[MongoDB PHP extension]](http://us1.php.net/manual/en/book.mongo.php).
*
* To establish a DB connection, set [[dsn]] and then call [[open()]] to be true.
*
* The following example shows how to create a Connection instance and establish
* the DB connection:
*
* ```php
* $connection = new \yii\mongodb\Connection([
* 'dsn' => $dsn,
* ]);
* $connection->open();
* ```
*
* After the Mongo connection is established, one can access Mongo databases and collections:
*
* ```php
* $database = $connection->getDatabase('my_mongo_db');
* $collection = $database->getCollection('customer');
* $collection->insert(['name' => 'John Smith', 'status' => 1]);
* ```
*
* You can work with several different databases at the same server using this class.
* However, while it is unlikely your application will actually need it, the Connection class
* provides ability to use [[defaultDatabaseName]] as well as a shortcut method [[getCollection()]]
* to retrieve a particular collection instance:
*
* ```php
* // get collection 'customer' from default database:
* $collection = $connection->getCollection('customer');
* // get collection 'customer' from database 'mydatabase':
* $collection = $connection->getCollection(['mydatabase', 'customer']);
* ```
*
* Connection is often used as an application component and configured in the application
* configuration like the following:
*
* ```php
* [
* 'components' => [
* 'mongodb' => [
* 'class' => '\yii\mongodb\Connection',
* 'dsn' => 'mongodb://developer:password@localhost:27017/mydatabase',
* ],
* ],
* ]
* ```
*
* @property Database $database Database instance. This property is read-only.
* @property string $defaultDatabaseName Default database name.
* @property file\Collection $fileCollection Mongo GridFS collection instance. This property is read-only.
* @property bool $isActive Whether the Mongo connection is established. This property is read-only.
* @property LogBuilder $logBuilder The log builder for this connection. Note that the type of this property
* differs in getter and setter. See [[getLogBuilder()]] and [[setLogBuilder()]] for details.
* @property QueryBuilder $queryBuilder The query builder for the this MongoDB connection. Note that the type
* of this property differs in getter and setter. See [[getQueryBuilder()]] and [[setQueryBuilder()]] for
* details.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class Connection extends Component
{
/**
* @event Event an event that is triggered after a DB connection is established
*/
const EVENT_AFTER_OPEN = 'afterOpen';
/**
* @var string host:port
*
* Correct syntax is:
* mongodb://[username:password@]host1[:port1][,host2[:port2:],...][/dbname]
* For example:
* mongodb://localhost:27017
* mongodb://developer:password@localhost:27017
* mongodb://developer:password@localhost:27017/mydatabase
*/
public $dsn;
/**
* @var array connection options.
* For example:
*
* ```php
* [
* 'socketTimeoutMS' => 1000, // how long a send or receive on a socket can take before timing out
* 'ssl' => true // initiate the connection with TLS/SSL
* ]
* ```
*
* @see https://docs.mongodb.com/manual/reference/connection-string/#connections-connection-options
*/
public $options = [];
/**
* @var array options for the MongoDB driver.
* Any driver-specific options not included in MongoDB connection string specification.
*
* @see http://php.net/manual/en/mongodb-driver-manager.construct.php
*/
public $driverOptions = [];
/**
* @var Manager MongoDB driver manager.
* @since 2.1
*/
public $manager;
/**
* @var array type map to use for BSON unserialization.
* Note: default type map will be automatically merged into this field, possibly overriding user-defined values.
* @see http://php.net/manual/en/mongodb-driver-cursor.settypemap.php
* @since 2.1
*/
public $typeMap = [];
/**
* @var bool whether to log command and query executions.
* When enabled this option may reduce performance. MongoDB commands may contain large data,
* consuming both CPU and memory.
* It makes sense to disable this option in the production environment.
* @since 2.1
*/
public $enableLogging = true;
/**
* @var bool whether to enable profiling the commands and queries being executed.
* This option will have no effect in case [[enableLogging]] is disabled.
* @since 2.1
*/
public $enableProfiling = true;
/**
* @var string name of the protocol, which should be used for the GridFS stream wrapper.
* Only alphanumeric values are allowed: do not use any URL special characters, such as '/', '&', ':' etc.
* @see \yii\mongodb\file\StreamWrapper
* @since 2.1
*/
public $fileStreamProtocol = 'gridfs';
/**
* @var string name of the class, which should serve as a stream wrapper for [[fileStreamProtocol]] protocol.
* @since 2.1
*/
public $fileStreamWrapperClass = 'yii\mongodb\file\StreamWrapper';
/**
* @var string name of the MongoDB database to use by default.
* If this field left blank, connection instance will attempt to determine it from
* [[dsn]] automatically, if needed.
*/
private $_defaultDatabaseName;
/**
* @var Database[] list of Mongo databases
*/
private $_databases = [];
/**
* @var QueryBuilder|array|string the query builder for this connection
* @since 2.1
*/
private $_queryBuilder = 'yii\mongodb\QueryBuilder';
/**
* @var LogBuilder|array|string log entries builder used for this connecton.
* @since 2.1
*/
private $_logBuilder = 'yii\mongodb\LogBuilder';
/**
* @var bool whether GridFS stream wrapper has been already registered.
* @since 2.1
*/
private $_fileStreamWrapperRegistered = false;
/**
* Sets default database name.
* @param string $name default database name.
*/
public function setDefaultDatabaseName($name)
{
$this->_defaultDatabaseName = $name;
}
/**
* Returns default database name, if it is not set,
* attempts to determine it from [[dsn]] value.
* @return string default database name
* @throws \yii\base\InvalidConfigException if unable to determine default database name.
*/
public function getDefaultDatabaseName()
{
if ($this->_defaultDatabaseName === null) {
if (preg_match('/^mongodb:\\/\\/.+\\/([^?&]+)/s', $this->dsn, $matches)) {
$this->_defaultDatabaseName = $matches[1];
} else {
throw new InvalidConfigException("Unable to determine default database name from dsn.");
}
}
return $this->_defaultDatabaseName;
}
/**
* Returns the query builder for the this MongoDB connection.
* @return QueryBuilder the query builder for the this MongoDB connection.
* @since 2.1
*/
public function getQueryBuilder()
{
if (!is_object($this->_queryBuilder)) {
$this->_queryBuilder = Yii::createObject($this->_queryBuilder, [$this]);
}
return $this->_queryBuilder;
}
/**
* Sets the query builder for the this MongoDB connection.
* @param QueryBuilder|array|string|null $queryBuilder the query builder for this MongoDB connection.
* @since 2.1
*/
public function setQueryBuilder($queryBuilder)
{
$this->_queryBuilder = $queryBuilder;
}
/**
* Returns log builder for this connection.
* @return LogBuilder the log builder for this connection.
* @since 2.1
*/
public function getLogBuilder()
{
if (!is_object($this->_logBuilder)) {
$this->_logBuilder = Yii::createObject($this->_logBuilder);
}
return $this->_logBuilder;
}
/**
* Sets log builder used for this connection.
* @param array|string|LogBuilder $logBuilder the log builder for this connection.
* @since 2.1
*/
public function setLogBuilder($logBuilder)
{
$this->_logBuilder = $logBuilder;
}
/**
* Returns the MongoDB database with the given name.
* @param string|null $name database name, if null default one will be used.
* @param bool $refresh whether to reestablish the database connection even, if it is found in the cache.
* @return Database database instance.
*/
public function getDatabase($name = null, $refresh = false)
{
if ($name === null) {
$name = $this->getDefaultDatabaseName();
}
if ($refresh || !array_key_exists($name, $this->_databases)) {
$this->_databases[$name] = $this->selectDatabase($name);
}
return $this->_databases[$name];
}
/**
* Selects the database with given name.
* @param string $name database name.
* @return Database database instance.
*/
protected function selectDatabase($name)
{
return Yii::createObject([
'class' => 'yii\mongodb\Database',
'name' => $name,
'connection' => $this,
]);
}
/**
* Returns the MongoDB collection with the given name.
* @param string|array $name collection name. If string considered as the name of the collection
* inside the default database. If array - first element considered as the name of the database,
* second - as name of collection inside that database
* @param bool $refresh whether to reload the collection instance even if it is found in the cache.
* @return Collection Mongo collection instance.
*/
public function getCollection($name, $refresh = false)
{
if (is_array($name)) {
list ($dbName, $collectionName) = $name;
return $this->getDatabase($dbName)->getCollection($collectionName, $refresh);
}
return $this->getDatabase()->getCollection($name, $refresh);
}
/**
* Returns the MongoDB GridFS collection.
* @param string|array $prefix collection prefix. If string considered as the prefix of the GridFS
* collection inside the default database. If array - first element considered as the name of the database,
* second - as prefix of the GridFS collection inside that database, if no second element present
* default "fs" prefix will be used.
* @param bool $refresh whether to reload the collection instance even if it is found in the cache.
* @return file\Collection Mongo GridFS collection instance.
*/
public function getFileCollection($prefix = 'fs', $refresh = false)
{
if (is_array($prefix)) {
list ($dbName, $collectionPrefix) = $prefix;
if (!isset($collectionPrefix)) {
$collectionPrefix = 'fs';
}
return $this->getDatabase($dbName)->getFileCollection($collectionPrefix, $refresh);
}
return $this->getDatabase()->getFileCollection($prefix, $refresh);
}
/**
* Returns a value indicating whether the Mongo connection is established.
* @return bool whether the Mongo connection is established
*/
public function getIsActive()
{
return is_object($this->manager) && $this->manager->getServers() !== [];
}
/**
* Establishes a Mongo connection.
* It does nothing if a MongoDB connection has already been established.
* @throws Exception if connection fails
*/
public function open()
{
if ($this->manager === null) {
if (empty($this->dsn)) {
throw new InvalidConfigException($this->className() . '::dsn cannot be empty.');
}
$token = 'Opening MongoDB connection: ' . $this->dsn;
try {
Yii::trace($token, __METHOD__);
Yii::beginProfile($token, __METHOD__);
$options = $this->options;
$this->manager = new Manager($this->dsn, $options, $this->driverOptions);
$this->manager->selectServer($this->manager->getReadPreference());
$this->initConnection();
Yii::endProfile($token, __METHOD__);
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), (int) $e->getCode(), $e);
}
$this->typeMap = array_merge(
$this->typeMap,
[
'root' => 'array',
'document' => 'array'
]
);
}
}
/**
* Closes the currently active DB connection.
* It does nothing if the connection is already closed.
*/
public function close()
{
if ($this->manager !== null) {
Yii::trace('Closing MongoDB connection: ' . $this->dsn, __METHOD__);
$this->manager = null;
foreach ($this->_databases as $database) {
$database->clearCollections();
}
$this->_databases = [];
}
}
/**
* Initializes the DB connection.
* This method is invoked right after the DB connection is established.
* The default implementation triggers an [[EVENT_AFTER_OPEN]] event.
*/
protected function initConnection()
{
$this->trigger(self::EVENT_AFTER_OPEN);
}
/**
* Creates MongoDB command.
* @param array $document command document contents.
* @param string|null $databaseName database name, if not set [[defaultDatabaseName]] will be used.
* @return Command command instance.
* @since 2.1
*/
public function createCommand($document = [], $databaseName = null)
{
return new Command([
'db' => $this,
'databaseName' => $databaseName,
'document' => $document,
]);
}
/**
* Registers GridFS stream wrapper for the [[fileStreamProtocol]] protocol.
* @param bool $force whether to enforce registration even wrapper has been already registered.
* @return string registered stream protocol name.
*/
public function registerFileStreamWrapper($force = false)
{
if ($force || !$this->_fileStreamWrapperRegistered) {
/* @var $class \yii\mongodb\file\StreamWrapper */
$class = $this->fileStreamWrapperClass;
$class::register($this->fileStreamProtocol, $force);
$this->_fileStreamWrapperRegistered = true;
}
return $this->fileStreamProtocol;
}
}