-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-secrets.php
37 lines (30 loc) · 951 Bytes
/
class-secrets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
class Secrets {
private ?string $secrets_path;
private ?object $secrets;
public function __construct( $secrets_path ) {
$this->secrets_path = $secrets_path;
if ( is_readable( $this->secrets_path ) ) {
$raw_secrets = file_get_contents( $this->secrets_path );
$this->secrets = json_decode( $raw_secrets );
}
}
public function __get( $key ) : ?string {
if ( empty( $this->secrets->{$key} ) ) {
return null;
}
return $this->secrets->{$key};
}
public function __set( $key, $value ) : void {
$this->secrets->{$key} = $value;
}
public function save() : void {
if ( ! is_writable( $this->secrets_path ) ) {
throw new Exception( "Secrets file {$this->secrets_path} is not writable." );
}
$result = file_put_contents( $this->secrets_path, json_encode( $this->secrets, JSON_PRETTY_PRINT ) );
if ( false === $result ) {
throw new Exception( "Could not save secrets to {$this->secrets_path}." );
}
}
}