Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 58 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| EnvDiffService | |
0.00% |
0 / 58 |
|
0.00% |
0 / 5 |
240 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getMissingVariables | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| getObsoleteVariables | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| parseEnvFile | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
56 | |||
| isRequired | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Modules\Updater\Services\Core; |
| 4 | |
| 5 | class EnvDiffService |
| 6 | { |
| 7 | protected string $localEnvPath; |
| 8 | protected string $exampleEnvPath; |
| 9 | |
| 10 | public function __construct() |
| 11 | { |
| 12 | $this->localEnvPath = ROOTPATH . '.env'; |
| 13 | $this->exampleEnvPath = WRITEPATH . 'updates/extracted/.env.example'; |
| 14 | } |
| 15 | |
| 16 | // ========================================================= |
| 17 | // Détecte les variables manquantes dans le .env local |
| 18 | // ========================================================= |
| 19 | |
| 20 | public function getMissingVariables(): array |
| 21 | { |
| 22 | $localVars = $this->parseEnvFile($this->localEnvPath); |
| 23 | $exampleVars = $this->parseEnvFile($this->exampleEnvPath); |
| 24 | |
| 25 | $missing = []; |
| 26 | |
| 27 | foreach ($exampleVars as $key => $exampleEntry) { |
| 28 | |
| 29 | // Variable absente du .env local → manquante |
| 30 | if (!array_key_exists($key, $localVars)) { |
| 31 | $missing[$key] = [ |
| 32 | 'key' => $key, |
| 33 | 'default' => $exampleEntry['value'], |
| 34 | 'comment' => $exampleEntry['comment'], |
| 35 | 'section' => $exampleEntry['section'], |
| 36 | 'required' => $this->isRequired($key), |
| 37 | ]; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return $missing; |
| 42 | } |
| 43 | |
| 44 | // ========================================================= |
| 45 | // Détecte les variables présentes localement |
| 46 | // mais absentes du .env.example (obsolètes) |
| 47 | // ========================================================= |
| 48 | |
| 49 | public function getObsoleteVariables(): array |
| 50 | { |
| 51 | $localVars = $this->parseEnvFile($this->localEnvPath); |
| 52 | $exampleVars = $this->parseEnvFile($this->exampleEnvPath); |
| 53 | |
| 54 | $obsolete = []; |
| 55 | |
| 56 | foreach ($localVars as $key => $value) { |
| 57 | if (!array_key_exists($key, $exampleVars)) { |
| 58 | $obsolete[] = $key; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return $obsolete; |
| 63 | } |
| 64 | |
| 65 | // ========================================================= |
| 66 | // Parse un fichier .env et retourne les variables |
| 67 | // ========================================================= |
| 68 | |
| 69 | public function parseEnvFile(string $path): array |
| 70 | { |
| 71 | if (!file_exists($path)) { |
| 72 | return []; |
| 73 | } |
| 74 | |
| 75 | $lines = file($path, FILE_IGNORE_NEW_LINES); |
| 76 | $vars = []; |
| 77 | $section = null; |
| 78 | $comment = null; |
| 79 | |
| 80 | foreach ($lines as $line) { |
| 81 | $line = trim($line); |
| 82 | |
| 83 | // Section (# Title) |
| 84 | if (preg_match('/^#\s*[-=]+\s*$/', $line)) { |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | if (preg_match('/^#\s+(.+)$/', $line, $matches)) { |
| 89 | $section = trim($matches[1]); |
| 90 | $comment = trim($matches[1]); |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | // Ligne vide |
| 95 | if (empty($line)) { |
| 96 | $comment = null; |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | // Variable = valeur |
| 101 | if (preg_match('/^([A-Z0-9_]+)\s*=\s*(.*)$/', $line, $matches)) { |
| 102 | $key = $matches[1]; |
| 103 | $value = trim($matches[2], '"\''); |
| 104 | |
| 105 | $vars[$key] = [ |
| 106 | 'value' => $value, |
| 107 | 'comment' => $comment, |
| 108 | 'section' => $section, |
| 109 | ]; |
| 110 | |
| 111 | $comment = null; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return $vars; |
| 116 | } |
| 117 | |
| 118 | // ========================================================= |
| 119 | // Variables qui nécessitent une valeur obligatoire |
| 120 | // ========================================================= |
| 121 | |
| 122 | protected function isRequired(string $key): bool |
| 123 | { |
| 124 | $required = [ |
| 125 | 'DB_HOST', |
| 126 | 'DB_DATABASE', |
| 127 | 'DB_USERNAME', |
| 128 | 'UPDATER_LICENCE_KEY', |
| 129 | 'UPDATER_REGISTRY_URL', |
| 130 | 'APP_KEY', |
| 131 | ]; |
| 132 | |
| 133 | return in_array($key, $required); |
| 134 | } |
| 135 | } |