Skip to content

Commit 9ca0761

Browse files
committed
Cleanup commit
- Documentation update - Slightly more efficient configuration construction, could still be improved.
1 parent ca0978d commit 9ca0761

File tree

4 files changed

+118
-69
lines changed

4 files changed

+118
-69
lines changed

README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ You may alternatively define values yourself:
4848

4949
```php
5050
$config = new \DCarbone\PHPConsulAPI\Config([
51-
'HttpClient' => $client // [required] Client conforming to GuzzleHttp\ClientInterface
51+
'HttpClient' => $client, // [required] Client conforming to GuzzleHttp\ClientInterface
5252
'Address' => 'address of server', // [required]
5353

5454
'Scheme' => 'http or https', // [optional] defaults to "http"
@@ -58,11 +58,33 @@ $config = new \DCarbone\PHPConsulAPI\Config([
5858
'TokenInHeader' => false, // [optional] specifies whether to send the token in the header or query string
5959
'InsecureSkipVerify' => false, // [optional] if set to true, ignores all SSL validation
6060
'CAFile' => '', // [optional] path to ca cert file, see http://docs.guzzlephp.org/en/latest/request-options.html#verify
61-
'CertFile' => '', // [optional] path to client pem. if set, requires KeyFile also be set
62-
'KeyFile' => '', // [optional] path to client
61+
'CertFile' => '', // [optional] path to client public key. if set, requires KeyFile also be set
62+
'KeyFile' => '', // [optional] path to client private key. if set, requires CertFile also be set
6363
]);
6464
```
6565

