Permalink
Browse files

Fix an inconsistency in the Factory/Bootstrap API with CouchDBClient,…

… HttpClient and DocumentManager dependencies.
  • Loading branch information...
beberlei committed Jun 2, 2011
1 parent 4123765 commit bf6f9d8b2ab38b012b7f6456daba2cbb0ea7daa0
@@ -83,7 +83,7 @@ static public function create(array $options)
}
$connectionClass = self::$clients[$options['type']];
$connection = new $connectionClass($options['host'], $options['port'], $options['user'], $options['password'], $options['ip']);
- if ($optinos['logging'] === true) {
+ if ($options['logging'] === true) {
$connection = new HTTP\LoggingClient($connection);
}
return new self($connection, $options['dbname']);
@@ -99,6 +99,24 @@ public function __construct(Client $client, $databaseName)
$this->databaseName = $databaseName;
}
+ public function setHttpClient(Client $client)
+ {
+ $this->httpClient = $client;
+ }
+
+ /**
+ * @return HttpClient
+ */
+ public function getHttpClient()
+ {
+ return $this->httpClient;
+ }
+
+ public function getDatabase()
+ {
+ return $this->databaseName;
+ }
+
/**
* Let CouchDB generate an array of UUIDs.
*
@@ -61,7 +61,6 @@ class Configuration
'allOrNothingFlush' => true,
'luceneHandlerName' => false,
'metadataResolver' => null,
- 'logging' => false,
);
/**
@@ -103,34 +102,6 @@ public function getValidateDoctrineMetadata()
return $this->attributes['validateDoctrineMetadata'];
}
- /**
- * Sets the HTTP client instance to use for the CouchDB communication
- *
- * @param Client $client
- */
- public function setHttpClient(Client $client)
- {
- $this->attributes['httpclient'] = $client;
- }
-
- /**
- * Gets the HTTP client instance to use for the CouchDB communication
- *
- * @return Client
- */
- public function getHttpClient()
- {
- if (!isset($this->attributes['httpclient'])) {
- if ($this->attributes['logging']) {
- $this->attributes['httpclient'] = new LoggingClient(new SocketClient());
- } else {
- $this->attributes['httpclient'] = new SocketClient();
- }
- }
-
- return $this->attributes['httpclient'];
- }
-
/**
* Adds a namespace under a certain alias.
*
@@ -284,28 +255,6 @@ public function getProxyNamespace()
return $this->attributes['proxyNamespace'];
}
- /**
- * Set the database name
- *
- * @param string $prefix The prefix for names of databases
- */
- public function setDatabase($databaseName)
- {
- $this->attributes['databaseName'] = $databaseName;
- }
-
- /**
- * Get the database name
- *
- *
- * @return string
- */
- public function getDatabase()
- {
- return isset($this->attributes['databaseName']) ?
- $this->attributes['databaseName'] : null;
- }
-
/**
* @param string $name
* @param string $className
@@ -359,12 +308,4 @@ public function getLuceneHandlerName()
return $this->attributes['luceneHandlerName'];
}
-
- public function enableLogging()
- {
- $this->attributes['logging'] = true;
- if (isset($this->attributes['httpclient'])) {
- $this->attributes['httpclient'] = new LoggingClient($this->attributes['httpclient']);
- }
- }
}
@@ -66,8 +66,14 @@ class DocumentManager implements ObjectManager
*/
private $evm;
- public function __construct(Configuration $config = null, EventManager $evm = null)
+ /**
+ * @param CouchDBClient $couchClient
+ * @param Configuration $config
+ * @param EventManager $evm
+ */
+ public function __construct(CouchDBClient $couchClient, Configuration $config = null, EventManager $evm = null)
{
+ $this->couchDBClient = $couchClient;
$this->config = $config ?: new Configuration();
$this->evm = $evm ?: new EventManager();
$this->metadataFactory = new ClassMetadataFactory($this);
@@ -88,9 +94,6 @@ public function getEventManager()
*/
public function getCouchDBClient()
{
- if ($this->couchDBClient === null) {
- $this->couchDBClient = new CouchDBClient($this->getConfiguration()->getHttpClient(), $this->getConfiguration()->getDatabase());
- }
return $this->couchDBClient;
}
@@ -101,9 +104,17 @@ public function getCouchDBClient()
* @param EventManager $evm
* @return DocumentManager
*/
- public static function create(Configuration $config = null, EventManager $evm = null)
+ public static function create($couchParams, Configuration $config = null, EventManager $evm = null)
{
- return new DocumentManager($config, $evm);
+ if (is_array($couchParams)) {
+ $couchClient = CouchDBClient::create($couchParams);
+ } else if ($couchParams instanceof CouchDBClient) {
+ $couchClient = $couchParams;
+ } else {
+ throw new \InvalidArgumentException("Expecting array of instance of CouchDBClient as first argument to DocumentManager::create().");
+ }
+
+ return new DocumentManager($couchClient, $config, $evm);
}
/**
@@ -114,6 +125,16 @@ public function getMetadataFactory()
return $this->metadataFactory;
}
+ public function getHttpClient()
+ {
+ return $this->couchDBClient->getHttpClient();
+ }
+
+ public function getDatabase()
+ {
+ return $this->couchDBClient->getDatabase();
+ }
+
/**
* @return Configuration
*/
@@ -185,7 +206,7 @@ public function createQuery($designDocName, $viewName)
if ($designDoc) {
$designDoc = new $designDoc['className']($designDoc['options']);
}
- $query = new ODMQuery($this->config->getHttpClient(), $this->config->getDatabase(), $designDocName, $viewName, $designDoc);
+ $query = new ODMQuery($this->couchDBClient->getHttpClient(), $this->couchDBClient->getDatabase(), $designDocName, $viewName, $designDoc);
$query->setDocumentManager($this);
return $query;
}
@@ -205,7 +226,7 @@ public function createNativeQuery($designDocName, $viewName)
if ($designDoc) {
$designDoc = new $designDoc['className']($designDoc['options']);
}
- $query = new Query($this->config->getHttpClient(), $this->config->getDatabase(), $designDocName, $viewName, $designDoc);
+ $query = new Query($this->couchDBClient->getHttpClient(), $this->couchDBClient->getDatabase(), $designDocName, $viewName, $designDoc);
return $query;
}
@@ -223,8 +244,8 @@ public function createLuceneQuery($designDocName, $viewName)
if ($designDoc) {
$designDoc = new $designDoc['className']($designDoc['options']);
}
- $query = new ODMLuceneQuery($this->config->getHttpClient(),
- $this->config->getDatabase(), $luceneHandlerName, $designDocName,
+ $query = new ODMLuceneQuery($this->couchDBClient->getHttpClient(),
+ $this->couchDBClient->getDatabase(), $luceneHandlerName, $designDocName,
$viewName, $designDoc
);
$query->setDocumentManager($this);
@@ -265,8 +265,8 @@ private function createDocumentAttachments($documentId, $data)
{
$attachments = array();
- $client = $this->dm->getConfiguration()->getHttpClient();
- $basePath = '/' . $this->dm->getConfiguration()->getDatabase() . '/' . $documentId . '/';
+ $client = $this->dm->getHttpClient();
+ $basePath = '/' . $this->dm->getCouchDBClient()->getDatabase() . '/' . $documentId . '/';
foreach ($data AS $filename => $attachment) {
if (isset($attachment['stub']) && $attachment['stub']) {
$instance = Attachment::createStub($attachment['content_type'], $attachment['length'], $attachment['revpos'], $client, $basePath . $filename);
View
@@ -27,13 +27,13 @@
$metaDriver = new \Doctrine\ODM\CouchDB\Mapping\Driver\AnnotationDriver($reader, $paths);
$config = new \Doctrine\ODM\CouchDB\Configuration();
-$config->setDatabase($database);
$config->setProxyDir(\sys_get_temp_dir());
$config->setMetadataDriverImpl($metaDriver);
-$config->setHttpClient($httpClient);
$config->setLuceneHandlerName('_fti');
-$dm = \Doctrine\ODM\CouchDB\DocumentManager::create($config);
+$couchClient = new \Doctrine\CouchDB\CouchDBClient($httpClient, $database);
+
+$dm = \Doctrine\ODM\CouchDB\DocumentManager::create($couchClient, $config);
ob_start(function($output) {
if (PHP_SAPI != "cli") {
@@ -4,15 +4,6 @@
class ConfigurationTest extends CouchDBTestCase
{
- public function testHttpClient()
- {
- $config = new \Doctrine\ODM\CouchDB\Configuration();
- $httpClient = new \Doctrine\CouchDB\HTTP\SocketClient();
-
- $config->setHttpClient($httpClient);
- $this->assertSame($httpClient, $config->getHttpClient());
- }
-
public function testDocumentNamespace()
{
$config = new \Doctrine\ODM\CouchDB\Configuration();
@@ -41,8 +41,9 @@ public function createCouchDBClient()
public function createDocumentManager()
{
- $database = $this->getTestDatabase();
- $httpClient = $this->getHttpClient();
+ $couchDBClient = $this->createCouchDBClient();
+ $httpClient = $couchDBClient->getHttpClient();
+ $database = $couchDBClient->getDatabase();
$httpClient->request('DELETE', '/' . $database);
$resp = $httpClient->request('PUT', '/' . $database);
@@ -53,13 +54,11 @@ public function createDocumentManager()
$metaDriver = new AnnotationDriver($reader, $paths);
$config = new Configuration();
- $config->setDatabase($database);
$config->setProxyDir(\sys_get_temp_dir());
$config->setMetadataDriverImpl($metaDriver);
$setMetadataCacheImpl = $config->setMetadataCacheImpl(new ArrayCache);
- $config->setHttpClient($httpClient);
$config->setLuceneHandlerName('_fti');
- return DocumentManager::create($config);
+ return DocumentManager::create($couchDBClient, $config);
}
}
@@ -8,25 +8,26 @@ public function testNewInstanceFromConfiguration()
{
$config = new \Doctrine\ODM\CouchDB\Configuration();
$httpClient = new \Doctrine\CouchDB\HTTP\SocketClient();
- $config->setHttpClient($httpClient);
+ $couchClient = new \Doctrine\CouchDB\CouchDBClient($httpClient, "test");
- $dm = \Doctrine\ODM\CouchDB\DocumentManager::create($config);
+ $dm = \Doctrine\ODM\CouchDB\DocumentManager::create($couchClient, $config);
$this->assertInstanceOf('Doctrine\ODM\CouchDB\DocumentManager', $dm);
$this->assertSame($config, $dm->getConfiguration());
- $this->assertSame($httpClient, $dm->getConfiguration()->getHttpClient());
+ $this->assertSame($httpClient, $dm->getHttpClient());
+ $this->assertEquals("test", $dm->getDatabase());
}
public function testGetClassMetadataFactory()
{
- $dm = \Doctrine\ODM\CouchDB\DocumentManager::create();
+ $dm = \Doctrine\ODM\CouchDB\DocumentManager::create(array('dbname' => 'test'));
$this->assertInstanceOf('Doctrine\ODM\CouchDB\Mapping\ClassMetadataFactory', $dm->getClassMetadataFactory());
}
public function testGetClassMetadataFor()
{
- $dm = \Doctrine\ODM\CouchDB\DocumentManager::create();
+ $dm = \Doctrine\ODM\CouchDB\DocumentManager::create(array('dbname' => 'test'));
$cmf = $dm->getClassMetadataFactory();
$cmf->setMetadataFor('stdClass', new \Doctrine\ODM\CouchDB\Mapping\ClassMetadata('stdClass'));
@@ -36,8 +37,8 @@ public function testGetClassMetadataFor()
public function testCreateNewDocumentManagerWithoutHttpClientUsingSocketDefault()
{
- $dm = \Doctrine\ODM\CouchDB\DocumentManager::create();
+ $dm = \Doctrine\ODM\CouchDB\DocumentManager::create(array('dbname' => 'test'));
- $this->assertInstanceOf('Doctrine\CouchDB\HTTP\SocketClient', $dm->getConfiguration()->getHttpClient());
+ $this->assertInstanceOf('Doctrine\CouchDB\HTTP\SocketClient', $dm->getHttpClient());
}
}
@@ -14,7 +14,7 @@ class AttachmentTest extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCa
public function setUp()
{
$this->dm = $this->createDocumentManager();
- $client = $this->dm->getConfiguration()->getHttpClient();
+ $client = $this->dm->getHttpClient();
$response = $client->request('PUT', '/' . $this->getTestDatabase() . '/user_with_attachment', \file_get_contents(__DIR__ . "/_files/user_with_attachment.json"));
}
@@ -16,7 +16,7 @@ public function setUp()
$this->type = 'Doctrine\Tests\ODM\CouchDB\Functional\User';
$this->dm = $this->createDocumentManager();
- $httpClient = $this->dm->getConfiguration()->getHttpClient();
+ $httpClient = $this->dm->getHttpClient();
$data = json_encode(
array(
@@ -25,7 +25,7 @@ public function setUp()
'type' => $this->type,
)
);
- $resp = $httpClient->request('PUT', '/' . $this->dm->getConfiguration()->getDatabase() . '/1', $data);
+ $resp = $httpClient->request('PUT', '/' . $this->dm->getDatabase() . '/1', $data);
$this->assertEquals(201, $resp->status);
}
@@ -160,7 +160,7 @@ public function testNullConversionHandledAutomatically()
public function testKeepTrackOfUnmappedData()
{
- $httpClient = $this->dm->getConfiguration()->getHttpClient();
+ $httpClient = $this->dm->getHttpClient();
$data = array(
'_id' => "2",
@@ -169,7 +169,7 @@ public function testKeepTrackOfUnmappedData()
'address' => array('city' => 'Bonn', 'country' => 'DE'),
'type' => str_replace("\\", ".", $this->type),
);
- $resp = $httpClient->request('PUT', '/' . $this->dm->getConfiguration()->getDatabase() . '/2', json_encode($data));
+ $resp = $httpClient->request('PUT', '/' . $this->dm->getDatabase() . '/2', json_encode($data));
$this->assertEquals(201, $resp->status);
$user = $this->dm->find($this->type, 2);
@@ -179,7 +179,7 @@ public function testKeepTrackOfUnmappedData()
$user->username = 'beberlei2';
$this->dm->flush();
- $resp = $httpClient->request('GET', '/' . $this->dm->getConfiguration()->getDatabase() . '/2');
+ $resp = $httpClient->request('GET', '/' . $this->dm->getDatabase() . '/2');
$this->assertEquals(200, $resp->status);
$data['username'] = 'beberlei2';
@@ -15,7 +15,7 @@ public function setUp()
$this->embeddedType = 'Doctrine\Tests\Models\Embedded\Embedded';
$this->dm = $this->createDocumentManager();
- $httpClient = $this->dm->getConfiguration()->getHttpClient();
+ $httpClient = $this->dm->getHttpClient();
$data = json_encode(
array(
'_id' => "1",
@@ -31,7 +31,7 @@ public function setUp()
),
'type' => str_replace('\\', '.', $this->type)
));
- $resp = $httpClient->request('PUT', '/' . $this->dm->getConfiguration()->getDatabase() . '/1', $data);
+ $resp = $httpClient->request('PUT', '/' . $this->dm->getDatabase() . '/1', $data);
$this->assertEquals(201, $resp->status);
}
Oops, something went wrong.

0 comments on commit bf6f9d8

Please sign in to comment.