Skip to content

Commit 3254999

Browse files
committed
Add hasRelationship() method.
1 parent f2c5f09 commit 3254999

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
@@ -111,6 +111,15 @@ $creds = $config->credentials('database');
111111

112112
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.
113113

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

116125
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)