|
| 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 | +``` |
0 commit comments