This commit is contained in:
Fabien Potencier 2014-02-21 07:38:26 +01:00
parent a8955bf96c
commit 7ee758c5fb
2 changed files with 18 additions and 16 deletions

View File

@ -99,18 +99,19 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
*/
protected function getMongo()
{
if ($this->mongo === null) {
if ($parsedDsn = $this->parseDsn($this->dsn)) {
list($server, $database, $collection) = $parsedDsn;
$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@]host/database/collection"', $this->dsn));
}
if (null !== $this->mongo) {
return $this->mongo;
}
return $this->mongo;
if (!$parsedDsn = $this->parseDsn($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));
}
list($server, $database, $collection) = $parsedDsn;
$mongoClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? '\Mongo' : '\MongoClient';
$mongo = new $mongoClass($server);
return $this->mongo = $mongo->selectCollection($database, $collection);
}
/**
@ -239,13 +240,15 @@ class MongoDbProfilerStorage implements ProfilerStorageInterface
private function parseDsn($dsn)
{
if (!preg_match('#^(mongodb://.*)/(.*)/(.*)$#', $dsn, $matches)) {
return null;
return;
}
$server = $matches[1];
$database = $matches[2];
$collection = $matches[3];
preg_match('#^mongodb://(([^:]+):?(.*)(?=@))?@?([^/]*)(.*)$#', $server, $matchesServer);
if ($matchesServer[5]=="" && $matches[2]!="") {
if ('' == $matchesServer[5] && '' != $matches[2]) {
$server .= '/'.$matches[2];
}

View File

@ -71,7 +71,7 @@ class MongoDbProfilerStorageTest extends AbstractProfilerStorageTest
}
}
public function providerDsn()
public function getDsns()
{
return array(
array('mongodb://localhost/symfony_tests/profiler_data', array(
@ -114,12 +114,11 @@ class MongoDbProfilerStorageTest extends AbstractProfilerStorageTest
}
/**
* @dataProvider providerDsn
* @dataProvider getDsns
*/
public function testDsnParser($dsn, $expected)
{
$r=new \ReflectionObject(self::$storage);
$m=$r->getMethod('parseDsn');
$m = new \ReflectionMethod(self::$storage, 'parseDsn');
$m->setAccessible(true);
$this->assertEquals($expected, $m->invoke(self::$storage, $dsn));