Skip to content

PHP5 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
doc
cover
phpunit.xml.dist
93 changes: 53 additions & 40 deletions Couchbase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
error_reporting(E_ALL | E_STRICT | E_DEPRECATED);

/**
* Couchbase Client
*
Expand All @@ -14,16 +14,6 @@
* @license Apache 2.0, See LICENSE file.
*/

/**
* Require dependent classes
*/
require("Couchbase/CouchDB.php");
require("Couchbase/Internal.php");
require("Couchbase/View.php");
require("Couchbase/ViewDefinition.php");
require("Couchbase/ViewResult.php");
require("Couchbase/ViewResultPaginator.php");

/**
* Exception that gets thrown when the memcached extension is not available
* @package Couchbase
Expand All @@ -45,27 +35,26 @@ class Couchbase_MemcachedNotLoadedException extends Exception {}
*/
class Couchbase extends Memcached
{

/**
* List of queries defined for this bucket/database keyed by `$group` and
* `$name`.
* @var array list of queries
*/
var $queries = array();
protected $queries = null;

/**
* Hostname and port for the CouchDB API inside Couchbase.
*
* @var array
*/
var $query_server = array();
protected $query_server = array();

/**
* Default bucket name for the Couchbase Cluster.
*
* @var string bucket name
*/
var $default_bucket_name = "default";
protected $default_bucket_name = "default";

/**
* Add a server to the connection pool.
Expand All @@ -77,48 +66,45 @@ class Couchbase extends Memcached
* @param $internal_port Couchbase admin API TCP port number.
* @return bool
*/
function addCouchbaseServer($host, $port = 11211, $couchport = 5984,
public function addCouchbaseServer($host, $port = 11211, $couchport = 5984,
/* private*/ $internal_host = null, $internal_port = 8091 /* private end */)
{
if($internal_host === null) {
if(empty($internal_host)) {
$internal_host = $host;
}

$this->query_server = array("host" => $host, "port" => $couchport);
$this->couchdb = new Couchbase_CouchDB("http://$host:$couchport/{$this->default_bucket_name}");
$this->couchbase = new Couchbase_Internal("http://$internal_host:$internal_port/");
$this->_readDesignDocs();
return parent::addServer($host, $port);
}

/**
* Helper method to allow defining a new view programatically.
*
* @param Couchbase_ViewDefinition $view_definition View definition.
* @param Couchbase_View $view_definition View definition.
* @return bool
*/
function addView($view_definition)
public function addView(Couchbase_View $view_definition)
{
$this->_readDesignDocs();

$this->queries[$view_definition->ddoc_name]
[$view_definition->name] = $view_definition;
$this->_updateDesignDocument($view_definition->ddoc_name);
$this->_waitForDesignDocUglyHack($view_definition->ddoc_name);
return true;
}

// wait for ddocs to be all synced to all buckets and whatnot
// the server should do the wait for me or send me a notification
function _waitForDesignDocUglyHack($ddoc_name)
/**
* @todo: Remove this hack?
*
* wait for ddocs to be all synced to all buckets and whatnot
* the server should do the wait for me or send me a notification
*/
private function _waitForDesignDocUglyHack($ddoc_name)
{
// var_dump("--waitForDdoc");
sleep(4);
// do {
// usleep(300);
// $result = $this->couchdb->view("default", $ddoc_name);
// var_dump($result);
// $json_result = json_decode($result);
// } while(isset($json_result->error) && ($json_result->error == "not_found"));
// var_dump("--done waitForDdoc");
}

/**
Expand All @@ -129,14 +115,16 @@ function _waitForDesignDocUglyHack($ddoc_name)
* @param $view_name Name of the view inside the design doc
* @return Couchbase_View instance ready for querying
*/
function getView($ddoc_name, $view_name)
public function getView($ddoc_name, $view_name)
{
$this->_readDesignDocs();

if(!isset($this->queries[$ddoc_name][$view_name])) {
return false;
}

$view = $this->queries[$ddoc_name][$view_name];
$view->db = $this;
$view->setDatabase($this);
return $view;
}

Expand All @@ -145,11 +133,9 @@ function getView($ddoc_name, $view_name)
*
* @return Couchbase_AllDocsView
*/
function getAllDocsView()
public function getAllDocsView()
{
$allDocsView = new Couchbase_AllDocsView;
$allDocsView->db = $this;
return $allDocsView;
return new Couchbase_AllDocsView($this);
}

/**
Expand All @@ -160,7 +146,7 @@ function getAllDocsView()
* @param integer $expriy Number of seconds until the item expires.
* @return boolean Success or not.
*/
function touch($key, $expriy = 0)
public function touch($key, $expriy = 0)
{
if(!method_exists("Memcached", "touch")) {
trigger_error(E_WARNING,
Expand All @@ -182,8 +168,12 @@ function touch($key, $expriy = 0)
* all the time.
* @return void
*/
function _readDesignDocs()
private function _readDesignDocs()
{
if ($this->queries !== null) {
return;
}

if(!$this->couchbase->bucketExists($this->default_bucket_name)) {
return;
}
Expand All @@ -196,6 +186,7 @@ function _readDesignDocs()
return;
}

$this->queries = array();
foreach($ddocs->rows AS $ddoc_row) {
$ddoc = $ddoc_row->doc;
$ddoc_name = str_replace("_design/", "", $ddoc->_id);
Expand All @@ -214,7 +205,7 @@ function _readDesignDocs()
* @param string $ddoc_name design doc to update.
* @return void
*/
function _updateDesignDocument($ddoc_name)
private function _updateDesignDocument($ddoc_name)
{
$ddoc_definition = $this->queries[$ddoc_name];
$ddoc = new stdClass;
Expand Down Expand Up @@ -246,4 +237,26 @@ function _updateDesignDocument($ddoc_name)
$ddoc_json = json_encode($ddoc);
$this->couchdb->saveDoc($ddoc_json);
}

/**
* Register Autoloader for Couchbase PHP SDK
*
* @return void
*/
static public function registerAutoload()
{
spl_autoload_register(array("Couchbase", "autoload"));
}

/**
* Autoloader function
*
* @param string $class
*/
static public function autoload($class)
{
if (strpos($class, "Couchbase") === 0) {
require_once dirname(__FILE__) . "/" . str_replace("_", "/", $class) . ".php";
}
}
}
34 changes: 34 additions & 0 deletions Couchbase/AllDocsView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Define a Couchbase query.
*
* @package Couchbase
* @license Apache 2.0
*/

class Couchbase_AllDocsView extends Couchbase_View
{
/**
* Constructor, fake ddoc and view names
*/
public function __construct(Couchbase $db)
{
parent::__construct("_builtin", "_all_docs", $db);
}

/**
* Return a Couchbase query result.
*
* Overrides the parent's method to query `_all_docs` instead of a custom
* view.
*
* @param array $options Optional associative array of view options.
* @return Couchbase_ViewResult
*/
public function getResult($options = array())
{
return new Couchbase_ViewResult(
$this->db->couchdb->allDocs($options)
);
}
}
27 changes: 15 additions & 12 deletions Couchbase/CouchDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
*/
class Couchbase_CouchDB
{

/**
* CouchDB server url, parsed into an object.
*
* @var URL CouchDB Server URL
*/
var $server = null;
protected $server = null;

/**
* Constructor, takes a URL spcifying the CouchDB server and database.
*
* @param string $dsn URL to a CouchDB server and database
* @example http://localhost:5984/database
*/
function __construct($dsn)
public function __construct($dsn)
{
$this->server = new stdClass;
$this->dsn = $dsn;
Expand All @@ -35,7 +34,7 @@ function __construct($dsn)
* @param string $name database name, must match [a-z][a-z0-9$()/_-].
* @return string JSON success or error message.
*/
function createDb($name)
public function createDb($name)
{
return $this->send("PUT", $this->server->path);
}
Expand All @@ -46,7 +45,7 @@ function createDb($name)
* @param string $name database name.
* @return string JSON success or error message.
*/
function deleteDb($name)
public function deleteDb($name)
{
return $this->send("DELETE", $this->server->path);
}
Expand All @@ -57,7 +56,7 @@ function deleteDb($name)
* @param string $doc JSON representation of a document.
* @return string JSON success or error message.
*/
function saveDoc($doc)
public function saveDoc($doc)
{
return $this->send("POST", $this->server->path, $doc);
}
Expand All @@ -69,7 +68,7 @@ function saveDoc($doc)
* @param string $id The documents's id.
* @return string document or error message as a JSON string.
*/
function open($id)
public function open($id)
{
return $this->send("GET", $this->server->path . "/$id");
}
Expand All @@ -82,7 +81,7 @@ function open($id)
* @param string $options Associative array of CouchDB view query options.
* @return string JSON result set of a CouchDB view Query.
*/
function view($group, $name, $options = array())
public function view($group, $name, $options = array())
{
$qs = $this->_options_to_query_string($options);
return $this->send("GET",
Expand All @@ -94,7 +93,7 @@ function view($group, $name, $options = array())
*
* @param string $options Associative array of CouchDB view query options.
*/
function allDocs($options)
public function allDocs($options)
{
$qs = $this->_options_to_query_string($options);
return $this->send("GET", $this->server->path . "/_all_docs?$qs");
Expand All @@ -107,7 +106,7 @@ function allDocs($options)
* @param string $options Associative array of CouchDBview query options.
* @return string URL query string to be appended to a view query.
*/
function _options_to_query_string($options)
private function _options_to_query_string($options)
{
// TODO: keys POST
$qs = array();
Expand Down Expand Up @@ -156,7 +155,7 @@ function _options_to_query_string($options)
* application/json.
* @return string JSON response.
*/
function send($method, $url, $post_data = NULL, $content_type = "application/json")
protected function send($method, $url, $post_data = NULL, $content_type = "application/json")
{
$s = fsockopen(
$this->server->host,
Expand Down Expand Up @@ -187,7 +186,10 @@ function send($method, $url, $post_data = NULL, $content_type = "application/jso
// var_dump($request);
// var_dump("-------------");
fwrite($s, $request);
$response = "";
$response = "";

while ( ( ( $line = fgets( $s ) ) !== false ) &&
( ( $lineContent = rtrim( $line ) ) === '' ) );

while(!feof($s)) {
$response .= fgets($s);
Expand All @@ -205,6 +207,7 @@ function send($method, $url, $post_data = NULL, $content_type = "application/jso
}
// var_dump($response);
// var_dump("--------------------------------");

return $this->body;
}
}
Loading