Skip to content

Commit

Permalink
Add the ability to remove custom .env keys when bundling the application
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Aug 7, 2023
1 parent 6a43575 commit 15814d8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions config/nativephp.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
*/
'provider' => \App\Providers\NativeAppServiceProvider::class,

/**
* A list of environment keys that should be removed from the
* .env file when the application is bundled for production.
*/
'cleanup_env_keys' => [],

/**
* The NativePHP updater configuration.
*/
Expand Down
29 changes: 28 additions & 1 deletion src/Commands/MinifyApplicationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Native\Laravel\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Native\Laravel\Compactor\Php;
use Symfony\Component\Finder\Finder;

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

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

$compactor = new \Native\Laravel\Compactor\Php();
$this->cleanUpEnvFile($appPath);

$compactor = new Php();

$phpFiles = Finder::create()
->files()
Expand All @@ -33,4 +37,27 @@ public function handle()
file_put_contents($phpFile->getRealPath(), $minifiedContent);
}
}

protected function cleanUpEnvFile(string $appPath): void
{
$envFile = $appPath.'/.env';

if (! file_exists($envFile)) {
return;
}

$this->info('Cleaning up .env file…');

$cleanUpKeys = config('nativephp.cleanup_env_keys', []);

$envContent = file_get_contents($envFile);
$envValues = collect(explode("\n", $envContent))
->filter(function (string $line) use ($cleanUpKeys) {
$key = Str::before($line, '=');
return ! in_array($key, $cleanUpKeys);
})
->join("\n");

file_put_contents($envFile, $envValues);
}
}

0 comments on commit 15814d8

Please sign in to comment.