Browse files

Add factory method for CouchDBClient

  • Loading branch information...
1 parent 1416cd2 commit 41237657abb4d7ffe7cf904962f6e29a02e9f0be @beberlei beberlei committed Jun 2, 2011
Showing with 33 additions and 0 deletions.
  1. +33 −0 lib/Doctrine/CouchDB/CouchDBClient.php
View
33 lib/Doctrine/CouchDB/CouchDBClient.php
@@ -56,6 +56,39 @@ class CouchDBClient
*/
private $version = null;
+ static private $clients = array(
+ 'socket' => 'Doctrine\CouchDB\HTTP\SocketClient',
+ 'stream' => 'Doctrine\CouchDB\HtTP\StreamClient',
+ );
+
+ /**
+ * Factory method for CouchDBClients
+ *
+ * @param array $options
+ * @return CouchDBClient
+ */
+ static public function create(array $options)
+ {
+ if (!isset($options['dbname'])) {
+ throw new \InvalidArgumentException("'dbname' is a required option to create a CouchDBClient");
+ }
+
+ $defaults = array('type' => 'socket', 'host' => 'localhost', 'port' => 5984, 'user' => null, 'password' => null, 'ip' => null, 'logging' => false);
+ $options = array_merge($defaults, $options);
+
+ if (!isset(self::$clients[$options['type']])) {
+ throw new \InvalidArgumentException(sprintf('There is no client implementation registered for %s, valid options are %s',
+ $options['type'], array_keys(self::$clients)
+ ));
+ }
+ $connectionClass = self::$clients[$options['type']];
+ $connection = new $connectionClass($options['host'], $options['port'], $options['user'], $options['password'], $options['ip']);
+ if ($optinos['logging'] === true) {
+ $connection = new HTTP\LoggingClient($connection);
+ }
+ return new self($connection, $options['dbname']);
+ }
+
/**
* @param Client $client
* @param string $databaseName

0 comments on commit 4123765

Please sign in to comment.