66+
#### Configuration Note:
67+
68+
By default, this client will attempt to locate a series of environment variables to describe much of the above
69+
configuration properties. See [here](./src/Config.php#L446) for that list, and see [here](./src/Consul.php#L36) for
70+
a list of the env var names.
71+
72+
For more advanced client configuration, such as proxy configuration, you must construct your own GuzzleHttp client
73+
prior to constructing a PHPConsulAPI Config object.
74+
75+
As an example:
76+
77+
```php
78+
$proxyClient = new \GuzzleHttp\Client(['proxy' => 'whatever proxy you want']]);
79+
$config = new \DCarbone\PHPConsulAPI\Config([
80+
'HttpClient' => $proxyClient,
81+
'Address' => 'address of server',
82+
]);
83+
```
84+
85+
When constructing your client, if you are using the `GuzzleHttp\Client` object directly or derivative thereof, you may
86+
pass any option listed in the [Guzzle Request Options](http://docs.guzzlephp.org/en/latest/request-options.html).
87+
6688
## Consul
6789

6890
Next, construct a [Consul](./src/Consul.php) object:

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
],
1313
"keywords": [
1414
"consul",
15-
"consul-api",
16-
"psr-7"
15+
"consul-api"
1716
],
1817
"require": {
1918
"php": ">=5.6.0",

src/Config.php

Lines changed: 68 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
See the License for the specific language governing permissions and
1616
limitations under the License.
1717
*/
18+
1819
use GuzzleHttp\Client;
1920
use GuzzleHttp\ClientInterface;
2021
use GuzzleHttp\RequestOptions;
@@ -148,52 +149,6 @@ public static function newDefaultConfig() {
148149
return new static(self::getDefaultConfig());
149150
}
150151

151-
/**
152-
* @return array
153-
*/
154-
private static function getDefaultConfig() {
155-
$conf = [
156-
'Address' => '127.0.0.1:8500',
157-
'Scheme' => 'http',
158-
];
159-
160-
// parse env vars
161-
foreach (static::getEnvironmentConfig() as $k => $v) {
162-
switch ($k) {
163-
case Consul::HTTPAddrEnvName:
164-
$conf['Address'] = $v;
165-
break;
166-
case Consul::HTTPTokenEnvName:
167-
$conf['Token'] = $v;
168-
break;
169-
case Consul::HTTPAuthEnvName:
170-
$conf['HttpAuth'] = $v;
171-
break;
172-
case Consul::HTTPCAFileEnvName:
173-
$conf['CAFile'] = $v;
174-
break;
175-
case Consul::HTTPClientCertEnvName:
176-
$conf['CertFile'] = $v;
177-
break;
178-
case Consul::HTTPClientKeyEnvName:
179-
$conf['KeyFile'] = $v;
180-
break;
181-
case Consul::HTTPSSLEnvName:
182-
if ((bool)$v) {
183-
$conf['Scheme'] = 'https';
184-
}
185-
break;
186-
case Consul::HTTPSSLVerifyEnvName:
187-
if ((bool)$v) {
188-
$conf['InsecureSkipVerify'] = true;
189-
}
190-
break;
191-
}
192-
}
193-
194-
return $conf;
195-
}
196-
197152
/**
198153
* @return string
199154
*/
@@ -448,24 +403,6 @@ public function getGuzzleRequestOptions() {
448403
return $opts;
449404
}
450405

451-
/**
452-
* @return array
453-
*/
454-
public static function getEnvironmentConfig() {
455-
return array_filter([
456-
Consul::HTTPAddrEnvName => static::_tryGetEnvParam(Consul::HTTPAddrEnvName),
457-
Consul::HTTPAuthEnvName => static::_tryGetEnvParam(Consul::HTTPAuthEnvName),
458-
Consul::HTTPCAFileEnvName => static::_tryGetEnvParam(Consul::HTTPCAFileEnvName),
459-
Consul::HTTPClientCertEnvName => static::_tryGetEnvParam(Consul::HTTPClientCertEnvName),
460-
Consul::HTTPClientKeyEnvName => static::_tryGetEnvParam(Consul::HTTPClientKeyEnvName),
461-
Consul::HTTPSSLEnvName => static::_tryGetEnvParam(Consul::HTTPSSLEnvName),
462-
Consul::HTTPSSLVerifyEnvName => static::_tryGetEnvParam(Consul::HTTPSSLVerifyEnvName),
463-
],
464-
function ($val) {
465-
return null !== $val;
466-
});
467-
}
468-
469406
/**
470407
* @param string $param
471408
* @return string|null
@@ -485,4 +422,71 @@ protected static function _tryGetEnvParam($param) {
485422

486423
return null;
487424
}
425+
426+
/**
427+
* @return array
428+
*/
429+
public static function getEnvironmentConfig() {
430+
$ret = [];
431+
foreach ([
432+
Consul::HTTPAddrEnvName => static::_tryGetEnvParam(Consul::HTTPAddrEnvName),
433+
Consul::HTTPAuthEnvName => static::_tryGetEnvParam(Consul::HTTPAuthEnvName),
434+
Consul::HTTPCAFileEnvName => static::_tryGetEnvParam(Consul::HTTPCAFileEnvName),
435+
Consul::HTTPClientCertEnvName => static::_tryGetEnvParam(Consul::HTTPClientCertEnvName),
436+
Consul::HTTPClientKeyEnvName => static::_tryGetEnvParam(Consul::HTTPClientKeyEnvName),
437+
Consul::HTTPSSLEnvName => static::_tryGetEnvParam(Consul::HTTPSSLEnvName),
438+
Consul::HTTPSSLVerifyEnvName => static::_tryGetEnvParam(Consul::HTTPSSLVerifyEnvName),
439+
] as $k => $v) {
440+
if (null !== $v) {
441+
$ret[$k] = $v;
442+
}
443+
}
444+
return $ret;
445+
}
446+
447+
/**
448+
* @return array
449+
*/
450+
private static function getDefaultConfig() {
451+
$conf = [
452+
'Address' => '127.0.0.1:8500',
453+
'Scheme' => 'http',
454+
];
455+
456+
// parse env vars
457+
foreach (static::getEnvironmentConfig() as $k => $v) {
458+
switch ($k) {
459+
case Consul::HTTPAddrEnvName:
460+
$conf['Address'] = $v;
461+
break;
462+
case Consul::HTTPTokenEnvName:
463+
$conf['Token'] = $v;
464+
break;
465+
case Consul::HTTPAuthEnvName:
466+
$conf['HttpAuth'] = $v;
467+
break;
468+
case Consul::HTTPCAFileEnvName:
469+
$conf['CAFile'] = $v;
470+
break;
471+
case Consul::HTTPClientCertEnvName:
472+
$conf['CertFile'] = $v;
473+
break;
474+
case Consul::HTTPClientKeyEnvName:
475+
$conf['KeyFile'] = $v;
476+
break;
477+
case Consul::HTTPSSLEnvName:
478+
if ((bool)$v) {
479+
$conf['Scheme'] = 'https';
480+
}
481+
break;
482+
case Consul::HTTPSSLVerifyEnvName:
483+
if ((bool)$v) {
484+
$conf['InsecureSkipVerify'] = true;
485+
}
486+
break;
487+
}
488+
}
489+
490+
return $conf;
491+
}
488492
}

tests/Usage/ConfigUsageTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace DCarbone\PHPConsulAPITests\Usage;
22

33
use DCarbone\PHPConsulAPI\Config;
4+
use DCarbone\PHPConsulAPI\Consul;
45

56
/*
67
Copyright 2016-2017 Daniel Carbone ([email protected])
@@ -23,6 +24,9 @@
2324
* @package DCarbone\PHPConsulAPITests\Usage
2425
*/
2526
class ConfigUsageTest extends \PHPUnit_Framework_TestCase {
27+
const DEFAULT_ADDRESS = '127.0.0.1:8500';
28+
const DEFAULT_SCHEME = 'http';
29+
2630
/**
2731
* @return Config
2832
*/
@@ -31,4 +35,24 @@ public function testCanConstructConfig() {
3135
$this->assertInstanceOf(Config::class, $config);
3236
return $config;
3337
}
38+
39+
/**
40+
* @depends testCanConstructConfig
41+
*/
42+
public function testConfigDefaults() {
43+
$config = new Config();
44+
45+
$expectedAddress = getenv(Consul::HTTPAddrEnvName) ?: self::DEFAULT_ADDRESS;
46+
$expectedScheme = (bool)getenv(Consul::HTTPSSLEnvName) ? 'https' : self::DEFAULT_SCHEME;
47+
48+
$this->assertEquals($expectedAddress,
49+
$config->getAddress(),
50+
sprintf('Default address is not "%s"', $expectedAddress));
51+
$this->assertEquals($expectedScheme,
52+
$config->getScheme(),
53+
sprintf('Default scheme is not "%s"', $expectedScheme));
54+
$this->assertNotNull($config->getHttpClient(), 'HttpClient is null');
55+
$this->assertFalse($config->isInsecureSkipVerify(), 'InsecureSkipVerify is not false');
56+
$this->assertFalse($config->isTokenInHeader(), 'TokenInHeader is not false');
57+
}
3458
}

0 commit comments

Comments
 (0)