Skip to content

Commit

Permalink
added config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
msamgan committed Oct 6, 2024
1 parent cea2de5 commit 72a86c9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ You can install the package via composer:
composer require msamgan/laravel-env-keys-checker
```

You can publish the config file with:

```bash
php artisan vendor:publish --tag="laravel-env-keys-checker-config"
```

## Usage

```bash
Expand Down
5 changes: 4 additions & 1 deletion config/env-keys-checker.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

// config for Msamgan/LaravelEnvKeysChecker
return [
// List of all the .env files to ignore while checking the env keys
'ignore_files' => [],

// List of all the env keys to ignore while checking the env keys
'ignore_keys' => [],
];
25 changes: 15 additions & 10 deletions src/Commands/LaravelEnvKeysCheckerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ public function handle(): int
{
$envFiles = glob(base_path('.env*'));

$ignoredFiles = config('env-keys-checker.ignore_files', []);

if (empty($envFiles)) {
$this->error('!! No .env files found.');

return self::FAILURE;
}

$envFiles = collect($envFiles)->filter(function ($file) use ($ignoredFiles) {
return ! in_array(basename($file), $ignoredFiles);
})->toArray();

$keys = $this->getAllKeys($envFiles);

$missingKeys = collect();
Expand All @@ -46,29 +52,29 @@ public function handle(): int

private function getAllKeys($files): Collection
{
$ignoredKeys = config('env-keys-checker.ignore_keys', []);

$files = is_array($files)
? collect($files)
: collect([$files]);

$keyArray = $files
->map(function ($file) {
$lines = file($file);

return collect($lines)->map(function ($line, $index) {
$key = explode('=', $line)[0];
return $files
->map(function ($file) use ($ignoredKeys) {
return collect(file($file))->map(function ($line, $index) {
[$key] = explode('=', $line);

return [
'key' => $key,
'line' => $index + 1,
];
})->filter(function ($item) {
return $item['key'] !== "\n" && ! str_starts_with($item['key'], '#');
})->filter(function ($keyData) use ($ignoredKeys) {
return ! in_array($keyData['key'], $ignoredKeys);
});
})
->flatten(1)
->unique('key');

return $keyArray;
}

private function checkForKeyInFile($keyData, $envFiles, $missingKeys): void
Expand All @@ -85,11 +91,10 @@ private function checkForKeyInFile($keyData, $envFiles, $missingKeys): void
}

if (! $keyExists) {
$fileParts = explode(DIRECTORY_SEPARATOR, $envFile);
$missingKeys->push([
'line' => $keyData['line'],
'key' => $keyData['key'],
'envFile' => end($fileParts),
'envFile' => basename($envFile),
]);
}
});
Expand Down
2 changes: 0 additions & 2 deletions src/LaravelEnvKeysCheckerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public function configurePackage(Package $package): void
$package
->name('laravel-env-keys-checker')
->hasConfigFile()
->hasViews()
->hasMigration('create_laravel_env_keys_checker_table')
->hasCommand(LaravelEnvKeysCheckerCommand::class);
}
}

0 comments on commit 72a86c9

Please sign in to comment.