Skip to content
This repository was archived by the owner on Feb 20, 2025. It is now read-only.

Commit 01d05f9

Browse files
author
Andrey Helldar
authored
Merge pull request #34 from TheDragonCode/3.x
Fixed removal of secret keys of `env` file
2 parents dd0a7fd + d96c8d7 commit 01d05f9

File tree

8 files changed

+67
-31
lines changed

8 files changed

+67
-31
lines changed

src/Services/Compiler.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public function __construct(Stringify $stringify, Config $config)
2727
$this->config = $config;
2828
}
2929

30-
public function items(array $items, array $target = []): self
30+
public function items(array $items, array $target = [], bool $secure = true): self
3131
{
32-
$this->items = $this->map(
33-
$this->filter($items, $target)
34-
);
32+
$filtered = $this->filter($items, $target);
33+
34+
$this->items = $this->map($filtered, $secure);
3535

3636
return $this;
3737
}
@@ -69,14 +69,14 @@ protected function filter(array $items, array $target): array
6969
return $items;
7070
}
7171

72-
protected function map(array $items): array
72+
protected function map(array $items, bool $secure): array
7373
{
7474
$result = [];
7575

7676
foreach ($items as $key => $value) {
7777
$key = Str::upper($key);
7878

79-
$replaced = $this->replace($key, $value);
79+
$replaced = $this->replace($key, $value, $secure);
8080

8181
$result[$key] = $this->parseStringValue($replaced);
8282
}
@@ -117,9 +117,9 @@ protected function compile(array $items): string
117117
return trim($result) . $separator;
118118
}
119119

120-
protected function replace(string $key, $value)
120+
protected function replace(string $key, $value, bool $secure)
121121
{
122-
return $this->isForceHiding($key) ? null : $this->value($key, $value);
122+
return $this->isForceHiding($key) && $secure ? null : $this->value($key, $value);
123123
}
124124

125125
protected function isForceHiding(string $key): bool

src/Services/Syncer.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,16 @@ protected function parsed(array $files): array
8989

9090
protected function compiler(array $items, array $target = []): Compiler
9191
{
92-
return $this->compiler->items($items, $target);
92+
return $this->compiler->items($items, $target, $this->hasSecure());
9393
}
9494

9595
protected function targetPath(): string
9696
{
9797
return rtrim($this->path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $this->filename;
9898
}
99+
100+
protected function hasSecure(): bool
101+
{
102+
return ! $this->sync;
103+
}
99104
}

tests/Concerns/Files.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ trait Files
1010

1111
protected $filename = '.env.example';
1212

13-
protected function expected(?string $path = null): string
13+
protected function expected(): string
1414
{
15-
$filename = $path ?: $this->fixture_expected;
16-
17-
return realpath(__DIR__ . '/../fixtures/expected/' . $filename);
15+
return realpath(__DIR__ . '/../fixtures/expected/' . $this->fixture_expected);
1816
}
1917

2018
protected function targetPath(): string

tests/Unit/MainTest.php

-15
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,6 @@ public function testStoring()
4242
$this->assertFileEquals($this->expected(), $this->targetPath());
4343
}
4444

45-
public function testSync()
46-
{
47-
$source = __DIR__ . '/../fixtures/source/.env.sync';
48-
$target = __DIR__ . '/../fixtures/expected/expected-sync';
49-
50-
$service = $this->service();
51-
52-
$service->path($this->path);
53-
$service->filename($this->filename, $source);
54-
55-
$service->store();
56-
57-
$this->assertFileEquals($this->expected('expected-sync'), $target);
58-
}
59-
6045
public function testCustomPathFailed()
6146
{
6247
$this->expectException(DirectoryNotFoundException::class);

tests/Unit/WithEnvTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Unit;
6+
7+
use DragonCode\Support\Facades\Helpers\Filesystem\File;
8+
use Tests\TestCase;
9+
10+
class WithEnvTest extends TestCase
11+
{
12+
protected $fixture_expected = 'expected-sync';
13+
14+
protected $filename = '.env';
15+
16+
public function testSync()
17+
{
18+
$this->copyFixture();
19+
20+
$service = $this->service();
21+
22+
$service->path($this->path);
23+
$service->filename($this->filename, true);
24+
25+
$service->store();
26+
27+
$this->assertFileExists($this->targetPath());
28+
$this->assertFileEquals($this->expected(), $this->targetPath());
29+
}
30+
31+
protected function copyFixture()
32+
{
33+
File::copy($this->path . '/.env.sync', $this->targetPath());
34+
}
35+
}

tests/fixtures/expected/expected-sync

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
APP_DEBUG=true
2-
APP_ENV=testing
3-
APP_KEY=
2+
APP_ENV=local
3+
APP_KEY=foo
44
APP_NAME=Laravel
55
APP_URL=https://example.com
6+
7+
DB_CONNECTION=mysql
8+
DB_DATABASE=forge
9+
DB_HOST=qwerty
10+
DB_PASSWORD=root
11+
DB_USERNAME=root

tests/fixtures/source/.env.sync

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
APP_DEBUG=true
2-
APP_ENV=testing
2+
APP_ENV=local
33
APP_KEY=foo
44
APP_NAME=Laravel
55
APP_URL=https://example.com
66

7+
DB_CONNECTION=mysql
8+
DB_DATABASE=forge
9+
DB_HOST=qwerty
10+
DB_PASSWORD=root
11+
DB_USERNAME=root
12+
713
CUSTOM_KEY_1=
814
CUSTOM_KEY_2=
915
CUSTOM_KEY_3=

tests/fixtures/source/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.env.example
2+
.env

0 commit comments

Comments
 (0)