Skip to content

Instantly share code, notes, and snippets.

@MirkoBonadei
Created July 1, 2013 07:14
Show Gist options
  • Save MirkoBonadei/5898913 to your computer and use it in GitHub Desktop.
Save MirkoBonadei/5898913 to your computer and use it in GitHub Desktop.
DriverFactory for PHP MongoDB Driver to manage the upgrade from version < 1.3.0 to version >= 1.3.0
<?php
class DriverFactory
{
private $server;
private $options;
public function __construct($server = 'localhost:27017', array $options = array())
{
$this->server = $server;
$this->options = $options;
}
public function connection()
{
$currentVersion = \Mongo::VERSION;
if (version_compare($currentVersion, '1.3.0') === -1) {
return new \Mongo($this->server, $this->options);
}
$upgradedOptions = $this->validOptionsFromLegacyOptions($this->options);
return new \MongoClient($this->server, $upgradedOptions);
}
private function validOptionsFromLegacyOptions(array $options)
{
// here some logic as remove 'safe' or 'persist' key or
// change the name of deprecated keys
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment