Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#7203 from marc-mabe/ca…
Browse files Browse the repository at this point in the history
…che/fixMongoOptions

Cache: fix mongo options
  • Loading branch information
weierophinney committed Feb 25, 2015
155 parents 99cd926 + da594a3 + e15aeac + a5fb9cb + 02e14e6 + e24be06 + 1cd130f + 2f4d84a + c5055d9 + b5302e2 + 9969a8b + 83a796c + 7f06dd1 + 40e1948 + fae5b14 + 1d8b0d4 + 0885ee4 + 31378a4 + 22dc55a + 4fe9653 + 0835021 + 26ee41a + be4158f + a1e2d7e + 4abdf38 + e59ea5a + fbff658 + 48d0341 + 401d180 + 4e95264 + 5431207 + a7a7974 + 7c3f597 + a50748b + 700cf3a + 1582c31 + 016f50c + 9852497 + 073b126 + bd99c5c + 56451bd + 8fe4cad + 18812d8 + 89590ab + 0f8f25b + c2a03a9 + 4c690cd + 52cb4e6 + f7ed984 + 1d190a2 + 3c5d19b + c3c849d + faa6e57 + ad46181 + 12a401e + 3738445 + 65aacba + bf5f5a3 + c2ac9c0 + 6d7cf99 + 1841b47 + f81171b + 5ffaadf + cd6d5f2 + a512601 + 926e71f + 1f29b3e + a75171a + 9a8e72f + 814716e + 88d9371 + 678232d + a0560e8 + 4ff045c + ac805a6 + 41f360c + 1939a3c + 730da5d + dd0a1b5 + 1bc1ed5 + cea3413 + f2b47df + 2b8aa6a + 42cb3fe + 46afe3b + ef4289e + f40a98a + 8c27e9d + 512d6ca + b1452f8 + 9f6e228 + cf96a5a + aa9ef86 + 04be01e + 93487ba + c640cb5 + ab2b436 + a876e82 + 01f54c9 + f0abecc + 920eae1 + db4f625 + f51678c + 5975f0e + e9d78fe + 03de5ee + d7320d7 + 16ff5c5 + f170b61 + b506606 + f6c7309 + 675c421 + 43220df + c076d41 + e9406c0 + bb1710e + 565433c + be0baa3 + 8d2dae7 + ba17210 + bf83129 + 40926a2 + a7690f0 + 4fbd64e + f1f095f + 5077dc6 + 7d6f60c + a5f3296 + e036825 + 337996c + 285fc46 + 277a8aa + 0103b22 + 0d32a70 + bb27ee5 + e8ecd1e + f2b29a0 + 5205046 + 257c166 + 3122c56 + a8eef4b + 736bf24 + f1bdd85 + 30d927d + 0b20126 + ef5cf6f + 2efe2a7 + 9c5c0a0 + b163911 + 078c1c8 + ae78853 + 4290f30 + 77fe406 + fb5d494 + 2ba295f commit 9bd872f
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 239 deletions.
33 changes: 27 additions & 6 deletions src/Storage/Adapter/MongoDbOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,38 @@ public function setResourceId($resourceId)
}

/**
* set mongo options
*
* @param array $libOptions
* Set the mongo DB server
*
* @param string $server
* @return self
*/
public function setLibOptions(array $libOptions)
public function setServer($server)
{
$this->triggerOptionEvent('lib_option', $libOptions);
$this->getResourceManager()->setLibOptions($this->getResourceId(), $libOptions);
$this->getResourceManager()->setServer($this->getResourceId(), $server);
return $this;
}

public function setConnectionOptions(array $connectionOptions)
{
$this->getResourceManager()->setConnectionOptions($this->getResourceId(), $connectionOptions);
return $this;
}

public function setDriverOptions(array $driverOptions)
{
$this->getResourceManager()->setDriverOptions($this->getResourceId(), $driverOptions);
return $this;
}

public function setDatabase($database)
{
$this->getResourceManager()->setDatabase($this->getResourceId(), $database);
return $this;
}

public function setCollection($collection)
{
$this->getResourceManager()->setCollection($this->getResourceId(), $collection);
return $this;
}
}
166 changes: 110 additions & 56 deletions src/Storage/Adapter/MongoDbResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function hasResource($id)
* Set a resource
*
* @param string $id
* @param array|Traversable|MongoCollection $resource
* @param array|MongoCollection $resource
*
* @return self
*
Expand All @@ -50,44 +50,24 @@ public function setResource($id, $resource)
{
if ($resource instanceof MongoCollection) {
$this->resources[$id] = array(
'initialized' => true,
'resource' => $resource,
'db' => (string) $resource->db,
'db_instance' => $resource->db,
'collection' => (string) $resource,
'collection_instance' => $resource,
);

return $this;
}

$this->resources[$id] = array_merge(
array(
'server' => 'mongodb://localhost:27017',
'collection' => 'cache',
'database' => 'zend',
'driverOptions' => array(),
'connectionOptions' => array(
/**
* Journaling is enabled by default in 64bit builds of Mongo 2.0+
* As such, we should default fsync to false and journal to true
* See:
* http://docs.mongodb.org/manual/tutorial/manage-journaling/
* http://www.php.net/manual/en/mongoclient.construct.php
*/
'fsync' => false,
'journal' => true,
),
),
ArrayUtils::iteratorToArray($resource),
// force initialized flag to false
array('initialized' => false)
);

$this->resources[$id] = $resource;
return $this;
}

/**
* @param string $id
* Instantiate and return the MongoCollection resource
*
* @param string $id
* @return MongoCollection
*
* @throws Exception\RuntimeException
*/
public function getResource($id)
Expand All @@ -96,52 +76,126 @@ public function getResource($id)
throw new Exception\RuntimeException("No resource with id '{$id}'");
}

