PageRenderTime 255ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/BF/Coucher/Client.php

https://bitbucket.org/bytefreak/php-coucher
PHP | 294 lines | 246 code | 45 blank | 3 comment | 50 complexity | 054837c908957d05d689c9deac0a074c MD5 | raw file
  1. <?php
  2. namespace BF\Coucher;
  3. class Client
  4. {
  5. /**
  6. * @var \BF\Coucher\Connection
  7. */
  8. protected $connection;
  9. protected $selectedDB = null;
  10. public function setLogRequests($logRequests=true)
  11. {
  12. $this->connection->logRequests = $logRequests;
  13. }
  14. public function getLoggedRequests()
  15. {
  16. return $this->connection->loggedRequests;
  17. }
  18. public function __construct($baseurl)
  19. {
  20. $this->connection = new Connection($baseurl);
  21. }
  22. public function getRequestCount()
  23. {
  24. return $this->connection->requestsSent;
  25. }
  26. public function connect($username=null,$password=null)
  27. {
  28. $this->connection->connect($username,$password);
  29. }
  30. public function close()
  31. {
  32. $this->connection->close();
  33. }
  34. public function getSession()
  35. {
  36. $r = $this->connection->sendRequest(Connection::METHOD_GET, "/_session");
  37. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  38. return $r->body;
  39. }
  40. public function getVersion()
  41. {
  42. $r = $this->connection->sendRequest(Connection::METHOD_GET, "/");
  43. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  44. return $r->body['version'];
  45. }
  46. public function getDatabases()
  47. {
  48. $r = $this->connection->sendRequest(Connection::METHOD_GET,"/_all_dbs");
  49. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  50. return $r->body;
  51. }
  52. public function getUUID()
  53. {
  54. $r = $this->connection->sendRequest(Connection::METHOD_GET,"/_uuids");
  55. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  56. return $r->body['uuids'][0];
  57. }
  58. public function getUUIDs($cnt)
  59. {
  60. $r = $this->connection->sendRequest(Connection::METHOD_GET,"/_uuids",array("count"=>$cnt));
  61. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  62. return $r->body['uuids'];
  63. }
  64. public function getStatus()
  65. {
  66. $r = $this->connection->sendRequest(Connection::METHOD_GET,"/_stats");
  67. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  68. return $r->body;
  69. }
  70. public function getConfig($section=null,$key=null)
  71. {
  72. $path = "/";
  73. if (!is_null($section)) {
  74. $path .= $section;
  75. if (!is_null($key)) $path .= '/'.$key;
  76. }
  77. $r = $this->connection->sendRequest(Connection::METHOD_GET,"/_config$path");
  78. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  79. return $r->body;
  80. }
  81. public function selectDatabase($db)
  82. {
  83. $this->selectedDB = $db;
  84. }
  85. public function getDatabaseInformations()
  86. {
  87. if (is_null($this->selectedDB)) throw new \Exception("no database selected!");
  88. $r = $this->connection->sendRequest(Connection::METHOD_GET,"/".$this->selectedDB);
  89. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  90. return $r->body;
  91. }
  92. public function createDatabase($name)
  93. {
  94. $r = $this->connection->sendRequest(Connection::METHOD_PUT, "/$name");
  95. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  96. return $r->body;
  97. }
  98. public function dropDatabase($name)
  99. {
  100. $r = $this->connection->sendRequest(Connection::METHOD_DELETE, "/$name");
  101. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  102. return $r->body;
  103. }
  104. public function getDocument($docId)
  105. {
  106. if (is_null($this->selectedDB)) throw new \Exception("no database selected!");
  107. $r = $this->connection->sendRequest(Connection::METHOD_GET, "/".$this->selectedDB."/".$docId);
  108. if (isset($r->body['error'])) return null;
  109. return $r->body;
  110. }
  111. public function bulkSaveDocuments(&$docs)
  112. {
  113. if (is_null($this->selectedDB)) throw new \Exception("no database selected!");
  114. $payload = json_encode(array("docs" => array_values($docs)));
  115. $r = $this->connection->sendRequest(Connection::METHOD_POST,"/".$this->selectedDB."/_bulk_docs",$payload);
  116. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  117. $keys = array_keys($docs);
  118. foreach ($r->body as $k => $docResponse) {
  119. if (is_object($docs[$keys[$k]])) {
  120. $docs[$keys[$k]]->_id = $docResponse["id"];
  121. $docs[$keys[$k]]->_rev = $docResponse["rev"];
  122. }else{
  123. $docs[$keys[$k]]["_id"] = $docResponse["id"];
  124. $docs[$keys[$k]]["_rev"] = $docResponse["rev"];
  125. }
  126. }
  127. return $r->body;
  128. }
  129. public function createDocument(&$document)
  130. {
  131. if (is_null($this->selectedDB)) throw new \Exception("no database selected!");
  132. $payload = $document;
  133. if (is_array($payload)) $payload = json_encode($payload);
  134. $r = $this->connection->sendRequest(Connection::METHOD_POST,"/".$this->selectedDB,$payload);
  135. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  136. $document["_id"] = $r->body['id'];
  137. $document["_rev"] = $r->body['rev'];
  138. return $r->body;
  139. }
  140. public function saveDocument($id,&$document)
  141. {
  142. if (is_null($this->selectedDB)) throw new \Exception("no database selected!");
  143. $payload = $document;
  144. if (is_array($payload) || is_object($payload)) $payload = json_encode($payload);
  145. $r = $this->connection->sendRequest(Connection::METHOD_PUT,"/".$this->selectedDB."/".$id,$payload);
  146. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  147. if (is_array($document)) {
  148. $document["_id"] = $r->body['id'];
  149. $document["_rev"] = $r->body['rev'];
  150. }elseif (is_object($document)){
  151. $document->_id = $r->body['id'];
  152. $document->_rev = $r->body['rev'];
  153. }
  154. return $r->body;
  155. }
  156. public function removeDocument($docOrId, $rev=null)
  157. {
  158. if (is_null($this->selectedDB)) throw new \Exception("no database selected!");
  159. if (is_object($docOrId)) $docOrId = (array) $docOrId;
  160. if (is_array($docOrId)) {
  161. $rev = $docOrId['_rev'];
  162. $docOrId = $docOrId["_id"];
  163. }
  164. if (is_null($rev)) throw new \Exception("Revision not provided as second parameter!");
  165. $r = $this->connection->sendRequest(Connection::METHOD_DELETE,'/'.$this->selectedDB.'/'.$docOrId, array("rev"=>$rev));
  166. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  167. return $r->body;
  168. }
  169. public function getAllDocuments($includeDocContent = true)
  170. {
  171. if (is_null($this->selectedDB)) throw new \Exception("no database selected!");
  172. return $this->performViewRequest('/'.$this->selectedDB.'/_all_docs',$includeDocContent);
  173. }
  174. public function getDocuments($keys,$includeDocContent=true,$options=array())
  175. {
  176. if (is_null($this->selectedDB)) throw new \Exception("no database selected!");
  177. $path = "/".$this->selectedDB."/_all_docs";
  178. $view = new View($this,$path,$keys,null,null,null,$includeDocContent,$options);
  179. return $view;
  180. }
  181. public function getView($designDoc,$viewname,$keys=null,$startkey=null,$endkey=null,$limit=null,$includeDocContent = true,$options = array())
  182. {
  183. if (is_null($this->selectedDB)) throw new \Exception("no database selected!");
  184. $path = "/".$this->selectedDB."/_design/$designDoc/_view/$viewname";
  185. $view = new View($this,$path,$keys,$startkey,$endkey,$limit,$includeDocContent,$options);
  186. return $view;
  187. }
  188. public function performViewRequest($path,$keys=null,$startkey=null,$endkey=null,$limit=null,$includeDocContent = true, $options = array())
  189. {
  190. $params = array();
  191. $params["include_docs"] = ($includeDocContent ? 'true' : 'false');
  192. if (!is_null($keys)) {
  193. if (!is_array($keys)) throw new \Exception("keys is kein array!");
  194. $params["keys"] = $keys;
  195. }
  196. if (!is_null($startkey)) $params["startkey"] = rawurlencode(json_encode($startkey));
  197. if (!is_null($endkey)) $params['endkey'] = rawurlencode(json_encode($endkey));
  198. if (!is_null($limit)) $params ['limit'] = (int)$limit;
  199. if (isset($options["startkey_docid"])) $params["startkey_docid"] = rawurlencode($options["startkey_docid"]);
  200. unset($options["startkey_docid"]);
  201. foreach ($options as $k => $v) {
  202. $params[$k] = rawurlencode(json_encode($v));
  203. }
  204. $keyData = array();
  205. if (isset($params["keys"])) {
  206. $keyData["keys"] = $params["keys"];
  207. unset($params["keys"]);
  208. }
  209. $qs = array();
  210. foreach ($params as $k => $v) {
  211. if (!is_null($v)) {
  212. $qs[] = "$k=$v";
  213. }
  214. }
  215. $qs = implode("&",$qs);
  216. $path .= '?'.$qs;
  217. $r = $this->connection->sendRequest(count($keyData) ? Connection::METHOD_POST : Connection::METHOD_GET,$path,count($keyData) ? json_encode($keyData) : array(),"application/json");
  218. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  219. return $r->body;
  220. }
  221. public function addAttachment($document,$name,$mimetype,$data)
  222. {
  223. if (is_null($this->selectedDB)) throw new \Exception("no database selected!");
  224. if (is_array($document)) $document = (object) $document;
  225. $docId = $document->_id;
  226. $docRev = $document->_rev;
  227. $path = "/".$this->selectedDB."/$docId/$name?rev=$docRev";
  228. $r = $this->connection->sendRequest(Connection::METHOD_PUT, $path,$data,$mimetype,array("Content-Length"=>strlen($data)));
  229. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  230. return $r->body;
  231. }
  232. public function callUpdateHandler($designDoc,$handler,$params,$docOrDocId=null)
  233. {
  234. if (is_null($this->selectedDB)) throw new \Exception("no database selected!");
  235. $path = "/".$this->selectedDB."/_design/".$designDoc."/_update/".$handler;
  236. $method = null;
  237. if ($docOrDocId) {
  238. $method = Connection::METHOD_PUT;
  239. if (is_array($docOrDocId)) $docOrDocId = (object)$docOrDocId;
  240. $docId = (is_object($docOrDocId)) ? $docOrDocId->_id : $docOrDocId;
  241. $path .= "/$docId";
  242. }else{
  243. $method = Connection::METHOD_POST;
  244. }
  245. $r = $this->connection->sendRequest(Connection::METHOD_POST, $path, $params);
  246. if (isset($r->body['error'])) throw new \Exception($r->body['error'].": ".$r->body['reason']);
  247. return $r->body;
  248. }
  249. }