Skip to content

Commit b3bbf9a

Browse files
committed
Documentation updates
1 parent 152c9b0 commit b3bbf9a

File tree

3 files changed

+176
-20
lines changed

3 files changed

+176
-20
lines changed

README.md

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# php-consul-api
22

3-
PHP client implementation for the Consul API
4-
5-
The primary purpose of this lib is to provide a dependency-free way of interacting with [Consul](https://www.consul.io/).
6-
7-
It is in alpha stages of development.
3+
PHP client implementation for the [Consul API](https://www.consul.io/docs/agent/http.html)
84

95
This library is loosely based upon the [official GO client](https://github.com/hashicorp/consul/tree/master/api).
106

@@ -20,28 +16,41 @@ Require Entry:
2016
}
2117
```
2218

23-
## KV
19+
## Usage
20+
21+
First, construct a [Config](./src/Config.php) object:
22+
23+
```php
24+
$config = new \DCarbone\PHPConsulAPI\Config(['Address' => 'address of your consul agent']);
25+
```
26+
27+
Next, construct a [Client](./src/Client.php) object:
2428

25-
All interactions with the `v1/kv` endpoint are done via the [KVClient](./src/KV/KVClient.php) class.
29+
```php
30+
$client = new \DCarbone\PHPConsulAPI\Client($config);
31+
```
2632

27-
Some basic examples:
33+
Once constructed, you interact with each Consul API via it's corresponding Client class:
2834

2935
```php
30-
use DCarbone\SimpleConsulPHP\Client;
31-
use DCarbone\SimpleConsulPHP\KV\KVPair;
36+
$kv_list = $client->KV()->list();
37+
var_dump($kv_list);
38+
```
3239

33-
$client = new Client();
40+
...as an example.
3441

35-
list($wm, $err) = $client->KV()->put(new KVPair(['Key' => 'prefix/mykey', 'Value' => 'my value']));
42+
## Current Clients
3643

37-
if (null !== $err)
38-
die('bad things happened');
44+
- [KV](./docs/KV.md)
45+
- [Agent](./docs/AGENT.md)
46+
- [Catalog](./docs/CATALOG.md)
47+
- [Status](./docs/STATUS.md)
3948

40-
list($kv, $qm, $err) = $client->KV()->get('prefix/mykey');
49+
More will be added as time goes on!
4150

42-
if (null !== $err))
43-
die('could not retrieve key!');
4451

45-
var_dump($kv);
52+
## TODO
4653

47-
```
54+
- Tests
55+
- Parity with GO lib
56+
- Code cleanup

docs/KV.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# PHP Consul API KV
2+
3+
All interactions with the [`v1/kv`](https://www.consul.io/docs/agent/http/kv.html) endpoint are done
4+
via the [KVClient](./src/KV/KVClient.php) class.
5+
6+
If you have constructed a [Client](./src/Client.php) object, this is done as so:
7+
8+
```php
9+
/** @var \DCarbone\PHPConsulAPI\KV\KVClient $kv */
10+
$kv = $client->KV();
11+
```
12+
13+
## Actions
14+
15+
### Key Getting
16+
17+
```php
18+
list($kvp, $qm, $err) = $client->KV()->get('prefix/keyname');
19+
if (null !== $err)
20+
die($err);
21+
22+
var_dump($qm, $kvp);
23+
```
24+
25+
### Key Listing
26+
27+
28+
*All Keys*:
29+
```php
30+
list($keys, $qm, $err) = $client->KV()->keys();
31+
if (null !== $err)
32+
die($err);
33+
34+
var_dump($qm, $keys);
35+
```
36+
37+
*Only Keys with Specific Prefix*
38+
```php
39+
list($keys, $qm, $err) = $client->KV()->keys('prefix');
40+
if (null !== $err)
41+
die($err);
42+
43+
var_dump($qm, $keys);
44+
```
45+
46+
### Key Putting
47+
48+
```php
49+
$kvp = new KVPair(['Key' => 'prefix/mykey', 'Value' => 'my value']);
50+
list($wm, $err) = $client->KV()->put($kvp);
51+
if (null !== $err)
52+
die($err);
53+
54+
var_dump($wm);
55+
```
56+
57+
### Key Deletion
58+
59+
```php
60+
list($wm, $err) = $client->KV()->delete('prefix/mykey');
61+
if (null !== $err)
62+
die($err);
63+
var_dump($wm);
64+
65+
```
66+
67+
### Tree Retrieval
68+
69+
This is a custom feature to allow `get-tree`-like functionality with 0.6.4 (the current release as of this writing)
70+
71+
This call executes a `$client->KV()->keys()` call, parses the results, and builds an object tree of
72+
[KVTree](./src/KV/KVTree.php) and [KVPair](./src/KV/KVPair.php) objects.
73+
74+
*Entire Tree - POTENTIALLY VERY SLOW!!!!!!*
75+
```php
76+
list($tree, $err) = $client->KV()->tree();
77+
if (null !== $err)
78+
die($err);
79+
80+
```
81+
82+
*Only Tree's under Specific Root Prefix*
83+
84+
```php
85+
list($tree, $err) = $client->KV()->tree('prefix');
86+
if (null !== $err)
87+
die($err);
88+
```
89+
90+
You may use the below little helper script to format the output for testing
91+
92+
```php
93+
function output_tree(\DCarbone\PHPConsulAPI\KV\KVTree $tree)
94+
{
95+
foreach($tree as $item)
96+
{
97+
if ($item instanceof \DCarbone\PHPConsulAPI\KV\KVTree)
98+
{
99+
echo '<li>';
100+
echo $item->getPrefix();
101+
echo '<br>';
102+
echo '<ul>';
103+
output_tree($item);
104+
echo '</ul>';
105+
echo '</li>';
106+
}
107+
else if ($item instanceof \DCarbone\PHPConsulAPI\KV\KVPair)
108+
{
109+
echo '<li>'.$item->getKey().'</li>';
110+
}
111+
}
112+
}
113+
114+
echo '<ul>';
115+
foreach($tree as $v)
116+
{
117+
if ($v instanceof \DCarbone\PHPConsulAPI\KV\KVTree)
118+
{
119+
echo '<li>';
120+
echo $v->getPrefix();
121+
echo '<br>';
122+
echo '<ul>';
123+
output_tree($v);
124+
echo '</ul>';
125+
echo '</li>';
126+
}
127+
else if ($v instanceof \DCarbone\PHPConsulAPI\KV\KVPair)
128+
{
129+
echo '<li>'.$v->getKey().'</li>';
130+
}
131+
}
132+
echo '</ul>';
133+
```
134+
135+
The above will output something along the following (depends entirely on the keys you set):
136+
137+
```html
138+
<ul>
139+
<li>dcarbone/<br>
140+
<ul>
141+
<li>dcarbone/key2</li>
142+
<li>dcarbone/second key</li>
143+
<li>dcarbone/testkey</li>
144+
</ul>
145+
</li>
146+
</ul>
147+
```

src/AbstractConsulClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected function buildWriteMeta($duration)
130130
/**
131131
* @param HttpResponse $response
132132
* @return array(
133-
* @type array|string|bool|int|float decoded resposne
133+
* @type array|string|bool|int|float decoded response
134134
* @type Error|null error, if any
135135
* )
136136
*/

0 commit comments

Comments
 (0)