|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Msamgan\LaravelEnvKeysChecker\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Msamgan\LaravelEnvKeysChecker\Actions\GetKeys; |
| 7 | +use Msamgan\LaravelEnvKeysChecker\Concerns\HelperFunctions; |
| 8 | + |
| 9 | +class EnvKeysSyncCommand extends Command |
| 10 | +{ |
| 11 | + use HelperFunctions; |
| 12 | + |
| 13 | + public $signature = 'env:sync-keys'; |
| 14 | + |
| 15 | + public $description = 'Sync keys from master .env file to other .env files.'; |
| 16 | + |
| 17 | + public function handle(GetKeys $getKeys): int |
| 18 | + { |
| 19 | + $allKeysCheck = $this->call('env:keys-check', [ |
| 20 | + '--auto-add' => 'none', |
| 21 | + '--no-progress' => true, |
| 22 | + '--no-display' => true, |
| 23 | + ]); |
| 24 | + |
| 25 | + if ($allKeysCheck === self::FAILURE) { |
| 26 | + $this->showFailureInfo('keys mismatch found. Syncing keys is not possible. Please fix the keys mismatch first.'); |
| 27 | + $this->showFailureInfo('Run `php artisan env:keys-check --auto-add=auto` to add missing keys automatically.'); |
| 28 | + |
| 29 | + return self::FAILURE; |
| 30 | + } |
| 31 | + |
| 32 | + $envFiles = glob(base_path('.env*')); |
| 33 | + $ignoredFiles = config('env-keys-checker.ignore_files', []); |
| 34 | + |
| 35 | + if (empty($envFiles)) { |
| 36 | + $this->showFailureInfo( |
| 37 | + message: 'No .env files found.' |
| 38 | + ); |
| 39 | + |
| 40 | + return self::FAILURE; |
| 41 | + } |
| 42 | + |
| 43 | + $envFiles = collect($envFiles)->filter(function ($file) use ($ignoredFiles) { |
| 44 | + return ! in_array(basename($file), $ignoredFiles); |
| 45 | + })->toArray(); |
| 46 | + |
| 47 | + if (empty($envFiles)) { |
| 48 | + $this->showFailureInfo( |
| 49 | + message: 'No .env files found.' |
| 50 | + ); |
| 51 | + |
| 52 | + return self::FAILURE; |
| 53 | + } |
| 54 | + |
| 55 | + $envFiles = collect($envFiles)->filter(function ($file) { |
| 56 | + return basename($file) !== config('env-keys-checker.master_env', '.env'); |
| 57 | + }); |
| 58 | + |
| 59 | + $envFiles->each(function ($envFile) { |
| 60 | + $totalKeysFromMaster = count(file(config('env-keys-checker.master_env', '.env'))); |
| 61 | + for ($line = 1; $line <= $totalKeysFromMaster; $line++) { |
| 62 | + $keyMaster = $this->getKeyFromFileOnLine(config('env-keys-checker.master_env', '.env'), $line); |
| 63 | + $keyEnvFile = $this->getKeyFromFileOnLine($envFile, $line); |
| 64 | + |
| 65 | + if ($keyMaster === $keyEnvFile) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + $keyMasterKey = explode('=', $keyMaster)[0]; |
| 70 | + $keyEnvFileKey = explode('=', $keyEnvFile)[0]; |
| 71 | + |
| 72 | + if ($keyMasterKey === $keyEnvFileKey) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + if ($this->checkIfComment($keyMaster)) { |
| 77 | + $this->pushKeyOnLine($envFile, $line, $keyMaster); |
| 78 | + |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + if ($this->checkIfEmptyLine($keyMaster)) { |
| 83 | + $this->pushKeyOnLine($envFile, $line, $keyMaster); |
| 84 | + |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + $this->moveKeyToLine($envFile, $keyMasterKey, $line); |
| 89 | + } |
| 90 | + |
| 91 | + $this->removeAllLinesAfter($totalKeysFromMaster, $envFile); |
| 92 | + }); |
| 93 | + |
| 94 | + $this->showSuccessInfo( |
| 95 | + message: 'Keys synced successfully.' |
| 96 | + ); |
| 97 | + |
| 98 | + return self::SUCCESS; |
| 99 | + } |
| 100 | + |
| 101 | + private function getKeyFromFileOnLine(string $file, int $line): string |
| 102 | + { |
| 103 | + return file($file)[$line - 1]; |
| 104 | + } |
| 105 | + |
| 106 | + private function checkIfComment(string $line): bool |
| 107 | + { |
| 108 | + return str_starts_with($line, '#'); |
| 109 | + } |
| 110 | + |
| 111 | + private function pushKeyOnLine($file, $line, $key): void |
| 112 | + { |
| 113 | + $lines = file($file); |
| 114 | + array_splice($lines, $line - 1, 0, $key); |
| 115 | + |
| 116 | + file_put_contents($file, implode('', $lines)); |
| 117 | + } |
| 118 | + |
| 119 | + private function checkIfEmptyLine(string $line): bool |
| 120 | + { |
| 121 | + return $line === "\n"; |
| 122 | + } |
| 123 | + |
| 124 | + private function moveKeyToLine(string $file, string $key, int $toLine): void |
| 125 | + { |
| 126 | + $lines = file($file); |
| 127 | + $keyLine = array_filter($lines, function ($line) use ($key) { |
| 128 | + return str_starts_with($line, $key); |
| 129 | + }); |
| 130 | + |
| 131 | + if (empty($keyLine)) { |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + $keyLine = array_keys($keyLine)[0]; |
| 136 | + $keyData = $lines[$keyLine]; |
| 137 | + |
| 138 | + unset($lines[$keyLine]); |
| 139 | + |
| 140 | + array_splice($lines, $toLine - 1, 0, $keyData); |
| 141 | + |
| 142 | + file_put_contents($file, implode('', $lines)); |
| 143 | + } |
| 144 | + |
| 145 | + private function removeAllLinesAfter(int $lineNumber, string $file): void |
| 146 | + { |
| 147 | + $lines = file($file); |
| 148 | + $lines = array_slice($lines, 0, $lineNumber); |
| 149 | + |
| 150 | + file_put_contents($file, implode('', $lines)); |
| 151 | + } |
| 152 | +} |
0 commit comments