Skip to content

Commit 15814d8

Browse files
committed
Add the ability to remove custom .env keys when bundling the application
1 parent 6a43575 commit 15814d8

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

config/nativephp.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
*/
3939
'provider' => \App\Providers\NativeAppServiceProvider::class,
4040

41+
/**
42+
* A list of environment keys that should be removed from the
43+
* .env file when the application is bundled for production.
44+
*/
45+
'cleanup_env_keys' => [],
46+
4147
/**
4248
* The NativePHP updater configuration.
4349
*/

src/Commands/MinifyApplicationCommand.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Native\Laravel\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Str;
7+
use Native\Laravel\Compactor\Php;
68
use Symfony\Component\Finder\Finder;
79

810
class MinifyApplicationCommand extends Command
@@ -21,7 +23,9 @@ public function handle()
2123

2224
$this->info('Minifying application…');
2325

24-
$compactor = new \Native\Laravel\Compactor\Php();
26+
$this->cleanUpEnvFile($appPath);
27+
28+
$compactor = new Php();
2529

2630
$phpFiles = Finder::create()
2731
->files()
@@ -33,4 +37,27 @@ public function handle()
3337
file_put_contents($phpFile->getRealPath(), $minifiedContent);
3438
}
3539
}
40+
41+
protected function cleanUpEnvFile(string $appPath): void
42+
{
43+
$envFile = $appPath.'/.env';
44+
45+
if (! file_exists($envFile)) {
46+
return;
47+
}
48+
49+
$this->info('Cleaning up .env file…');
50+
51+
$cleanUpKeys = config('nativephp.cleanup_env_keys', []);
52+
53+
$envContent = file_get_contents($envFile);
54+
$envValues = collect(explode("\n", $envContent))
55+
->filter(function (string $line) use ($cleanUpKeys) {
56+
$key = Str::before($line, '=');
57+
return ! in_array($key, $cleanUpKeys);
58+
})
59+
->join("\n");
60+
61+
file_put_contents($envFile, $envValues);
62+
}
3663
}

0 commit comments

Comments
 (0)