Skip to content

Commit 9e627ed

Browse files
committed
Modified Actions API
1 parent a733f75 commit 9e627ed

File tree

7 files changed

+298
-254
lines changed

7 files changed

+298
-254
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
"Chemem\\Fauxton\\": "src/"
2828
},
2929
"files": [
30-
"src/Http/functions.php",
31-
"src/Actions/functions.php"
30+
"src/Http/functions.php"
3231
]
3332
},
3433
"autoload-dev": {

examples/all-docs.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
require __DIR__ . '../vendor/autoload.php';
44

5-
use \Chemem\Fauxton\Actions;
65
use \React\EventLoop\Factory;
6+
use \Chemem\Fauxton\Actions\Action;
77
use \Psr\Http\Message\ResponseInterface;
88

99
$loop = Factory::create();
1010

11-
$docs = Actions\allDocs('your_database')->run($loop)->then(function (ResponseInterface $data) {
12-
echo (string) $data->getBody();
13-
});
11+
$docs = Action::init($loop)->allDocs('your_database')->then(
12+
function (ResponseInterface $response) {
13+
echo $response->getBody();
14+
},
15+
function (\Exception $error) {
16+
echo $error->getMessage();
17+
}
18+
);
1419

1520
$loop->run();

examples/multiple.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
require dirname(__DIR__) . '/vendor/autoload.php';
44

5-
use \Chemem\Fauxton\Actions;
65
use \React\EventLoop\Factory;
76
use function \React\Promise\any;
7+
use \Chemem\Fauxton\Actions\Action;
88
use \Psr\Http\Message\ResponseInterface;
99

1010
const KEYS = array(
@@ -22,13 +22,20 @@
2222

2323
$loop = Factory::create();
2424

25+
$action = Action::init($loop);
26+
2527
$actions = array(
26-
Actions\docKeys('your_database', KEYS, array('include_docs' => 'true'))->run($loop),
27-
Actions\search('your_database', QUERY)->run($loop)
28+
$action->docKeys('your_database', KEYS, array('include_docs' => 'true')),
29+
$action->search('your_database', QUERY)
2830
);
2931

30-
any($actions)->then(function (ResponseInterface $result) {
31-
echo (string) $result->getBody();
32-
});
32+
any($actions)->then(
33+
function (ResponseInterface $result) {
34+
echo (string) $result->getBody();
35+
},
36+
function (\Exception $error) {
37+
echo $error->getMessage();
38+
}
39+
);
3340

3441
$loop->run();

examples/spool.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
require dirname(__DIR__) . '/vendor/autoload.php';
44

55
use \React\EventLoop\Factory;
6-
use \Chemem\Fauxton\Actions;
6+
use \Chemem\Fauxton\Actions\Action;
7+
use \Psr\Http\Message\ResponseInterface;
78

89
$data = '';
910

1011
$loop = Factory::create();
1112

12-
$ret = Actions\uuids(2)->run($loop)->then(function ($result) use (&$data) {
13-
$data .= (string) $result->getBody();
14-
});
13+
$action = Action::init($loop)->uuids(2)->then(
14+
function (ResponseInterface $result) use (&$data) {
15+
$data .= (string) $result->getBody();
16+
},
17+
function (\Exception $error) use (&$data) {
18+
$data .= $error->getMessage();
19+
}
20+
);
1521

1622
$loop->run();
1723

src/Actions/Action.php

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
*
5+
* fauxton-client-supported CouchDB actions
6+
*
7+
* @author Lochemem Bruno Michael
8+
* @license Apache-2.0
9+
*/
10+
11+
namespace Chemem\Fauxton\Actions;
12+
13+
use \Chemem\Fauxton\Http;
14+
use \React\Promise\Promise;
15+
use \Chemem\Bingo\Functional\Algorithms as A;
16+
use function \Chemem\Bingo\Functional\PatternMatching\patternMatch;
17+
18+
class Action
19+
{
20+
const _queryParams = 'Chemem\\Fauxton\\Actions\\Action::_queryParams';
21+
22+
const _resolve = 'Chemem\\Fauxton\\Actions\\Action::_resolve';
23+
24+
const _withDb = 'Chemem\\Fauxton\\Actions\\Action::_withDb';
25+
26+
private $loop;
27+
28+
public function __construct(object $loop)
29+
{
30+
$this->loop = $loop;
31+
}
32+
33+
public static function init(object $loop) : Action
34+
{
35+
return new static($loop);
36+
}
37+
38+
public static function _resolve(object $loop, ...$opts) : Promise
39+
{
40+
return \React\Promise\resolve(Http\_exec($loop, ...$opts));
41+
}
42+
43+
public static function _queryParams(string $action, array $params, array $qParams = array()) : array
44+
{
45+
return array(
46+
$action => A\extend($params, array(
47+
'{params}' => empty($qParams) ? '' : http_build_query($qParams)
48+
))
49+
);
50+
}
51+
52+
public static function _reject(string $message) : Promise
53+
{
54+
return new \React\Promise\Promise(function ($resolve, $reject) use ($message) {
55+
return $reject($message);
56+
});
57+
}
58+
59+
public static function _withDb(object $loop, string $method, string $opt, string $database, array $params = []) : Promise
60+
{
61+
return self::_resolve($loop, $method, array(
62+
$opt => array(
63+
'{db}' => $database
64+
)
65+
), $params);
66+
}
67+
68+
public function uuids(int $count) : Promise
69+
{
70+
return self::_resolve($this->loop, 'get', ['uuids' => ['{count}' => $count]]);
71+
}
72+
73+
public function allDbs() : Promise
74+
{
75+
return self::_resolve($this->loop, 'get', ['allDbs' => []]);
76+
}
77+
78+
public function database(string $database, string $option = 'view') : Promise
79+
{
80+
$action = A\partial(self::_resolve, $this->loop);
81+
$urlOpts = array(
82+
'dbgen' => array(
83+
'{db}' => $database
84+
)
85+
);
86+
87+
return patternMatch(array(
88+
'"create"' => function () use ($action, $urlOpts) {
89+
return $action('put', $urlOpts);
90+
},
91+
'"view"' => function () use ($action, $urlOpts) {
92+
return $action('get', $urlOpts);
93+
},
94+
'_' => function () {
95+
return self::_reject('One of either "create" or "view" is allowed.');
96+
}
97+
), $option);
98+
}
99+
100+
public function allDocs(string $database, array $params = []) : Promise
101+
{
102+
$docs = A\compose(
103+
A\partial(self::_queryParams, 'allDocs', array('{db}' => $database)),
104+
A\partial(self::_resolve, $this->loop, 'get')
105+
);
106+
107+
return $docs($params);
108+
}
109+
110+
public function docKeys(string $database, array $keys, array $params = array()) : Promise
111+
{
112+
$resolve = A\partial(self::_resolve, $this->loop, 'post');
113+
$keys = A\compose(
114+
A\partial(self::_queryParams, 'allDocs', array('{db}' => $database)),
115+
A\partialRight($resolve, isset($keys['keys']) ? $keys : array('keys' => $keys))
116+
);
117+
118+
return $keys($params);
119+
}
120+
121+
public function doc(string $database, string $docId, array $params = array()) : Promise
122+
{
123+
$doc = A\compose(
124+
A\partial(self::_queryParams, 'docById', array(
125+
'{db}' => $database,
126+
'{docId}' => $docId
127+
)),
128+
A\partial(self::_resolve, $this->loop, 'get')
129+
);
130+
131+
return $doc($params);
132+
}
133+
134+
public function search(string $database, array $query) : Promise
135+
{
136+
return self::_withDb($this->loop, 'post', 'search', $database, $query);
137+
}
138+
139+
public function createIndex(string $database, array $params) : Promise
140+
{
141+
return self::_withDb($this->loop, 'post', 'index', $database, $params);
142+
}
143+
144+
public function getIndexes(string $database) : Promise
145+
{
146+
return self::_withDb($this->loop, 'get', 'index', $database);
147+
}
148+
149+
public function insertSingle(string $database, array $data) : Promise
150+
{
151+
return self::_withDb($this->loop, 'post', 'dbgen', $database, $data);
152+
}
153+
154+
public function insertMultiple(string $database, array $data) : Promise
155+
{
156+
return !isset($data['docs']) ?
157+
self::_reject('"docs" key is missing. Schema is {"docs": [{data}]}') :
158+
self::_withDb($this->loop, 'post', 'bulkDocs', $database, $data);
159+
}
160+
161+
public function updateSingle(string $database, string $rev, string $docId, array $update) : Promise
162+
{
163+
return self::_resolve($this->loop, 'put', array(
164+
'deleteDoc' => array(
165+
'{db}' => $database,
166+
'{rev}' => $rev,
167+
'{docId}' => $docId
168+
)
169+
), $update);
170+
}
171+
172+
public function updateMultiple(string $database, array $data) : Promise
173+
{
174+
$check = A\partialRight(A\every, function (array $data) : bool {
175+
return count(A\filter(A\partialRight(A\arrayKeysExist, '_id', '_rev'), $data)) == count($data);
176+
});
177+
178+
return !$check($data) ?
179+
self::_reject('"_rev" and "_id" keys are required for all fields.') :
180+
$this->insertMultiple($database, $data);
181+
}
182+
183+
public function deleteSingle(string $database, string $rev, string $docId) : Promise
184+
{
185+
return self::_resolve($this->loop, 'delete', array(
186+
'deleteDoc' => array(
187+
'{db}' => $database,
188+
'{rev}' => $rev,
189+
'{docId}' => $docId
190+
)
191+
));
192+
}
193+
194+
public function deleteMultiple(string $database, array $data) : Promise
195+
{
196+
$delete = A\compose(A\partial(A\map, function (array $list) {
197+
return !is_array($list) ? $list : A\map(A\partialRight(A\extend, array('_deleted' => true)), $list);
198+
}), A\partial(self::_resolve, $this->loop, 'post', array('bulkdocs' => array('{db}' => $database))));
199+
200+
return isset($data['docs']) ? $delete($data) : self::_reject('"docs" key is missing. Schema is {"docs": [{data}]}');
201+
}
202+
203+
public function changes(string $database, array $params) : Promise
204+
{
205+
$changes = A\compose(
206+
A\partial(self::_queryParams, 'changes', array('{db}' => $database)),
207+
A\partial(self::_resolve, $this->loop, 'get')
208+
);
209+
210+
return $changes($params);
211+
}
212+
213+
public function createDesignDoc(string $database, string $ddoc, array $docData) : Promise
214+
{
215+
return self::_resolve($this->loop, 'put', array(
216+
'ddoc' => array(
217+
'{db}' => $database,
218+
'{ddoc}' => $ddoc
219+
)
220+
), $docData);
221+
}
222+
223+
public function deleteDesignDoc(string $database, string $ddoc) : Promise
224+
{
225+
return self::_resolve($this->loop, 'delete', array(
226+
'ddoc' => array(
227+
'{db}' => $database,
228+
'{ddoc}' => $ddoc
229+
)
230+
));
231+
}
232+
}

0 commit comments

Comments
 (0)