Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

Commit 88f5925

Browse files
committed
Allow using the package in the configuration files
1 parent 644982f commit 88f5925

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

src/Credentials.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
class Credentials
1010
{
11+
const CONFIG_PREFIX = '___credentials_';
1112

1213
/** @var Encrypter */
1314
private $encrypter;

src/CredentialsServiceProvider.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
class CredentialsServiceProvider extends ServiceProvider
1010
{
11+
1112
/**
1213
* Bootstrap the application services.
1314
*/
@@ -18,6 +19,22 @@ public function boot()
1819
], 'config');
1920

2021
$this->mergeConfigFrom(__DIR__ . '/../config/credentials.php', 'credentials');
22+
23+
// Update configuration strings
24+
if( !app()->configurationIsCached()) {
25+
$this->fixConfig();
26+
}
27+
}
28+
29+
protected function fixConfig()
30+
{
31+
collect(array_dot(config()->all()))->filter(function ($item) {
32+
return is_string($item) && starts_with($item, Credentials::CONFIG_PREFIX);
33+
})->map(function ($item, $key) {
34+
$item = str_replace_first(Credentials::CONFIG_PREFIX, '', $item);
35+
36+
config()->set($key, credentials($item));
37+
});
2138
}
2239

2340
/**

src/EditCredentialsCommand.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,8 @@ public function handle(Credentials $credentials)
3838

3939
$process = new Process($editor.' '.$meta['uri']);
4040

41-
try {
42-
$process->setTty(true);
43-
$process->mustRun();
44-
} catch (ProcessFailedException $e) {
45-
dd($e->getMessage());
46-
}
41+
$process->setTty(true);
42+
$process->mustRun();
4743

4844
$data = json_decode(file_get_contents($meta['uri']), JSON_OBJECT_AS_ARRAY);
4945

src/helpers.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
function credentials(string $key, $default = null) {
77
$filename = config('credentials.file');
88

9-
$credentials = app(Credentials::class);
10-
$credentials->load($filename);
9+
try {
10+
$credentials = app(Credentials::class);
11+
$credentials->load($filename);
1112

12-
return $credentials->get($key);
13+
return $credentials->get($key);
14+
} catch (ReflectionException $e) {
15+
return Credentials::CONFIG_PREFIX.$key;
16+
}
1317
}
1418
}

tests/CredentialTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,22 @@ public function it_can_use_the_helper_function()
123123

124124
$this->assertSame('my-secret-value', credentials('key'));
125125
}
126+
127+
/** @test */
128+
public function it_replaces_credential_strings_in_the_configuration_files()
129+
{
130+
$this->app['config']->set('credentials.file', __DIR__ . '/temp/credentials.php.enc');
131+
132+
$data = [
133+
'key' => 'my-secret-value'
134+
];
135+
136+
$credentials = app(Credentials::class);
137+
138+
$credentials->store($data, __DIR__ . '/temp/credentials.php.enc');
139+
140+
$this->app['config']->set('credentials.secret', Credentials::CONFIG_PREFIX.'key');
141+
142+
$this->assertSame('my-secret-value', credentials('key'));
143+
}
126144
}

0 commit comments

Comments
 (0)