Skip to content

Commit 9324e14

Browse files
authored
Merge pull request #7 from platformsh/relationships
Add hasRelationship() method.
2 parents 393ecf9 + 3254999 commit 9324e14

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ $creds = $config->credentials('database');
113113

114114
The return value of `credentials()` is an associative array matching the relationship JSON object, which includes the appropriate user, password, host, database name, and other pertinent information. See the [Service documentation](https://docs.platform.sh/configuration/services.html) for your service for the exact structure and meaning of each property. In most cases that information can be passed directly to whatever other client library is being used to connect to the service.
115115

116+
To make sure that a relationship is defined before you try to access credentials out of it, use the `hasRelationship()` method:
117+
118+
```php
119+
if ($config->hasRelationship('database') {
120+
$creds = $conifg->credentials('database');
121+
// ...
122+
}
123+
```
124+
116125
## Formatting service credentials
117126

118127
In some cases the library being used to connect to a service wants its credentials formatted in a specific way; it could be a DSN string of some sort or it needs certain values concatenated to the database name, etc. For those cases you can use "Credential Formatters". A Credential Formatter is any `callable` (function, anonymous function, object method, etc.) that takes a credentials array and returns any type, since the library may want different types.

src/Config.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,19 @@ public function formattedCredentials(string $relationship, string $formatter)
427427
return $this->credentialFormatters[$formatter]($this->credentials($relationship));
428428
}
429429

430+
/**
431+
* Determines if a relationship is defined, and thus has credentials available.
432+
*
433+
* @param string $relationship
434+
* The name of the relationship to check.
435+
* @return bool
436+
* True if the relationship is defined, false otherwise.
437+
*/
438+
public function hasRelationship(string $relationship) : bool
439+
{
440+
return isset($this->relationshipsDef[$relationship]);
441+
}
442+
430443
/**
431444
* Reads an environment variable, taking the prefix into account.
432445
*

tests/ConfigTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,22 @@ public function test_credentials_missing_relationship_index_throws() : void
220220
$creds = $config->credentials('database', 3);
221221
}
222222

223+
public function test_hasRelationship_returns_true_for_existing_relationship() : void
224+
{
225+
$env = $this->mockEnvironmentDeploy;
226+
$config = new Config($env);
227+
228+
$this->assertTrue($config->hasRelationship('database'));
229+
}
230+
231+
public function test_hasRelationship_returns_false_for_missingrelationship() : void
232+
{
233+
$env = $this->mockEnvironmentDeploy;
234+
$config = new Config($env);
235+
236+
$this->assertFalse($config->hasRelationship('missing'));
237+
}
238+
223239
public function test_reading_existing_variable_works() : void
224240
{
225241
$env = $this->mockEnvironmentDeploy;

0 commit comments

Comments
 (0)