From de19a81107cc8a6eb1caac75376216cbb4657ef2 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Thu, 13 Dec 2012 15:52:09 -0500 Subject: [PATCH] [HttpKernel] Support MongoClient and Mongo connection classes MongoClient defaults its write concern to w=1 (i.e. "safe" writes), which means update() may return an array instead of boolean true. Check for this before returning from write(). --- .../HttpKernel/Profiler/MongoDbProfilerStorage.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php b/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php index 59a9282da4..382ebbfe1f 100644 --- a/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php +++ b/src/Symfony/Component/HttpKernel/Profiler/MongoDbProfilerStorage.php @@ -102,7 +102,9 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface 'time' => $profile->getTime() ); - return $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true)); + $result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true)); + + return isset($result['ok']) ? (boolean) $result['ok'] : true; } /** @@ -114,12 +116,15 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface { if ($this->mongo === null) { if (preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $this->dsn, $matches)) { - $mongo = new \Mongo($matches[1] . (!empty($matches[2]) ? '/' . $matches[2] : '')); + $server = $matches[1] . (!empty($matches[2]) ? '/' . $matches[2] : ''); $database = $matches[2]; $collection = $matches[3]; + + $mongoClass = (version_compare(phpversion('mongo'), '1.3.0', '<')) ? '\Mongo' : '\MongoClient'; + $mongo = new $mongoClass($server); $this->mongo = $mongo->selectCollection($database, $collection); } else { - throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use MongoDB with an invalid dsn "%s". The expected format is "mongodb://user:pass@location/database/collection"', $this->dsn)); + throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use MongoDB with an invalid dsn "%s". The expected format is "mongodb://[user:pass@]host/database/collection"', $this->dsn)); } }