Skip to content

Commit 7582134

Browse files
committed
Implementing Acquire, Release, and CAS endpionts for KV
- "KVClient::put" bugfix
1 parent 313e9c5 commit 7582134

File tree

3 files changed

+119
-34
lines changed

3 files changed

+119
-34
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Require Entry:
1212

1313
```json
1414
{
15-
"dcarbone/php-consul-api": "0.2.*"
15+
"dcarbone/php-consul-api": "0.3.*"
1616
}
1717
```
1818

src/HttpRequest.php

+46-29
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class HttpRequest
3737
/** @var string */
3838
public $url;
3939

40+
/** @var string */
41+
private $_compiledBody = null;
42+
4043
/** @var array */
4144
private $_curlOpts = array();
4245
/** @var array */
@@ -124,6 +127,41 @@ public function setWriteOptions(WriteOptions $writeOptions = null)
124127
$this->params['token'] = $token;
125128
}
126129

130+
/**
131+
* @return string
132+
*/
133+
public function getCompiledBody()
134+
{
135+
if (!isset($this->_compiledBody))
136+
{
137+
switch(gettype($this->body))
138+
{
139+
case 'integer':
140+
case 'double':
141+
$this->_compiledBody = (string)$this->body;
142+
break;
143+
144+
case 'string':
145+
$this->_compiledBody = $this->body;
146+
break;
147+
148+
case 'object':
149+
case 'array':
150+
$this->_compiledBody = json_encode($this->body);
151+
break;
152+
153+
case 'boolean':
154+
$this->_compiledBody = $this->body ? 'true' : 'false';
155+
break;
156+
157+
default:
158+
$this->_compiledBody = '';
159+
}
160+
}
161+
162+
return $this->_compiledBody;
163+
}
164+
127165
/**
128166
* @return array(
129167
* @type HttpResponse|null response or null on error
@@ -150,7 +188,12 @@ public function execute()
150188
);
151189
}
152190

153-
Logger::debug('Executing '.$this->method.' request '.$this->url.($this->body ? ' with body "'.$this->body.'"':''));
191+
Logger::debug(sprintf(
192+
'Executing %s request %s%s',
193+
$this->method,
194+
$this->url,
195+
$this->body ? sprintf(' with body "%s"', $this->getCompiledBody()) : ''
196+
));
154197

155198
switch($this->method)
156199
{
@@ -208,38 +251,12 @@ public function execute()
208251
private function _preparePUT()
209252
{
210253
$this->_curlOpts[CURLOPT_CUSTOMREQUEST] = 'PUT';
211-
$this->_curlOpts[CURLOPT_POSTFIELDS] = $this->_compileBody();
254+
$this->_curlOpts[CURLOPT_POSTFIELDS] = $this->getCompiledBody();
212255
}
213256

214257
private function _prepareDELETE()
215258
{
216259
$this->_curlOpts[CURLOPT_CUSTOMREQUEST] = 'DELETE';
217-
$this->_curlOpts[CURLOPT_POSTFIELDS] = $this->_compileBody();
218-
}
219-
220-
/**
221-
* @return string
222-
*/
223-
private function _compileBody()
224-
{
225-
switch(gettype($this->body))
226-
{
227-
case 'integer':
228-
case 'double':
229-
return (string)$this->body;
230-
231-
case 'string':
232-
return $this->body;
233-
234-
case 'object':
235-
case 'array':
236-
return json_encode($this->body);
237-
238-
case 'boolean':
239-
return $this->body ? 'true' : 'false';
240-
241-
default:
242-
return '';
243-
}
260+
$this->_curlOpts[CURLOPT_POSTFIELDS] = $this->getCompiledBody();
244261
}
245262
}

src/KV/KVClient.php

+72-4
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,20 @@ public function keys($prefix = null, QueryOptions $queryOptions = null)
162162
}
163163

164164
/**
165-
* @param KVPair $KVPair
165+
* @param KVPair $p
166166
* @param WriteOptions $writeOptions
167167
* @return array(
168168
* @type \DCarbone\PHPConsulAPI\WriteMeta write metadata
169169
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
170170
* )
171171
*/
172-
public function put(KVPair $KVPair, WriteOptions $writeOptions = null)
172+
public function put(KVPair $p, WriteOptions $writeOptions = null)
173173
{
174-
$r = new HttpRequest('put', sprintf('v1/kv/%s', $KVPair->Key), $this->_Config);
174+
$r = new HttpRequest('put', sprintf('v1/kv/%s', $p->Key), $this->_Config);
175175
$r->setWriteOptions($writeOptions);
176-
$r->body = ($KVPair);
176+
$r->body = $p->Value;
177+
if (0 !== $p->Flags)
178+
$r->params->set('flags', $p->Flags);
177179

178180
list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
179181
$wm = $this->buildWriteMeta($duration);
@@ -200,6 +202,72 @@ public function delete($key, WriteOptions $writeOptions = null)
200202
return [$wm, $err];
201203
}
202204

205+
/**
206+
* @param KVPair $p
207+
* @param WriteOptions $writeOptions
208+
* @return array(
209+
* @type \DCarbone\PHPConsulAPI\WriteMeta write metadata
210+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
211+
* )
212+
*/
213+
public function cas(KVPair $p, WriteOptions $writeOptions = null)
214+
{
215+
$r = new HttpRequest('put', sprintf('v1/kv/%s', $p->Key), $this->_Config);
216+
$r->setWriteOptions($writeOptions);
217+
$r->params->set('cas', $p->ModifyIndex);
218+
if (0 !== $p->Flags)
219+
$r->params->set('flags', $p->Flags);
220+
221+
list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
222+
$wm = $this->buildWriteMeta($duration);
223+
224+
return [$wm, $err];
225+
}
226+
227+
/**
228+
* @param KVPair $p
229+
* @param WriteOptions $writeOptions
230+
* @return array(
231+
* @type \DCarbone\PHPConsulAPI\WriteMeta write metadata
232+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
233+
* )
234+
*/
235+
public function acquire(KVPair $p, WriteOptions $writeOptions = null)
236+
{
237+
$r = new HttpRequest('put', sprintf('v1/kv/%s', $p->Key), $this->_Config);
238+
$r->setWriteOptions($writeOptions);
239+
$r->params->set('acquire', $p->Session);
240+
if (0 !== $p->Flags)
241+
$r->params->set('flags', $p->Flags);
242+
243+
list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
244+
$wm = $this->buildWriteMeta($duration);
245+
246+
return [$wm, $err];
247+
}
248+
249+
/**
250+
* @param KVPair $p
251+
* @param WriteOptions $writeOptions
252+
* @return array(
253+
* @type \DCarbone\PHPConsulAPI\WriteMeta write metadata
254+
* @type \DCarbone\PHPConsulAPI\Error|null error, if any
255+
* )
256+
*/
257+
public function release(KVPair $p, WriteOptions $writeOptions = null)
258+
{
259+
$r = new HttpRequest('put', sprintf('v1/kv/%s', $p->Key), $this->_Config);
260+
$r->setWriteOptions($writeOptions);
261+
$r->params->set('release', $p->Session);
262+
if (0 !== $p->Flags)
263+
$r->params->set('flags', $p->Flags);
264+
265+
list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
266+
$wm = $this->buildWriteMeta($duration);
267+
268+
return [$wm, $err];
269+
}
270+
203271
/**
204272
* @param null|string $prefix
205273
* @param QueryOptions $queryOptions

0 commit comments

Comments
 (0)