Skip to content

Commit 68e5d69

Browse files
committed
Tidy up code
1 parent 150da25 commit 68e5d69

19 files changed

Lines changed: 314 additions & 260 deletions

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# PHPVault
22

33
PHPVault is a PHP library that can create, read, encrypt and decrypt environment files (so-called dotenv files). For
4-
example is `.env`, an encrypted file `.env.enc`, etc. Within your project you can automatically load these encrypted
5-
environment variables from `.env.enc` into `getenv()`, `$_ENV` and `$_SERVER`. The corresponding key-value pairs within
6-
these dotenv files are encrypted and decrypted using an asymmetric encryption method
4+
example is `.env` a plain file, `.env.enc` an encrypted file, etc. Within your project you can automatically load these
5+
encrypted environment variables from `.env.enc` into `getenv()`, `$_ENV` and `$_SERVER`. The corresponding key-value
6+
pairs within these dotenv files are encrypted and decrypted using an asymmetric encryption method
77
([Public-key cryptography](https://en.wikipedia.org/wiki/Public-key_cryptography)). Private keys are only available
88
on productive systems for decrypting dotenv values. The public key, on the other hand, can be safely checked into
99
the repository and is used everywhere to encrypt new values.
@@ -89,10 +89,10 @@ $ vendor/bin/php-vault set .env.enc DB_NAME secret.name --public-key
8989
### Display the environment file
9090

9191
* The contents displayed are encrypted.
92-
* Use public key (`--public-key` → read from `.keys/public.key`).
92+
* Do not need any key.
9393

9494
```bash
95-
$ vendor/bin/php-vault display .env.enc --load-encrypted --public-key
95+
$ vendor/bin/php-vault display .env.enc --load-encrypted
9696
...
9797
```
9898

@@ -131,7 +131,7 @@ The file was successfully written to ".env".
131131

132132
### Display the decrypted file without encryption
133133

134-
* Do not need any key
134+
* Do not need any key.
135135

136136
```bash
137137
$ vendor/bin/php-vault display .env --display-decrypted

src/Command/BaseCommand.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ protected function getPublicKeyPath(string $path = '.keys', string $name = 'publ
185185
/**
186186
*
187187
*
188-
* @param PHPVault $core
188+
* @param PHPVault $phpVaultCore
189189
* @return bool
190190
* @throws SodiumException
191191
* @throws Exception
192192
*/
193-
protected function loadPrivateOrPublicKey(PHPVault $core): bool
193+
protected function loadPrivateOrPublicKey(PHPVault $phpVaultCore): bool
194194
{
195195
$privateKey = $this->getOption(self::OPTION_PRIVATE_KEY, $this->getPrivateKeyPath(), true);
196196
$publicKey = $this->getOption(self::OPTION_PUBLIC_KEY, $this->getPublicKeyPath(), true);
@@ -204,7 +204,7 @@ protected function loadPrivateOrPublicKey(PHPVault $core): bool
204204
return false;
205205
}
206206

207-
$core->getKeyPair()->loadPublicKeyFromFile($publicKey);
207+
$phpVaultCore->getKeyPair()->loadPublicKeyFromFile($publicKey);
208208
return true;
209209
}
210210

@@ -217,7 +217,7 @@ protected function loadPrivateOrPublicKey(PHPVault $core): bool
217217
return false;
218218
}
219219

220-
$core->getKeyPair()->loadPrivateKeyFromFile($privateKey);
220+
$phpVaultCore->getKeyPair()->loadPrivateKeyFromFile($privateKey);
221221
return true;
222222
}
223223

@@ -227,14 +227,14 @@ protected function loadPrivateOrPublicKey(PHPVault $core): bool
227227
/**
228228
* Writes all environment variables from vault to file.
229229
*
230-
* @param PHPVault $core
230+
* @param PHPVault $phpVaultCore
231231
* @param string|null $envFile
232232
* @param string $outputType
233233
* @param bool $ignoreExistingFile
234234
* @return void
235235
* @throws Exception
236236
*/
237-
protected function writeEnvVariables(PHPVault $core, ?string $envFile = null, string $outputType = Reader::OUTPUT_TO_ENCRYPTED, bool $ignoreExistingFile = false): void
237+
protected function writeEnvVariables(PHPVault $phpVaultCore, ?string $envFile = null, string $outputType = Reader::OUTPUT_TO_ENCRYPTED, bool $ignoreExistingFile = false): void
238238
{
239239
/* Check if option was given to write a file. */
240240
if (!$envFile) {
@@ -248,7 +248,7 @@ protected function writeEnvVariables(PHPVault $core, ?string $envFile = null, st
248248
}
249249

250250
/* Write file */
251-
$envFileString = $core->getVault()->getWriter()->getEnvString($outputType, true);
251+
$envFileString = $phpVaultCore->getVault()->getWriter()->getEnvString($outputType, true);
252252
file_put_contents($envFile, $envFileString);
253253

254254
/* Check that the given env file was written. */

src/Command/DecryptFileCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,17 @@ public function execute(): void
9090
}
9191

9292
/* Initiate the PhpVault Core */
93-
$core = new PHPVault();
93+
$phpVaultCore = new PHPVault();
9494

9595
/* Loads private or public key. */
96-
if (!$this->loadPrivateOrPublicKey($core)) {
96+
if (!$this->loadPrivateOrPublicKey($phpVaultCore)) {
9797
return;
9898
}
9999

100100
/* Load env decrypted file */
101-
$core->getVault()->getReader()->addFileToVault($envFileEncrypted, Reader::LOAD_FROM_ENCRYPTED, Reader::OUTPUT_TO_DECRYPTED);
101+
$phpVaultCore->getVault()->getReader()->addFileToVault($envFileEncrypted, Reader::LOAD_FROM_ENCRYPTED, Reader::OUTPUT_TO_DECRYPTED);
102102

103103
/* Writes the vault */
104-
$this->writeEnvVariables($core, $envFileDecrypted, Reader::OUTPUT_TO_DECRYPTED);
104+
$this->writeEnvVariables($phpVaultCore, $envFileDecrypted, Reader::OUTPUT_TO_DECRYPTED);
105105
}
106106
}

src/Command/DisplayCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,24 +104,24 @@ public function execute(): void
104104
}
105105

106106
/* Initiates the PhpVault Core. Loads private or public key from $_SERVER if given. */
107-
$core = new PHPVault(false);
107+
$phpVaultCore = new PHPVault(false);
108108

109109
/* Loads private or public key. */
110-
if (!$this->loadPrivateOrPublicKey($core)) {
110+
if (!$this->loadPrivateOrPublicKey($phpVaultCore)) {
111111
return;
112112
}
113113

114114
/* Load env file */
115-
$core->getVault()->getReader()->addFileToVault(
115+
$phpVaultCore->getVault()->getReader()->addFileToVault(
116116
$envFile,
117117
$loadEncrypted ? Reader::LOAD_FROM_ENCRYPTED : Reader::LOAD_FROM_DECRYPTED,
118118
$displayDecrypted ? Reader::OUTPUT_TO_DECRYPTED : Reader::OUTPUT_TO_ENCRYPTED
119119
);
120120

121121
/* Displays the vault */
122-
$this->logger->getDisplay()->envVariables($core, Reader::OUTPUT_TO_DECRYPTED);
122+
$this->logger->getDisplay()->envVariables($phpVaultCore, $displayDecrypted ? Reader::OUTPUT_TO_DECRYPTED : Reader::OUTPUT_TO_ENCRYPTED);
123123

124124
/* Writes the vault */
125-
$this->writeEnvVariables($core, $writeEnv, $displayDecrypted ? Reader::OUTPUT_TO_DECRYPTED : Reader::OUTPUT_TO_ENCRYPTED);
125+
$this->writeEnvVariables($phpVaultCore, $writeEnv, $displayDecrypted ? Reader::OUTPUT_TO_DECRYPTED : Reader::OUTPUT_TO_ENCRYPTED);
126126
}
127127
}

src/Command/EncryptFileCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ public function execute(): void
8585
}
8686

8787
/* Initiate the PhpVault Core */
88-
$core = new PHPVault();
88+
$phpVaultCore = new PHPVault();
8989

9090
/* Loads private or public key. */
91-
if (!$this->loadPrivateOrPublicKey($core)) {
91+
if (!$this->loadPrivateOrPublicKey($phpVaultCore)) {
9292
return;
9393
}
9494

9595
/* Load env decrypted file */
96-
$core->getVault()->getReader()->addFileToVault($envFileDecrypted, Reader::LOAD_FROM_DECRYPTED, Reader::OUTPUT_TO_ENCRYPTED);
96+
$phpVaultCore->getVault()->getReader()->addFileToVault($envFileDecrypted, Reader::LOAD_FROM_DECRYPTED, Reader::OUTPUT_TO_ENCRYPTED);
9797

9898
/* Writes the vault */
99-
$this->writeEnvVariables($core, $envFileEncrypted);
99+
$this->writeEnvVariables($phpVaultCore, $envFileEncrypted);
100100
}
101101
}

src/Command/GenerateKeysCommand.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,28 @@ public function __construct(bool $allowUnknown = false, App $app = null)
7777
public function execute(): void
7878
{
7979
/* Initiate the PhpVault Core */
80-
$core = new PHPVault(true);
80+
$phpVaultCore = new PHPVault(true);
8181

8282
/* Load options */
8383
$persist = $this->getOption(self::OPTION_PERSIST);
8484

8585
/* Display the private and public key */
8686
if (!$persist) {
87-
$this->logger->getDisplay()->privateAndPublicKeys($core);
87+
$this->logger->getDisplay()->privateAndPublicKeys($phpVaultCore);
8888
}
8989

9090
/* Persist keys */
91-
$this->persistKeys($core);
91+
$this->persistKeys($phpVaultCore);
9292
}
9393

9494
/**
9595
* Persist keys.
9696
*
97-
* @param PHPVault $core
97+
* @param PHPVault $phpVaultCore
9898
* @return void
9999
* @throws Exception
100100
*/
101-
protected function persistKeys(PHPVault $core): void
101+
protected function persistKeys(PHPVault $phpVaultCore): void
102102
{
103103
/* Check if persist option exists. */
104104
if (!$this->getOption(self::OPTION_PERSIST)) {
@@ -134,8 +134,8 @@ protected function persistKeys(PHPVault $core): void
134134
$gitignoreAbsolute = sprintf('%s/%s', $pathKeys, $gitignore);
135135

136136
/* Write files. */
137-
file_put_contents($pathPrivateKey, $core->getKeyPair()->getPrivateKey());
138-
file_put_contents($pathPublicKey, $core->getKeyPair()->getPublicKey());
137+
file_put_contents($pathPrivateKey, $phpVaultCore->getKeyPair()->getPrivateKey());
138+
file_put_contents($pathPublicKey, $phpVaultCore->getKeyPair()->getPublicKey());
139139
file_put_contents($gitignoreAbsolute, $gitignoreContent);
140140

141141
/* Check files. */

src/Command/InfoCommand.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,30 @@ public function __construct(bool $allowUnknown = false, App $app = null)
6565
public function execute(): void
6666
{
6767
/* Initiate the PhpVault Core */
68-
$core = new PHPVault();
68+
$phpVaultCore = new PHPVault();
6969

7070
/* Loads private or public key. */
71-
if (!$this->loadPrivateOrPublicKey($core)) {
71+
if (!$this->loadPrivateOrPublicKey($phpVaultCore)) {
7272
return;
7373
}
7474

7575
/* No key was loaded */
76-
if ($core->getKeyPair()->noKeyIsLoaded()) {
76+
if ($phpVaultCore->getKeyPair()->noKeyIsLoaded()) {
7777
$this->logger->getDisplay()->noKeyLoaded();
7878
return;
7979
}
8080

8181
/* Private key was loaded */
82-
if ($core->getKeyPair()->getPrivateKey()) {
83-
$this->logger->getDisplay()->privateKeyLoaded($core);
84-
$this->logger->getDisplay()->keyLoadedFrom($core->getKeyPair());
82+
if ($phpVaultCore->getKeyPair()->getPrivateKey()) {
83+
$this->logger->getDisplay()->privateKeyLoaded($phpVaultCore);
84+
$this->logger->getDisplay()->keyLoadedFrom($phpVaultCore->getKeyPair());
8585
return;
8686
}
8787

8888
/* Private key was loaded */
89-
if ($core->getKeyPair()->getPublicKey()) {
90-
$this->logger->getDisplay()->publicKeyLoaded($core);
91-
$this->logger->getDisplay()->keyLoadedFrom($core->getKeyPair());
89+
if ($phpVaultCore->getKeyPair()->getPublicKey()) {
90+
$this->logger->getDisplay()->publicKeyLoaded($phpVaultCore);
91+
$this->logger->getDisplay()->keyLoadedFrom($phpVaultCore->getKeyPair());
9292
return;
9393
}
9494
}

src/Command/SetCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,25 @@ public function execute(): void
100100
}
101101

102102
/* Initiates the PhpVault Core. Loads private or public key from $_SERVER if given. */
103-
$core = new PHPVault();
103+
$phpVaultCore = new PHPVault();
104104

105105
/* Loads private or public key. */
106-
if (!$this->loadPrivateOrPublicKey($core)) {
106+
if (!$this->loadPrivateOrPublicKey($phpVaultCore)) {
107107
return;
108108
}
109109

110110
/* Load env file */
111111
if (file_exists($envFile)) {
112-
$core->getVault()->getReader()->addFileToVault($envFile, Reader::LOAD_FROM_ENCRYPTED, Reader::OUTPUT_TO_ENCRYPTED);
112+
$phpVaultCore->getVault()->getReader()->addFileToVault($envFile, Reader::LOAD_FROM_ENCRYPTED, Reader::OUTPUT_TO_ENCRYPTED);
113113
}
114114

115115
/* set new name value set */
116-
$core->getVault()->add($name, $value, $description);
116+
$phpVaultCore->getVault()->add($name, $value, $description);
117117

118118
/* Displays the vault */
119-
$this->logger->getDisplay()->envVariables($core, Reader::OUTPUT_TO_DECRYPTED);
119+
$this->logger->getDisplay()->envVariables($phpVaultCore);
120120

121121
/* Writes the vault */
122-
$this->writeEnvVariables($core, $envFile, Reader::OUTPUT_TO_ENCRYPTED, true);
122+
$this->writeEnvVariables($phpVaultCore, $envFile, Reader::OUTPUT_TO_ENCRYPTED, true);
123123
}
124124
}

src/Decrypter.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@
3131

3232
class Decrypter
3333
{
34-
protected PHPVault $core;
34+
protected PHPVault $phpVaultCore;
3535

3636
/**
3737
* Decrypter constructor.
3838
*
39-
* @param PHPVault $core
39+
* @param PHPVault $phpVaultCore
4040
*/
41-
public function __construct(PHPVault $core)
41+
public function __construct(PHPVault $phpVaultCore)
4242
{
43-
$this->core = $core;
43+
$this->phpVaultCore = $phpVaultCore;
4444
}
4545

4646
/**
@@ -54,12 +54,12 @@ public function __construct(PHPVault $core)
5454
public function decrypt(string $data): ?string
5555
{
5656
/* Check mode */
57-
if ($this->core->getMode() < Mode::MODE_DECRYPT) {
57+
if ($this->phpVaultCore->getMode() < Mode::MODE_DECRYPT) {
5858
throw new Exception('Decrypter::decrypt: The Decrypter class is not able to decrypt strings. Please load a private key to do this.');
5959
}
6060

6161
$key = sodium_crypto_box_keypair_from_secretkey_and_publickey(
62-
base64_decode($this->core->getKeyPair()->getPrivateKey()),
62+
base64_decode($this->phpVaultCore->getKeyPair()->getPrivateKey()),
6363
base64_decode(PHPVault::CORE_PUBLIC_KEY)
6464
);
6565

src/Encrypter.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ class Encrypter
3333
{
3434
const PATTERN_ALREADY_DECRYPTED = '~^Wy[IJ][A-Za-z0-9]+[=]*$~';
3535

36-
protected PHPVault $core;
36+
protected PHPVault $phpVaultCore;
3737

3838
/**
3939
* Decrypter constructor.
4040
*
41-
* @param PHPVault $core
41+
* @param PHPVault $phpVaultCore
4242
*/
43-
public function __construct(PHPVault $core)
43+
public function __construct(PHPVault $phpVaultCore)
4444
{
45-
$this->core = $core;
45+
$this->phpVaultCore = $phpVaultCore;
4646
}
4747

4848
/**
@@ -57,7 +57,7 @@ public function __construct(PHPVault $core)
5757
public function encrypt(string $message, string $nonce = null): ?string
5858
{
5959
/* Check mode */
60-
if ($this->core->getMode() < Mode::MODE_ENCRYPT) {
60+
if ($this->phpVaultCore->getMode() < Mode::MODE_ENCRYPT) {
6161
throw new Exception('Encrypter::encrypt: The Encrypter class is not able to encrypt strings. Please load at least a public key to do this.');
6262
}
6363

@@ -74,7 +74,7 @@ public function encrypt(string $message, string $nonce = null): ?string
7474

7575
$key = sodium_crypto_box_keypair_from_secretkey_and_publickey(
7676
base64_decode(PHPVault::CORE_PRIVATE_KEY),
77-
base64_decode($this->core->getKeyPair()->getPublicKey())
77+
base64_decode($this->phpVaultCore->getKeyPair()->getPublicKey())
7878
);
7979

8080
$dataArray = array(

0 commit comments

Comments
 (0)