Couch class

Summary

The Couch.php class is the one of the low level class that is used to handle the communication between the high level classes and CouchDB. Before version 2.0.2, the default Http adapter was curl and all the possible adapters where declared into the Couch.php class. With 2.0.2, the library code has been refactored so that the Http adapters are declared into separate classes. The Couch class nowaday use a HttpAdapterInterface to communicate with CouchDB.

Note: The following methods are public methods of the Couch class. Therefore, you will mostly use the high level classes which usually inherit the Couch class. For example, all the following methods will be directly available from the CouchClient class.

API Reference

class PHPOnCouch\Couch

This is the low level class that handles communications with CouchDB.

PHPOnCouch\Couch::dsn()
Returns:The dsn of the current Couch instance

PHPOnCouch\Couch::options()
Returns:The options passed to the Couch instance.

PHPOnCouch\Couch::getSessionCookie()
Returns:The current session cookie. Returns null if not set.

PHPOnCouch\Couch::setSessionCookie($cookie)

Set the current session cookie.

Params string $cookie:
 The cookie to set

PHPOnCouch\Couch::query($method, $url, $parameters = array(), $data = null, $contentType = null)

Send a query to the CouchDB server.

Params string $method:
 The HTTP method to use (GET,PUT,POST,...)
Params string $url:
 The URL to fetch
Params array $parameters:
 The query parameters to pass to the query
Params mixed $data:
 The request body(null by default)
Params string $contentType:
 The content type of the data.
Returns:The server response or false if an error occured.

PHPOnCouch\Couch::continuousQuery($callable, $method, $url, $parameters = array(), $data = null)

Send a query to CouchDB. For each line returned by the server, the $callable will be called. If the callable returns false, the continuousQuery will stop.

Params Function $callable:
 The function called for every document returned.
Params string $method:
 The HTTP method to use (GET,PUT,POST,...)
Params string $url:
 The URL to fetch
Params array $parameters:
 The query parameters to pass to the query
Params mixed $data:
 The request body(null by default)
Params string $contentType:
 The content type of the data.
Returns:The server response or false if an error occured.

PHPOnCouch\Couch::storeFile($url, $file, $contentType)

Make a request with the $file content passed into the request body. The $file must be on the disk.

Params function $callable:
 The function called for every document returned
Params string $method:
 The HTTP method to use (GET,PUT,POST,...)
Params string $url:
 The URL to fetch
Params array $parameters:
 The query parameters to pass to the query
Params mixed $data:
 The request body(null by default)
Params string $contentType:
 The content type of the data.
Returns:The server response or false if an error occured.

PHPOnCouch\Couch::storeAsFile($url, $data, $contentType)

Make a request with the $data passed into the request body.

Params function $callable:
 The function called for every document returned
Params string $method:
 The HTTP method to use (GET,PUT,POST,...)
Params string $url:
 The URL to fetch
Params array $parameters:
 The query parameters to pass to the query
Params mixed $data:
 The request body(null by default)
Params string $contentType:
 The content type of the data.
Returns:The server response or false if an error occured.

PHPOnCouch\Couch::initAdapter($options)

This function is called to initialized the adapter. By default, it will load the cURL adapter. The options passed are the same options passed to the Couch class. It’s must be an array of options. You don’t have to call this method. It will be automatically call when using the Couch class.

Params array $options:
 The options passed to the Couch instance

Example :

$couch = new Couch("http://localhost:5984");
$couch->initAdapter([]) //Set the curl by default

PHPOnCouch\Couch::getAdapter()

This function return the current adapter. If it’s not set, the Couch::initAdapter will be called.

Returns:The Adapter currently used.

Example :

$couch = new PHPOnCouch\Couch("http://localhost:5984");
$adapter = $couch->getAdapter();
$doc =  $adapte->query('GET','db/_all_docs');

PHPOnCouch\Couch::setAdapter(CouchHttpAdapterInterface $adapter)

This function set the current adapter of the Couch class. You must specify a class that implements the CouchHttpAdapterInterface.

Params CouchHttpAdapterInterface $adapter:
 The adapter to set.

You can implemented the following adapters :

  • CouchHttpAdapterSocket
  • CouchHttpAdapterCurl (default)

Note

Even if the CouchHttpAdapter used is Curl, the Socket adapter is still used for the continuous_query function since it is not implemented with cURL.

Example:

use PHPOnCouch\Adapter\CouchHttpAdapterCurl;

$couch = new PHPOnCouch\Couch("http://localhost:5984");
$adapter = new CouchHttpAdapterSocket([]);
$couch->setAdapter($adapter);