Skip to content

Commit defa020

Browse files
committed
Documentation updates
1 parent 5169215 commit defa020

File tree

5 files changed

+164
-3
lines changed

5 files changed

+164
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ First, construct a [Config](./src/Config.php) object:
2424
$config = new \DCarbone\PHPConsulAPI\Config(['Address' => 'address of your consul agent']);
2525
```
2626

27+
See the [configuration documentation](./docs/CONFIG.md) for more information on this object.
28+
2729
Next, construct a [Consul](./src/Consul.php) object:
2830

2931
```php
3032
$consul = new \DCarbone\PHPConsulAPI\Consul($config);
3133
```
3234

35+
*NOTE* If you do not create your own config object, [Consul](./src/Consu.php) will create it's own
36+
using [Config::newDefaultConfig()](../src/Config.php#L47).
37+
3338
Once constructed, you interact with each Consul API via it's corresponding Client class:
3439

3540
```php
@@ -51,6 +56,7 @@ var_dump($kv_list);
5156
- [Event](./docs/EVENT.md)
5257
- [Coordinate](./docs/COORDINATE.md)
5358
- [Health](./docs/HEALTH.md)
59+
- [Session](./docs/SESSION.md)
5460

5561
More will be added as time goes on!
5662

docs/CONFIG.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# PHP Consul API Configuration
2+
3+
This class is modeled quite closely after the
4+
[Config Struct](https://github.com/hashicorp/consul/blob/master/api/api.go#L101) present in the
5+
[Consul API Subpackage](https://github.com/hashicorp/consul/tree/master/api).
6+
7+
The primary difference is that the HTTP client is not stored within the config object. This is a possible future
8+
feature.
9+
10+
## Default Configuration
11+
12+
If you have defined some of the [Consul Environment Variables](https://www.consul.io/docs/agent/options.html)
13+
on your hosts then it would probably be easiest to simply execute the following:
14+
15+
```php
16+
$config = \DCarbone\PHPConsulAPI\Config::newDefaultConfig();
17+
```
18+
19+
You can see what this ultimately does [in the method definition](../src/Config.php#L47).
20+
21+
## Advanced Configuration
22+
23+
You may alternatively define values yourself:
24+
25+
```php
26+
$config = new \DCarbone\PHPConsulAPI\Config([
27+
'Address' => 'address of server', // REQUIRED
28+
'Scheme' => 'http or https', // REQUIRED
29+
'Datacenter' => 'name of datacenter', // OPTIONAL
30+
'HttpAuth' => 'user:pass', // OPTIONAL,
31+
'WaitTime' => 30, // OPTIONAL, not used yet
32+
'Token' => 'auth token', // OPTIONAL
33+
'CAFile' => 'path to ca', // OPTIONAL
34+
'CertFile' => 'path to cert', // OPTIONAL
35+
'KeyFile' => 'path to key', // OPTIONAL
36+
'InsecureSkipVerify' => false, // OPTIONAL
37+
]);
38+
```

docs/SESSION.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# PHP Consul API Session
2+
3+
ALl interactions with the [`v1/session`](https://www.consul.io/docs/agent/http/session.html) endpoint
4+
are done via the [SessionClient](../src/Session/SessionClient.php) class.
5+
6+
If you have constructed a [Consul](../src/Consul.php) object, this is done as so:
7+
8+
```php
9+
$session = $consul->Session;
10+
```
11+
12+
## Actions
13+
14+
### Create
15+
16+
In order to initialize a new session, you must first create a [SessionEntry](../src/Session/SessionEntry.php) object.
17+
18+
Below is a quick example:
19+
20+
```php
21+
$sessionEntry = new \DCarbone\PHPConsulAPI\Session\SessionEntry(
22+
array(
23+
'LockDelay' => '10s',
24+
'Name' => 'my-test-lock',
25+
'Behavior' => 'release',
26+
'TTL' => '15s',
27+
)
28+
);
29+
30+
list($sid, $wm, $err) = $consul-Session->create($sessionEntry);
31+
if (null !== $err)
32+
die($err);
33+
34+
var_dump($id, $wm);
35+
```
36+
37+
### Destroy
38+
39+
```php
40+
list($wm, $err) = $consul->Session->destroy($sid);
41+
if (null !== $err)
42+
die($err);
43+
44+
var_dump($wm);
45+
```
46+
47+
### Renew
48+
49+
```php
50+
list($sessions, $wm, $err) = $consul->Session->renew($sid);
51+
if (null !== $err)
52+
die($err);
53+
54+
var_dump($sessions, $wm);
55+
```
56+
57+
### Info
58+
59+
```php
60+
list($sessions, $qm, $err) = $consul->Session->info($sid);
61+
if (null !== $err)
62+
die($err);
63+
64+
var_dump($seesions, $qm);
65+
```
66+
67+
### List sessions belonging to a specific node
68+
69+
```php
70+
list($sessions, $qm, $err) = $consul->Session->node($node);
71+
if (null !== $err)
72+
die($err);
73+
74+
var_dump($sessions, $qm);
75+
```
76+
77+
### List all sessions
78+
79+
```php
80+
list($sessions, $qm, $err) = $consul->Session->listSessions();
81+
if (null !== $err)
82+
die($err);
83+
84+
var_dump($sessions, $qm);
85+
```

docs/STATUS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
All interactions with the [`v1/status`](https://www.consul.io/docs/agent/http/status.html) endpoint
44
are done via the [StatusClient](../src/Status/StatusClient.php) class.
55

6-
If you have constructed a [Client../src/Consul.php) object, this is done as so:
6+
If you have constructed a [Consul](../src/Consul.php) object, this is done as so:
77

88
```php
99
$status = $consul->Status;

src/Config.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function setHttpAuth($httpAuth)
162162
throw new \InvalidArgumentException(sprintf(
163163
'%s::setHttpAuth - Value is expected to be string of "username:password" or instance of "ConsulHttpAuth", %s seen.',
164164
get_class($this),
165-
gettype($httpAuth)
165+
is_string($httpAuth) ? $httpAuth : gettype($httpAuth)
166166
));
167167
}
168168

@@ -279,7 +279,25 @@ public function setInsecureSkipVerify($insecureSkipVerify)
279279
*/
280280
public function compileAddress()
281281
{
282-
return sprintf('%s://%s', $this->getScheme(), $this->getAddress());
282+
if ('' === ($scheme = $this->getScheme()))
283+
{
284+
throw new \LogicException(sprintf(
285+
'%s - "Scheme" was left undefined in this config object. Definition: %s',
286+
get_class($this),
287+
json_encode($this)
288+
));
289+
}
290+
291+
if ('' === ($addr = $this->getAddress()))
292+
{
293+
throw new \LogicException(sprintf(
294+
'%s - "Address" was left undefined in this config object. Definition: %s',
295+
get_class($this),
296+
json_encode($this)
297+
));
298+
}
299+
300+
return sprintf('%s://%s', $scheme, $addr);
283301
}
284302

285303
/**
@@ -326,4 +344,18 @@ protected static function _tryGetEnvParam($param)
326344

327345
return false;
328346
}
347+
348+
/**
349+
* Specify data which should be serialized to JSON
350+
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
351+
* @return mixed data which can be serialized by json_encode, which is a value of any type other than a resource.
352+
*/
353+
public function jsonSerialize()
354+
{
355+
$data = parent::jsonSerialize();
356+
if (isset($data['HttpAuth']))
357+
unset($data['HttpAuth']);
358+
359+
return $data;
360+
}
329361
}

0 commit comments

Comments
 (0)