if (! $this->resources[$id]['initialized']) {
$clientClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? 'Mongo' : 'MongoClient';
$resource = $this->resources[$id];
if (!isset($resource['collection_instance'])) {

try {
/* @var $client \Mongo|\MongoClient */
$client = new $clientClass(
$this->resources[$id]['server'],
$this->resources[$id]['connectionOptions'],
(array) $this->resources[$id]['driverOptions']
);

$collection = $client->selectCollection(
$this->resources[$id]['database'],
$this->resources[$id]['collection']
);
if (!isset($resource['db_instance'])) {

if (!isset($resource['client_instance'])) {
$clientClass = version_compare(phpversion('mongo'), '1.3.0', '<') ? 'Mongo' : 'MongoClient';
$resource['client_instance'] = new $clientClass(
isset($resource['server']) ? $resource['server'] : null,
isset($resource['connection_options']) ? $resource['connection_options'] : array(),
isset($resource['driver_options']) ? $resource['driver_options'] : array()
);
}

$resource['db_instance'] = $resource['client_instance']->selectDB(
isset($resource['db']) ? $resource['db'] : ''
);
}

$collection = $resource['db_instance']->selectCollection(
isset($resource['collection']) ? $resource['collection'] : ''
);
$collection->ensureIndex(array('key' => 1));

$this->resources[$id]['resource'] = $collection;
$this->resources[$id]['collection_instance'] = $collection;

} catch (MongoException $e) {
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
}

$this->resources[$id]['initialized'] = true;
}

return $this->resources[$id]['resource'];
return $this->resources[$id]['collection_instance'];
}

/**
* @param string $id
* @param array $libOptions
*
* @return self
*/
public function setLibOptions($id, array $libOptions)
public function setServer($id, $server)
{
$this->resources[$id]['server'] = (string)$server;

unset($this->resource[$id]['client_instance']);
unset($this->resource[$id]['db_instance']);
unset($this->resource[$id]['collection_instance']);
}

public function getServer($id)
{
if (!$this->hasResource($id)) {
return $this->setResource($id, $libOptions);
throw new Exception\RuntimeException("No resource with id '{$id}'");
}

unset($this->resources[$id]['resource']);
return isset($this->resources[$id]['server']) ? $this->resources[$id]['server'] : null;
}

public function setConnectionOptions($id, array $connectionOptions)
{
$this->resources[$id]['connection_options'] = $connectionOptions;

$this->resources[$id] = array_merge($this->resources[$id], $libOptions);
$this->resources[$id]['initialized'] = false;
unset($this->resource[$id]['client_instance']);
unset($this->resource[$id]['db_instance']);
unset($this->resource[$id]['collection_instance']);
}

return $this;
public function getConnectionOptions($id)
{
if (!$this->hasResource($id)) {
throw new Exception\RuntimeException("No resource with id '{$id}'");
}

return isset($this->resources[$id]['connection_options']) ? $this->resources[$id]['connection_options'] : array();
}

public function setDriverOptions($id, array $driverOptions)
{
$this->resources[$id]['driver_options'] = $driverOptions;

unset($this->resource[$id]['client_instance']);
unset($this->resource[$id]['db_instance']);
unset($this->resource[$id]['collection_instance']);
}

public function getDriverOptions($id)
{
if (!$this->hasResource($id)) {
throw new Exception\RuntimeException("No resource with id '{$id}'");
}

return isset($this->resources[$id]['driver_options']) ? $this->resources[$id]['driver_options'] : array();
}

public function setDatabase($id, $database)
{
$this->resources[$id]['db'] = (string)$database;

unset($this->resource[$id]['db_instance']);
unset($this->resource[$id]['collection_instance']);
}

public function getDatabase($id)
{
if (!$this->hasResource($id)) {
throw new Exception\RuntimeException("No resource with id '{$id}'");
}

return isset($this->resources[$id]['db']) ? $this->resources[$id]['db'] : '';
}

public function setCollection($id, $collection)
{
$this->resources[$id]['collection'] = (string)$collection;

unset($this->resource[$id]['collection_instance']);
}

public function getCollection($id)
{
if (!$this->hasResource($id)) {
throw new Exception\RuntimeException("No resource with id '{$id}'");
}

return isset($this->resources[$id]['collection']) ? $this->resources[$id]['collection'] : '';
}
}
29 changes: 7 additions & 22 deletions test/Storage/Adapter/MongoDbOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,17 @@ public function testGetResourceId()
$this->assertEquals($resourceId, $this->object->getResourceId());
}

public function testSetLibOptions()
public function testSetServer()
{
$resourceManager = new MongoDbResourceManager();
$this->object->setResourceManager($resourceManager);

$this->assertAttributeEmpty('resources', $this->object->getResourceManager());

$libOptions = array('foo' => 'bar');

$this->object->setLibOptions($libOptions);

$expected = array(
$this->object->getResourceId() => array(
'collection' => 'cache',
'database' => 'zend',
'driverOptions' => array(),
'foo' => 'bar',
'initialized' => false,
'options' => array(
'fsync' => false,
'journal' => true,
),
'server' => 'mongodb://localhost:27017',
)
);
$resourceId = $this->object->getResourceId();
$server = 'mongodb://test:1234';

$this->assertFalse($this->object->getResourceManager()->hasResource($resourceId));

$this->assertAttributeEquals($expected, 'resources', $this->object->getResourceManager());
$this->object->setServer($server);
$this->assertSame($server, $this->object->getResourceManager()->getServer($resourceId));
}
}
Loading

0 comments on commit 9bd872f

Please sign in to comment.