-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBarstool.php
189 lines (160 loc) · 4.43 KB
/
Barstool.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* require the adaptor class
*
* @author Ed Finkler
*/
require_once(
dirname(__FILE__)
.DIRECTORY_SEPARATOR.'Barstool'
.DIRECTORY_SEPARATOR.'Adaptor.php'
);
/**
* Barstool is a simple key/object storage system
* This PHP implementation of Lawnchair is based on
* the JavaScript original by Brian Leroux
* <http://brianleroux.github.com/lawnchair/>
*
* Data is stored as JSON strings inside the storage backend
*/
class Barstool
{
protected $adaptors = array(
'sqlite'=>'Barstool_Adaptor_Sqlite',
'pdo'=>'Barstool_Adaptor_Pdo',
'textfile'=>'Barstool_Adaptor_Textfile'
);
/**
* Constructor
*/
public function __construct($options) {
$this->init($options);
}
/**
*
*/
public function init($options) {
if ($options['adaptor']) {
$this->loadAdaptor($options);
} else {
throw new Exception('missing adaptor from init options');
}
}
/**
* Loads an adaptor
*
* @param string $adaptor
* @return boolean
* @author Ed Finkler
*/
protected function loadAdaptor($options) {
if (array_key_exists($options['adaptor'], $this->adaptors)) {
require_once(
dirname(__FILE__)
.DIRECTORY_SEPARATOR.'Barstool'
.DIRECTORY_SEPARATOR.'Adaptor'
.DIRECTORY_SEPARATOR.ucfirst(strtolower($options['adaptor'])).'.php'
);
$this->adaptor = new $this->adaptors[$options['adaptor']]($options);
return true;
} else {
throw new Exception('invalid adaptor type "'.$options['adaptor'].'" from init options');
return false;
}
}
/**
* save an object to the store
*
* @param mixed $obj
* @param string|function $callback a function to call on success
* @return boolean
* @author Ed Finkler
*/
public function save($obj, $callback=null) {
return $this->adaptor->save($obj, $callback);
}
/**
* get an object from the store, and invoke a callback on it if exists
*
* @param string $key
* @param string|function $callback
* @return void
* @author Ed Finkler
*/
public function get($key, $callback=null) {
return $this->adaptor->get($key, $callback);
}
/**
* returns whether a key exists, directly or to the callback
*
* @param string $key
* @param string|function $callback
* @return boolean
* @author Ed Finkler
*/
public function exists($key, $callback=null) {
return $this->adaptor->exists($key, $callback);
}
/**
* returns all rows, directly or to the callback
*
* @param string|function $callback
* @return array
* @author Ed Finkler
*/
public function all($callback=null) {
return $this->adaptor->all($callback);
}
/**
* Removes a json object from the store
*
* @param string $key
* @param string|function $callback
* @return boolean
* @author Ed Finkler
*/
public function remove($keyOrObj, $callback=null) {
return $this->adaptor->remove($keyOrObj, $callback);
}
/**
* deletes all docs from the store and returns self
*
* @param string|function $callback
* @return Barstool
* @author Ed Finkler
*/
public function nuke($callback=null) {
$this->adaptor->nuke($callback);
return $this;
}
/**
* Iterator that accepts two paramters:
*
* - conditional test for a record
* - callback to invoke on matches
*
* @param string|function $condition
* @param string|function $callback
* @return array|boolean
* @author Ed Finkler
*/
public function find($condition, $callback=null) {
$this->adaptor->find($condition, $callback);
}
/**
* Classic iterator.
* - Passes the record and the index as the second parameter to the callback.
* - Accepts a string for a callback func or a function to be invoked for each document in the collection.
*
* @param string|function $callback
* @return boolean
* @author Ed Finkler
*/
public function each($callback) {
$this->adaptor->each($callback);
}
public function setReturnAssocArray($state) {
$this->adaptor->setReturnAssocArray($state);
}
}
?>