1 <?php
2 3 4 5 6 7
8
9 namespace Cross\DB\Drivers;
10
11 use Couchbase\PasswordAuthenticator;
12 use Cross\Exception\CoreException;
13 use CouchbaseCluster;
14 use Exception;
15
16 17 18 19 20
21 class CouchDriver
22 {
23 24 25
26 protected $link;
27
28 29 30 31
32 function __construct(array $params)
33 {
34 if (!class_exists('CouchbaseCluster')) {
35 throw new CoreException('Class CouchbaseCluster not found!');
36 }
37
38 try {
39 $authenticator = new PasswordAuthenticator();
40 $authenticator->username($params['username'])->password($params['password']);
41
42 $cluster = new CouchbaseCluster($params['dsn']);
43 $cluster->authenticate($authenticator);
44
45 $bucket = isset($params['bucket']) ? $params['bucket'] : 'default';
46 if (!empty($params['bucket_password'])) {
47 $this->link = $cluster->openBucket($bucket, $params['bucket_password']);
48 } else {
49 $this->link = $cluster->openBucket($bucket);
50 }
51 } catch (Exception $e) {
52 throw new CoreException ($e->getMessage());
53 }
54 }
55
56 57 58 59 60 61 62 63
64 public function __call($method, $argv)
65 {
66 $result = null;
67 if (method_exists($this->link, $method)) {
68 try {
69 $result = ($argv == null)
70 ? $this->link->$method()
71 : call_user_func_array(array($this->link, $method), $argv);
72 } catch (Exception $e) {
73 throw new CoreException($e->getMessage());
74 }
75 }
76
77 return $result;
78 }
79 }
80