diff --git a/lib/Doctrine/MongoDB/Connection.php b/lib/Doctrine/MongoDB/Connection.php index 0034cc5d..6fa570ab 100644 --- a/lib/Doctrine/MongoDB/Connection.php +++ b/lib/Doctrine/MongoDB/Connection.php @@ -276,6 +276,9 @@ public function initialize() $server = $this->server ?: 'mongodb://localhost:27017'; $options = $this->options; + $options = isset($options['timeout']) ? $this->convertConnectTimeout($options) : $options; + $options = isset($options['wTimeout']) ? $this->convertWriteTimeout($options) : $options; + $this->mongoClient = $this->retry(function() use ($server, $options) { return version_compare(phpversion('mongo'), '1.3.0', '<') ? new \Mongo($server, $options) @@ -442,4 +445,52 @@ protected function retry(\Closure $retry) } } } + + /** + * Converts "timeout" MongoClient constructor option to "connectTimeoutMS" + * for driver versions 1.4.0+. + * + * Note: MongoClient actually allows case-insensitive option names, but + * we'll only process the canonical version here. + * + * @param array $options + * @return array + */ + protected function convertConnectTimeout(array $options) + { + if (version_compare(phpversion('mongo'), '1.4.0', '<')) { + return $options; + } + + if (isset($options['timeout']) && ! isset($options['connectTimeoutMS'])) { + $options['connectTimeoutMS'] = $options['timeout']; + unset($options['timeout']); + } + + return $options; + } + + /** + * Converts "timeout" MongoClient constructor option to "connectTimeoutMS" + * for driver versions 1.4.0+. + * + * Note: MongoClient actually allows case-insensitive option names, but + * we'll only process the canonical version here. + * + * @param array $options + * @return array + */ + protected function convertWriteTimeout(array $options) + { + if (version_compare(phpversion('mongo'), '1.4.0', '<')) { + return $options; + } + + if (isset($options['wTimeout']) && ! isset($options['wTimeoutMS'])) { + $options['wTimeoutMS'] = $options['wTimeout']; + unset($options['wTimeout']); + } + + return $options; + } } diff --git a/tests/Doctrine/MongoDB/Tests/ConnectionTest.php b/tests/Doctrine/MongoDB/Tests/ConnectionTest.php index 6f95e6a7..161abbc6 100644 --- a/tests/Doctrine/MongoDB/Tests/ConnectionTest.php +++ b/tests/Doctrine/MongoDB/Tests/ConnectionTest.php @@ -206,6 +206,22 @@ public function testToString() $this->assertEquals('Test', (string) $conn); } + public function testConnectTimeoutOptionIsConverted() + { + if (version_compare(phpversion('mongo'), '1.4.0', '<')) { + $this->markTestSkipped('This test is not applicable to driver versions < 1.4.0'); + } + + /* Since initialize() creates MongoClient directly, we cannot examine + * the options passed to its constructor. + * + * Note: we do not test "wTimeout" conversion, since the driver does not + * raise a deprecation notice for its usage (see: PHP-1079). + */ + $conn = new Connection(null, array('timeout' => 10000)); + $conn->initialize(); + } + private function getTestConnection($mongo) { return new Connection($mongo);